TRIM, LTRIM and RTRIM are SQL functions used to filter unwanted characters from strings. By default they remove spaces, but a set of characters can be specified for removal as well.
TRIM, LTRIM 、RTRIM是用于过滤一些不想要的字符的sql函数,默认去除空格,也可以去除指定的字符串。
TRIM enables you to trim leading or trailing characters (or both) from a character string. If trim_character or trim_source is a character literal, then you must enclose it in single quotes.
If you specify LEADING, then Oracle Database removes any leading characters equal to trim_character.
If you specify TRAILING, then Oracle removes any trailing characters equal to trim_character.
If you specify BOTH or none of the three, then Oracle removes leading and trailing characters equal to trim_character.
If you do not specify trim_character, then the default value is a blank space.
If you specify only trim_source, then Oracle removes leading and trailing blank spaces.
The function returns a value with datatype VARCHAR2. The maximum length of the value is the length of trim_source.
If either trim_source or trim_character is null, then the TRIM function returns null.
Both trim_character and trim_source can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is of VARCHAR2 datatype if trim_source is a character datatype and a LOB if trim_source is a LOB datatype. The return string is in the same character set as trim_source.
官方文档:http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions199.htm
下面举例说明:
1、trim(str)等价于trim(both from '字符串')
select trim(' 去除前后空格 ') from dual;
等价于
SQL> select trim(both from' 去除前后空格 ') from dual;
TRIM(BOTHFROM'去除前后空格')
----------------------------
去除前后空格
2、trim(leading from str)
SQL> select trim(leading from ' 用leading只去除前面空格,后面的空格不会去除 ')||'看看前面那句话后面的空格没去掉吧' from dual;
TRIM(LEADINGFROM'用LEADING只去
-----------------------------------------------------------------------------
用leading只去除前面空格,后面的空格不会去除 看看前面那句话后面的空格没去掉吧
3、trim(trailing from ' 字符串')
SQL> select 'Start'||trim(trailing from ' 用trailing只去除末尾的空格,字符串前面的空格不会去除 ')||'End' from dual;
'START'||TRIM(TRAILINGFROM'用T
--------------------------------------------------------------
Start 用trailing只去除末尾的空格,字符串前面的空格不会去除End
4、删除字符串末尾的字符d
SQL> select 'Start='||trim(trailing 'd' from 'Hello World')||'=End' from dual;
'START='||TRIM(TRAILING'D'FROM
------------------------------
Start=Hello Worl=End
LTRIM
LTRIM()(char [,set]):该函数用于去掉字符串char左端所包含的set中的任何字符。Oracle从左端第一个字符开始扫描,逐一去掉在set中出现的字符,当遇到不是set中的字符时终止,然后返回剩余结果。
先看几个实例:
SQL> select ltrim('109224323','109') from dual;
LTRIM('109224323','109')
------------------------
224323
这个的功能应该都知道的噢~~ 再来看一个:
SQL> select ltrim('10900094323','109') from dual;
LTRIM('10900094323','109')
---------------------------
4323
是不是有点迷糊了?按道理说应该是00094323的结果嘛~~ 再来看两个对比的:
SQL> select ltrim('10900111000991110224323','109') from dual;
LTRIM('10900111000991110224323
------------------------------
224323
SQL> select ltrim('109200111000991110224323','109') from dual;
LTRIM('10920011100099111022432
------------------------------
200111000991110224323
是不是有这样的疑问:为什么第二个查询语句多了一个2就没被截了呢?
再来看一个:
SQL> select ltrim('902100111000991110224323','109') from dual;
LTRIM('90210011100099111022432
------------------------------
2100111000991110224323
我想大家都都会想:按道理说是截109的值,为什么90也被截了?
总结:ltrim(x,y) 函数是按照y中的字符一个一个截掉x中的字符,并且是从左边开始执行的,只要遇到y中有的字符, x中的字符都会被截掉, 直到在x的字符中遇到y中没有的字符为止函数命令才结束 .
函数将109当成了三个字符以1,0,9在字符串开始直道出现不为1,0,9这三个字符中的任意一个开始截取;
可以看出,ltrim函数是从匹配函数开始之后出现在子串中任何字符都被屏蔽掉了;
ltrim('902100111000991110224323','109')从左边开始截取,直到出现不符合1,0,9结束,即到2停止截取,不管2后面是否存在1,0,9照样输出,rtrim()同理
RTRIM 同LTRIM。。。