ORACLE 字符串函数

字符串函数

如果错误或者更加优秀的解决方法,欢迎各位大大指教,共同进步,共创和谐社会,哈哈。
emmmmm,本人有点小懒可能不会及时更新或回复,一切随缘吧。

1、INSTR()

格式一:instr(string1,string2) 返回string2第一次出现在string1的位置,若没有出现,则显示0

select instr('hello','lc') from dual;  // 0
select instr('hello','lo') from dual;  // 4

格式二:instr(string1,string2,start_position,nth_potision)
返回string1从start_position位置开始检索,找到第n次出现string2的位置

select instr('hello','l',1,2) from dual;    // 4
select instr('hello','l',4,1) from dual;    // 4
2、CONCAT

concat(string1,string2) 把string1和string2拼接起来

select concat('hello','world') from dual;  //helloworld
3、LENGTH、LENGTHB

length(string1):返回字符串所占字符长度,单位是字符
lengthb(string1):返回字符串所占字节长度,单位是字节

select length('你好') from dual;  // 2
select lengthb('你好') from dual; // 4
4、SUBSTR

格式一、substr(string1,a,b)
从string1的a位置截取长度为b的字符串

select substr('helloworld',2,4) from dual;  //ello
select substr('helloworld',10,4) from dual; //d
select substr('helloworld',12,4) from dual; //null

格式二、substr(string1,a)
从string1的第a个字符开始截取后面所有字母

select substr('helloworld',2) from dual;    //elloworld
select substr('helloworld',-2) from dual;   //ld(可以用此方法获取字符串最后几位)
5、LPAD、RPAD

lpad(string1,length,[pad_string]) 左填充
rpad(string1,length,[pad_string]) 右填充
string1待填充字符串
length字符长度,如果n大于string1,则填充至n,如果小于string1,则从左往右截取string1长度为length的字符串
pad_string填充字符,缺省值为空格

select lpad('hello',10) from dual;      //'     hello'
select lpad('hello',10,0) from dual;    //00000hello
select lpad('hello',3) from dual;       //hel

select lpad('hello',10) from dual;      //'hello     '
select lpad('hello',10,0) from dual;    //hello00000
select lpad('hello',3) from dual;       //hel
6、TRIM,LTRIM,RTRIM

1、去除空格
trim去除两端空格,不去除字符串中间空格
ltrim去除左端空格,不去除中间
rtrim去除右端空格,不去除中间

select trim('  aa bb cc  ')from dual;       //'aa bb cc'
select ltrim('  aa bb cc  ')from dual;      //'aa bb cc  '
select rtrim('  aa bb cc  ')from dual;      //'  aa bb cc'

2、去除指定符号
trim( leading|trailing|both string1 from string2)
从前面,后面,前后面去除string2中的string1,缺省值为both

trim只能去除单个字符,而ltrim和rtrim可以去除多个字符。

select trim(leading 'a' from 'abccba')from dual;    //bccba
select trim(trailing 'a' from 'abccba')from dual;   //abccb
select trim(both 'a' from 'abccba')from dual;       //bccb
select trim('a' from 'abccba') from dual;           //bccb
select trim('ab' from 'abccba') from dual;          //报错    

ltrim(stng1,string2)
从string1中移除string2的字符
移除规则是从string1从左到右,当该字符出现在string2的字符集中,移除该字符,直到出现不存在string2中的字符或string1为空,移除结束

select ltrim('aabbcc','abc') from dual;     //null
select ltrim('aaddbbcc','abc') from dual;   //ddbbcc
select ltrim('aabbccefg','abc') from dual;  //efg

rtrim与ltrim类似,规则改为从右往左

select rtrim('aabbcc','abc') from dual;         //null
select rtrim('aaddbbcc','abc') from dual;       //aadd
select rtrim('aaccbbefg','abc') from dual;      //aaccbbefg
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值