源数据
with temp as(
select 1 as id ,'张三' as name ,101 as class_id from dual
union all
select 2 as id ,'李四' as name ,101 as class_id from dual
union all
select 3 as id ,'王二' as name ,102 as class_id from dual
union all
select 4 as id ,'王五' as name ,103 as class_id from dual
union all
select 5 as id ,'张七' as name ,103 as class_id from dual
)
1、拼接 concat() 和 ||
select concat(t.id,t.name) ,t.id||t.name from temp t
标题2、替换 replace
Replace (‘要更新的字段’,’要替换的字符串’,’替换后的字符串’)
select replace(t.name,'张','李') ,t.name from temp t
3、截取 substr
1.substr(‘被截取的字段’,int a,int b)
2.substr(‘被截取的字段’,int a)
第一种 是表示从a个字符开始(包含a这个字符),b表示截取的长度
第二中 是表示从a个字符开始(包含a这个字符)截取剩下的所有字符
eg:
select substr('helloword',0,100) from dual -- 超出最多长度截取所有 返回 'helloword'
select substr('helloword',0) from dual --截取所有 返回 'helloword'
select substr('helloword',1) from dual --截取所有 返回 'helloword'
select substr('helloword',2) from dual --截取从第2个字符开始(包含第2个字符) 返回 'elloword'
select substr('helloword',-1) from dual --截取从倒数第一个字符开始包含倒数第一个字符 返回 'd'
4 、查找 instr
1.instr(‘查找的字段’,’查找的内容’)–返回第一次出现的位置
select instr('helloword','l') from dual 3
select instr('helloword','ll') from dual 3 整段字符第一次出现位置
select instr('helloword','llt') from dual 0 找不到返回0
2.instr(‘查找的字段’,’查找的内容’,’从哪个位置开始检索’)–返回第一次出现的位置
select instr('helloword','l',3) from dual 返回3
select instr('helloword','l',4) from dual 返回4
3.instr(‘查找的字段’,’查找的内容’,’从哪个位置开始检索’,’第几次出现’)
select instr('helloword','l',3,1) from dual 返回3
select instr('helloword','l',3,2) from dual 返回4
5、长度 length
1.lengthb()返回的是字符串的长度,返回的是多少字节
select lengthb('我helloword') from dual 返回13
2.length()返回的是字符串的长度,返回的是多少字符
select length('我helloword') from dual 返回10、
通常用 lengthb(‘string’)=length(‘string’) 判断是否有中文
6、去空格 trim
不能去除字符中的空格
1.去除左边空格
select ltrim(' 我hello word ') from dual 返回 ‘我hello word ’
select trim(leading from' 我hello word ') from dual 返回 ‘我hello word ’
select trim( leading 'x' from'xxx我helloxxxwordxx') from dual 返回‘我helloxxxwordxx’
2.去除右边的空格
select rtrim(' 我hello word ') from dual 返回 ‘ 我hello word’
select trim(trailing from' 我hello word ') from dual 返回 ‘ 我hello word’
select trim( trailing 'x' from'xxx我helloxxxwordxx') from dual 返回‘xxx我helloxxxword’
3.去除左右的空格
select trim(' 我hello word ') from dual 返回 ‘我hello word’
4.删除头和尾的某种字符,不能删除中间的
select trim( 'x' from'xxx我helloxxxwordxx') from dual 返回 ‘我helloxxxword’
7、 nvl 空置转换
select nvl(47,'14') from dual 返回 47
select nvl(null,'14') from dual 返回 ‘14’
8、类型转换 cast、to_char、to_date、to_number
1.cast(‘值或者字段’ as 数据类型(长度))
select cast('14' as number(20,0)) from dual
8.1、to_char
8.2、Data------>char
select to_char(sysdate ,'yyyy-MM-dd hh24:mi:ss') from dual 返回 2020-08-19 14:14:25
8.3、Number ----->char
select to_char(7788 ) from dual
select to_char(7788,'99,999' ) from dual 返回 7,788
select to_char(7788,'$99,999.9' ) from dual $7,788.0
select to_char(7788,'xxxx' ) from dual 转换16进制 1e6c
select to_char(7788.97,'9999.9' ) from dual 7789.0 保留一位小数
8.4、To_date
select to_date('2020-02-01 14:25:25' ,'yyyy-MM-dd hh24:mi:ss') from dual
select to_char(1406538765000/(1000*60*60*24)+to_date('1970-01-01 08:00:00','yyyy-MM-dd hh24:mi:ss') ,'yyyy-MM-dd hh24:mi:ss') from dual
9、反转
select reverse('88899') from dual 返回 ‘99888’