Oracle 的trim,ltrim,rtrim函数的区别
该函数共有两种作用:
第一种,即大家都比较熟悉的去除空格。
例子:
--TRIM去除指定字符的前后空格
SQL> SELECT TRIM(' dd df ') FROM dual;
TRIM('DDDF')
------------
dd df
--LTRIM去除指定字符的前面空格
SQL> SELECT LTRIM(' dd df ') FROM dual;
LTRIM('DDDF')
-------------
dd df
--RTRIM去除指定字符后面后空格
SQL> SELECT RTRIM(' dd df ') FROM dual;
RTRIM('DDDF')
-------------
dd df
第二种:去除字符:
select ltrim('10999910224323109999999','109') from dual;
select rtrim('10999910224323109999999','109') from dual;
这两个是把后面的“109”拆分成字符“1”,“0”,“9”,当有1,0,或者9 的时候就截取掉。
trim去除字符的写法:
--表示字符串string2去除前面|后面|前后面(leading|trailing|both)的字符string1,默认去除方式为both
SELECT TRIM(leading|trailing|both string1 FROM string2) FROM dual;
例子:
SQL> SELECT trim(leading 'd' from 'dfssa') FROM dual;
TRIM(LEADING'D'FROM'DFSSA')
---------------------------
fssa
SQL> SELECT trim(both '1' from '123sfd111') FROM dual;
TRIM(BOTH'1'FROM'123SFD111')
----------------------------
23sfd
SQL> SELECT trim(trailing '2' from '213dsq12') FROM dual;
TRIM(TRAILING'2'FROM'213DSQ12'
------------------------------
213dsq1
注:trim去除字符只能是单个字符,如下,要去除的字符若为字符集则报错
SQL> SELECT trim(trailing '12' from '123dsq12') FROM dual;
SELECT trim(trailing '12' from '123dsq12') FROM dual
ORA-30001: 截取集仅能有一个字符