zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Oracle函数->TRIM(去掉首尾空格、首尾字符)

Oracle字符 函数 gt 去掉 空格 -& trim
2023-09-27 14:25:30 时间

 

TRIM

语法:

TRIM([ { { LEADING | TRAILING | BOTH }[ trim_character ]| trim_character } FROM ] trim_source)

 

序号 函数                                                  函数结果       备注
1 trim(' test ') 'test' 删除字符串前后空格
2 trim(both from ' test ') 'test' 'both'参数表示同时去除字符串前后所指定的内容(默认情况下删除空格)
3 trim(trailing from ' test ') ' test' 'trailing'参数表示删除字符串尾部空格
4 trim(leading from ' test ') 'test ' 'leading'参数表示删除字符串头部空格
5 trim('x' from 'xxxtestxxx') 'test' 删除字符串前后的字符'x' 
6 trim(both 'x' from 'xxxtestxxx') 'test' 删除字符串前后的字符'x'
7 trim(trailing 'x' from  'xxxtestxxx') 'xxxtest' 删除字符串尾部的字符'x'
8 trim(leading 'x' from 'xxxtestxxx') 'testxxx' 删除字符串头部的字符'x'
       
       

 

 注意: 'trim_character'参数只允许包含一个字符,不支持多个字符。多个字符报错信息如下

 trim不能满足我们去除多字符要求,但是我们可以使用rtrim和ltrim来处理

 

 

LTRIM /RTRIM

序号 函数                                                              函数结果           备注                                                                           
 1  rtrim('xyxxtestxyyx','xy') 'xyxxtest'   删除字符串右边的'xy'字符
 2  ltrim('xyxxtestxyyx','xy') 'testxyyx'   删除字符串左边的'xy'字符
 3  ltrim(rtrim('xyxxtestxyyx','xy'),'xy')  'test'  删除字符串左右两边的'xy'字符
 4      
       

 

注意:使用LTRIM和RTRIM函数时的注意事项:'xy'不表示整个'xy'字符串进行匹配,而是发现任意的字符'x'或字符'y'均做删除操作