Oracle 常用的几个通用函数

1、NVL()

格式:NVL(value,string1)

如果value为null,则把该值替换为string1,如果不为空,还是该值本身;

用法:select nvl(123,9) from dual;--结果为:123

           select nvl(null,9) from dual;--结果为:9

           select nvl(null,'a') from dual;--结果为:a

NVL()函数相当于if....else...语句,if value=null then value=9 else value=value fi;

2、NVL2()

格式:NVL2(value,string1,string2)

NVL()的扩展函数,如果value为空,则返回string2,如果不为空,则返回string1

用法:select * from testnumber;

          select nvl2(nvltest,'非null','null') from testnumber where listid=2.00;

3、Decode()

格式:decode(column,if1,value1,if2,value2.....ifn,valuen) 

           decode(expressio,if1,value1,if2,value2.....ifn,valuen,elsevalue)

如果decode的传入的值为字段,则当字段值为if1时,返回value1,以此类推;

如果decode的传入的是表达式,则当表达式值为if1时,返回value1,以此类推,如果表达式值不在其中,则返回elsevalue;

用法:(1)select * from testnumber;

(2)select username,
       decode(username, 'cleaner', '清洁员', 'teacher', '教师','farmer','农民') decode值
  from testnumber;

(3)select username,
       decode(username, 'cleaner', '清洁员', 'teacher', '教师','农民') decode值
  from testnumber;

(4)select username,
       decode(username, 'cleaner', '清洁员', 'teacher', '教师') decode值
  from testnumber;


 

猜你喜欢

转载自blog.csdn.net/haidibeike/article/details/9000345