case when和decode的使用和区别

case when用法:

case when 条件表达式1 then 返回值1 when 条件表达式2 then 返回值2 else 缺省值 end)

例如:

/*用法1*/
select case when sex = 0 then '男' else '女' end as 性别 from table_name;
/*用法2*/
select case when sex = 0 then '男' 
	 when  sex = 1 then '女' else '' end as 性别 from table_name;

decode用法:

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

例如:

select decode(sex,0,'男',1,'女','') as 性别 from table_name

区别:

  • decode只能用在oracle、达梦数据库有一定局限性,而case when比较通用;
  • decode表达式只能是等式判断,而case when可以用于=,>=,<,<=,<>,is null,is not null 等的判断;
  • decode格式使用起来比较简洁,case when格式复杂但更为灵活;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值