首页 > 解决方案 > Initcap 不适用于特殊字符

问题描述

我正在做

select initcap('the /soap') from dual; 

结果:/肥皂

但我希望结果是:

肥皂

我希望我的肥皂变小。我怎样才能做到这一点?

标签: oracleoracle11g

解决方案


Initcap 就是这样工作的。

文档说:

单词由空格或非字母数字字符分隔。

这意味着“soap”被认为是一个单独的词(而不是“/soap”,这是你想要的)。

解决方法需要一些输入,例如:

SQL> with test (col) as
  2    (select 'the /soap' from dual),
  3  t_split as
  4    -- split string to rows
  5    (select regexp_substr(col, '[^ ]+', 1, level) val,
  6            level lvl
  7     from test
  8     connect by level <= regexp_count(col, ' ') + 1
  9    ),
 10  t_initcap as
 11    -- apply INITCAP only to words that don't contain special characters
 12    (select case when not regexp_like(val, '[^0-9A-Za-z]') then initcap(val)
 13                 else val
 14            end valinit,
 15            lvl
 16     from t_split
 17    )
 18    -- merge them back
 19  select listagg(valinit, ' ') within group (order by lvl) result
 20  from t_initcap;

RESULT
--------------------------------------------------------------------------------
The /soap

SQL>

推荐阅读