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

☆樱花仙子☆ 提交于 2019-12-06 04:18:57
Frank Schmitt

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