oracle decode函数使用方法

2025-04-29 10:54:00

1、用法一: 使用decode()判断字符串是否一样decode(value, if 条件1,then 值1,if 条件2,then 值2,...,else 其他值)Sql测试:select aac001,decode(aac001,'0000000001','张三','0000000002','李四','others') as name from ac01 where rownum<=1;输出结果:0000000002,李四

2、用法二: 使用decode()比较大小Select decode(sign(var1-var2),1荑樊综鲶,var1,var2) from dual;(注释: sign()函数根据某个值是0、正数、负数,分别返回0、1、-1)Sql测试:Select decode(sign(100-90),1,100,90) from dual;输出结果 :100解释: 100-90=10>0则sign()函数返回1,decode()函数取值var1为100Sql测试:Select decode(sign(100-90),-1,100,90) from dual;输出结果:90100-90=10>0则sign()返回1,decode()函数取值var2为90

3、用法三: 使用decode()函数分段假设工资大于10000为高薪,工资介于5000到10000为中等,工资小于5000位低薪Sql测试:Select ename,sal,decode(sign(sal-10000),1,'高薪',0,'高薪',-1,decode(sign(sal-5000),1'中等',0,'中等',-1,'低薪'))) from ac01;输出结果:李明 12000 高薪张三 5000 中等王五 3000 低薪

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢