decode函数的使用
select username,decode(username,'aaa','计算机部门','bbb','市场部门','其他') as 部门
from users
decode函数是case when的一种简化
SQL> select username,decode(username,'aaa','计算机部门','bbb','市场部门','其他')
as 部门 from users;
用户名 部门
-------------------- ----------
aaa 计算机部门
bbb 市场部门
ccc 其他
cccccc 其他
aaa 计算机部门
DECODE
--decode 函数的使用,对字段的值,进行条件判断
decode (字段名,值,结果,值,结果 ,默认值) --也可不指定默认值,返回null
--示例
select username ,DECODE(username,'aaa','计算机部门','bbb','市场部门' ,'其他部门') AS 部门
from xcxuser;
Decode函数的使用:
decode与case...when类似
decode(字段名,值,结果,....,默认值)
decode(col_name,value1,result1,value2,result2,...,[defaulvalue]) ;
例如: select username,decode(username,'aaa','计算机部门','bbb','市场部门','其他') as 部门 from users;
decode函数的使用
decode(column_name,value1,result1,...,defaultvalue)
select username,decode(username,'aaa','计算机部门','bbb','市场部门','其他') as 部门 from users;
select username,decode(username,'aaa','计算机部门','bbb','市场部门','其他') as 部门 from users;
decode函数的使用
作用:对字段中的值进行一个条件的判断。
语法:如图,它和case...when语句相似,参数1是字段名,参数2是参数值,参数3是显示结果,参数四是默认值,也可以不指定默认值,这时如果都不满足条件,返回null。
举例:
decode函数的使用
作用:对字段中的值进行一个条件的判断。
语法:如图,它和case...when语句相似,参数1是字段名,参数2是参数值,参数3是显示结果,参数四是默认值,也可以不指定默认值,这时如果都不满足条件,返回null。
举例:
关于case when和decode
select username ,decode (username ,'aaa','计算机部门', 'bbb','市场部门','其他部门')as 部门 from users;
case...when
select username,case username when 'aaa' then '计算机部门',‘bbb’ then ‘市场部门’ else '其他部门' end as 部门
from users;
select username ,case when uasername='aaa' then '计算机部门' ,username='bbb' then‘市场部门’,‘其他部门’end as 部门
from users;
decode
select username ,decode (username,'aaa',‘计算机部门’,‘bbb’,‘市场部门’,‘其他部门’) as 部门
from users;
distinct可以去除多余的行
decode 函数
decode (column_name, value1, result1, value2, result2, ... , defaultvalue)
decode函数
作用和case...when...then一样
case...when
select username,case username when 'aaa' then '计算机部门',‘bbb’ then ‘市场部门’ else '其他部门' end as 部门 from users;
select username ,case when uasername='aaa' then '计算机部门' ,username='bbb' then‘市场部门’,‘其他部门’end as 部门 from users;
decode
select username ,decode (username,'aaa',‘计算机部门’,‘bbb’,‘市场部门’,‘其他部门’) as 部门 from users;
decode实例
decode函数:
decode (column_name,value,result,...,defaultresult);
类似case..when...then..语句即可以根据字段不同值设置不同结果