PL/SQL procedure: UPDATE uppercase names to initcaps, with special handling for some values

时光怂恿深爱的人放手 提交于 2019-12-07 19:13:57

问题


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.


回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!