ITeye博客

oracle函数decode用法

woainichenxueming · 2010-08-12 21:55 · 数据库 · 数据库 · Markdown

decode用法:

1。decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)

该函数的含义如下:
IF 条件=值1 THEN
    RETURN(返回值1)
ELSIF 条件=值2 THEN
    RETURN(返回值2)
    ......
ELSIF 条件=值n THEN
    RETURN(返回值n)
ELSE
    RETURN(缺省值)
END IF

eg:select decode('&n','1','hello','2','world') from dual;当你n传入参数为1的时候会输出hello,n=2时输出world。

2.decode(表达式a,b,c,d)

  这个的意思是当表达式a的值等于‘b的时候显示‘c’否则显示‘d’

eg:select decode(sum(&a+&b),10,'the sum is 10','the sum is not 10') from dual;当你的输入a+b=10的时候输出'the sum is 10'。否则输出the sum is not 10

decode和case when .... then  else ....end比较

eg:select e.* ,case when e.deptno = 20 then
                           'e.sal >= 1500 '
                           else
                           'e.sal>2500'
                           end as sals from emp e;

decode用的比较多的地方就是取值判断。例如:user表的status字段为0时显示禁用,为1是显示可用。直接用decode函数取值,到前台显示的时候就不用再if esle的判断了。

 

顺便提一下to_date和to_char的区别

to_date(a,'yyyy-mm-dd')是将字符串转换为数据库的日期类型

to_char(a,'yyyy-mm-dd')是将日期或数字转换成字符串

eg:

  使用TO_CHAR函数处理数字
TO_CHAR(number, '格式')
TO_CHAR(salary,’$99,999.99’);
使用TO_CHAR函数处理日期
TO_CHAR(date,’格式’);

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。