oracle学习之路-decode

oracle decode函数用法:

其实decode函数就像if else一样,以我写的sql举例:select decode(a.dept_code,'010103',2) as keshi from dept_dict a
当dept_code等于010103时显示的值就是2,当然decode还有一些扩展用法,如下:

select dept_name,decode(dept_code,'010103','内科','020103','外科','03','妇产科系统','0301','妇科','other') dept_code from dept_dict 这个sql就相当于if else elseif 当然内部也可以自己加对字符的处理等等,如下:

select dept_name,decode(dept_code,'010103',substr('内科急诊',0,2),'020103',dept_name || ':' || dept_code,'03','妇产科系统','0301','妇科','other') dept_code from dept_dict  像这里面的话用了截取字符函数和拼接字符 还可以使用nvl函数再进行判断 如下:
select dept_code,decode(nvl(dept_name,'空'),'空','骨肿瘤','other') dept_code from dept_dict  decode去判断nvl中的条件,假如dept_name等于null时,nvl函数返回值'空',然后decode函数再去做判断,是'空'的话,则返回'骨肿瘤' 否则返回'other'
 
补充:nvl2函数用法   select nvl2(dept_name,'是空的','不是空的') from dept_dict 可以输入两个参数 类似decode
补充:字段拼接显示 用 || 即可,如果需要两个字段间需要符号分隔,则 || 符号 ||。

猜你喜欢

转载自www.cnblogs.com/ywj20170619/p/9077665.html
今日推荐