I need a procedure to do something like this: original name: AMSTERDAM new name: Amsterdam. I made this procedure for it:
create or replace PROCEDURE NaamRoutine
is
BEGIN
update Gemeentenew
set gemeentenaam = lower(gemeentenaam);
update Gemeentenew
set gemeentenaam = initcap(gemeentenaam);
END;
The problem is there are a couple of names that start like 'S GRAVENHAGE and need to be 's Gravenhage.
Assuming the special handling is necessary only for names like 'S...
, adding a simple REPLACE
should work. BTW, you don't need two separate UPDATE
statements - INITCAP
will automatically convert non-initial characters to lowercase.:
with v_data(name) as (
select 'AMSTERDAM' from dual union all
select '''S GRAVENHAGE' from dual union all
select 'DEN HAAG' from dual union all
select 'IJSLAND' from dual
)
select
replace(nls_initcap(name, 'NLS_SORT=xDutch'), '''S', '''s') name
from v_data
Name
----
Amsterdam
's Gravenhage
Den Haag
IJsland
This will replace all occurrences of 'S
with 's
. If you need to handle other cases as well, I suggest you try REGEXP_REPLACE()
.
The function NLS_INITCAP
helps with some globalization issues. For example, it capitalizes both the I
and the J
in IJSLAND
. But it doesn't help with 'S
names. I'm not sure if that is a bug with Oracle's globalization functions or if those city names are all exceptions.
In the first place, if you want to manipulate the same field twice, nest them rather than perform them separately.
select initcap( lower( '''S GRAVENHAGE' )) as Resp from dual;
However, you don't even need to do that in this case. initcap
will give you what you want all by itself.
Now, if you have something that starts with an 's and a space ('s xxx), then you can then run it through a regex replacement.
select regexp_replace( initcap( '''S GRAVENHAGE' ), '^''S ', '''s ' ) as Resp from dual;
来源:https://stackoverflow.com/questions/24174226/pl-sql-procedure-update-uppercase-names-to-initcaps-with-special-handling-for