SQL 之 CASE WHEN 修改查询数据,不符合修改条件的部分则保持不变

Q: Region is a mix of abbreviations and full names. Fix up the remaining regions by converting the full names to their corresponding abbreviations (e.g., South Australia -> SA). Can you work out how to do this using a single query?

区域是由缩写和全名混合而成的。通过将全名转换为相应的缩写 (例如,South Australia -> SA) 来修复其余区域。你能想出如何使用一个单一的查询来做到这一点吗?

如果运行以上语句将会出现一个错误,

Cannot insert the value NULL into column 'region', table 'AmazonOrders.dbo.Addresses'; column does not allow nulls. UPDATE fails.
 

这是因为先前建表时设置 region 的内容不能为空

注意

CASE WHEN THEN ELSE END

可以不写else但是一定要有end ,没有else如果不满足条件会以null填充

解决方法: 

为使其余值保持不变,加入 ELSE column 语句

本例中为 ELSE region:

查看修改结果:

SELECT column FROM Table

​​

 

​​

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值