Oracle 常用函数


1、字符串连接
select concat('0591','8888888') || '转23' from dual
结果:05918888888转23

2、首字母大写

select
initcap('zhang san') from dual
结果:Zhang San

3、INSTR(C1,C2,I,J)

在一个字符串中搜索指定的字符,返回发现指定的字符的位置;
C1 被搜索的字符串
C2 希望搜索的字符串
I 搜索的开始位置,默认为1
J 出现的位置,默认为1
select
instr('test test','es',1,2) from dual
结果:7

4、length 长度

select
length('张三') from dual       结果:2
select
length('abc') from dual      结果:3

5.upper lower 转成大小写

select
upper('aJkjJ') from dual  结果:AJKJJ
select
lower('aJkjJ') from dual  结果:ajkjj

6、字符串补全

ipad(str,count,replaceStr)
str:指定字符串
count:总位数
replaceStr:当str的总位数小于count,那么少出来的位置用replace补全
select
lpad('abc',10,'*') from dual     结果:*******abc
select
rpad('abc',10,'*') from dual     结果:abc*******

7、字符串去除空格
select ltrim('    abc           ') from dual   去除左空格
select rtrim('    abc           ') from dual   去除右空格

trim()没有参数:表示去除所有的空格
select trim('    abc           ') from dual    去除左右空格

trim(s from str)表示把str中的s部分全部删除掉
select trim(0 from 7500) from dual        结果:75
select trim('0' from '     7500       ') from dual         结果:     7500      所以要想删除,必须要先把空格先去除
select trim('0' from trim('     7500       ')) from dual   结果:75


8、字符串替换
select replace('abc','b','d') from dual 结果:adc

9、取余数
select mod(10,3) from dual    结果:1
select mod(3,3) from dual     结果:0
select mod(2,3) from dual     结果:2

10、四舍五入
ROUND和TRUNC
按照指定的精度进行舍入
SQL> select round(55.5),round(-55.4),trunc(55.5),trunc(-55.5) from dual;
结果分别是:56 -55 55 -55

11、小数的精度
select
trunc(123.456,1) from dual 结果:123.4

12、截取字串符
select substr('abcdefg',2,3) from dual
结果:bcd

13、case 的用法
select (case when 'x'='x' then 1 else 0 end) from dual                               结果:1
select (case when 60>50 then 1 when 50>60 then 2 end) from dual      结果:1

14、判断是否为空
NVL(expr1, expr2)->expr1为NULL,返回expr2;不为NULL,返回expr1。注意两者的类型要一致
select nvl(null,'abc') from dual   结果:abc

NVL2 (expr1, expr2, expr3) ->expr1不为NULL,返回expr2;为NULL,返回expr3。expr2和expr3类型不同的话,expr3会转换为expr2的类型
select nvl2(null,null,'abc') from dual    结果:abc

15、是否相等
NULLIF (expr1, expr2) ->相等返回空串,不等返回expr1
select
nullIf('abcd','abc') from dual     结果:abcd

16.转换成NUM

to_number(code))

原文地址:https://www.cnblogs.com/dodui/p/1933120.html