【Oracle】【6】去掉字符串中的空格/字符
SELECT TRIM(' 去除前后空格 ') FROM DUAL; SELECT REPLACE(' 去除 任意位置的空格 ', ' ', '') FROM DUAL;
扩展:
1,both, trailing, leading
(1)默认为删除空格
--结果:<test> SELECT TRIM(BOTH FROM ' test ') FROM DUAL;
--结果:< test> SELECT TRIM(TRAILING FROM ' test ') FROM DUAL;
--结果:<test > SELECT TRIM(LEADING FROM ' test ') FROM DUAL;
(2)“trim_character”参数只允许包含一个字符
--结果:<test> SELECT TRIM(BOTH 'x' FROM 'xxxxTESTxxxx') FROM DUAL;
--结果:<test> SELECT TRIM( 'x' FROM 'xxxxTESTxxxx') FROM DUAL;
2,trim,rtrim, ltrim
(1)默认为删除空格
--结果:<test> SELECT TRIM( ' test ') FROM DUAL;
--结果:< test> SELECT RTRIM( ' test ') FROM DUAL;
--结果:<test > SELECT LRIM( ' test ') FROM DUAL;
(2)“OR”不表示整个“OR”字符串进行匹配,而是发现任意的字符“O”或字符“R”均做删除操作
--是无效的 SELECT TRIM('OR__WORLD__OR','OR') FROM DUAL;
--结果:<OROR__WORLD__> SELECT RTRIM('OROR__WORLD__OROR','OR') FROM DUAL; --结果:<OROR__WORLD__X> SELECT RTRIM('OROR__WORLD__XOROR','OR') FROM DUAL; --结果:<OROR__WORLD__> SELECT RTRIM('OROR__WORLD__R','OR') FROM DUAL;
--结果:<__WORLD__OROR> SELECT LTRIM('OROR__WORLD__OROR','OR') FROM DUAL; --结果:<X__WORLD__OROR> SELECT LTRIM('ORORX__WORLD__OROR','OR') FROM DUAL; --结果:<__WORLD__OROR> SELECT LTRIM('O__WORLD__OROR','OR') FROM DUAL;
--联合使用:结果:<__WORLD__> SELECT RTRIM(LTRIM('OR__WORLD__OR','OR'), 'OR') FROM DUAL;
3,replace函数不仅可以去除字符串两端的空格,也可去除字符串内部的空格。也可去除和替换其他字符
--结果:<把晚上替换成晚上> SELECT REPLACE('把早上替换成晚上', '早上', '晚上') FROM DUAL;
参考博客:
1,(转)oracle中如何对字符串进行去除空格的方法 - 王玉涛 - 博客园
http://www.cnblogs.com/wangyt223/archive/2012/11/29/2793959.html