学习笔记-数据库函数replace(),lpad(),substr()的用法

1.查询语句中replace()的用法(eg:sndtime的值为2020-10-30T11:22:20):

select

--有条件的替换(把T替换成空格,得到2020-10-30 11:22:20)
case when sndrcvflag='1' then replace(sndtime,'T',' ') else replace(rcvtime,'T',' ') end,

--无条件的替换(把T替换成空格,得到2020-10-30 11:22:20)
replace(sndtime,'T',' ')

--拼接方式取值成格式为:20201030112220
sndtime[1,4]||sndtime[6,7]||sndtime[9,10]||sndtime[12,13]||sndtime[15,16]||sndtime[18,19]

--replace嵌套多次替换(把-T:替换去掉)取成格式为:20201030112220
replace(replace(replace(sndtime,'T',''),'-',''),':','')

from arpsquerep

2.查询语句字符串补齐的方法lpad():
(1) lpad左侧补齐(原字符串:3352,补齐之后:0000000000003352):
  lpad(str,len,padstr)
  eg:select lpad(oritxno,16,'0') from arpsquerep 返回值为补齐了长度是16的字符串oritxno, 其左边由字符串0填补到16字符长度。

    假如oritxno的长度大于16, 则返回值被缩短至16字符。

(2) rpad右侧补齐(原字符串:3352,补齐之后:3352000000000000):
  rpad(str,len,padstr)
  eg:select rpad(oritxno,16,'0') from arpsquerep 返回值为补齐了长度是16的字符串oritxno, 其右边由字符串0填补到16字符长度。

    假如oritxno的长度大于16, 则返回值被缩短至16字符。

3.截取字段值substr()的用法:
select substr('abcdefg',3,4) from dual; 结果是cdef
select substr('abcdefg',-3,4) from dual; 结果efg
select substr('abcde',2),substr('abcde',-2),substr('abcde',2,3),substr('abcdewww',-7,3) from dual; 结果是bcde、de、bcd、bcd

posted @ 2021-01-06 16:42  小鬼Tang  阅读(370)  评论(0)    收藏  举报