oracle - Initcap 不适用于特殊字符
问题描述
我正在做
select initcap('the /soap') from dual;
结果:/肥皂
但我希望结果是:
肥皂
我希望我的肥皂变小。我怎样才能做到这一点?
解决方案
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>
推荐阅读
- apache-spark - 使用定义的模式以 avro 格式从 Spark 写入 Kafka?
- ios - StatusBarStyle 在 ios 13.5 上不会改变
- javascript - 处理 AJAX 结果
- xml - 在 Google Cloud Endpoint 中支持 XML
- java - 最大堆大小无效?
- python - Python关于IO二进制文件中ASCII码0x1a的问题
- java - 无法解决 ArithmeticException?/ 零?
- r - gghistogram + gganimate中的鬼条
- date - 将日期格式 yywwd 转换为 dd-mm-yy
- c++ - 关于在 STL 列表和函数对象 C++ 中使用 while 循环而不是 for 的问题