coalesce 函数的使用方法,

本文通过一个具体的SQL查询案例展示了如何使用COALESCE函数和CASE WHEN语句来选择非空的电话号码列。当第一列为空时,尝试使用第二列,若第二列亦为空,则使用第三列。此外,还介绍了如何使用REPLACE函数去除空字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


需求:表中有三列: mobile1,mobile2,mobile3 ,三列电话只,如果mobile1为空,使用mobile2,如果mobile2 也为空,使用mobile3,

select coalesce(mobile1,replace(mobile2,' ',''),mobile3) mobile ,mobile1,mobile2,mobile3 from ods_customer_idv_dx t



在mobile2中使用replace 就是预付此列中有空格,所以先过滤掉.

 这个coalesce函数其实相当于  

 select case when mobile1 is not  null then mobile1 else 
             case when mobile2 is not null then mobile2
             else mobile3 end 
             end mobile,mobile1,mobile2,mobile3 from ods_customer_idv_dx t


 

 

结果:

 

   	MOBILE	MOBILE1	MOBILE2	MOBILE3
1	18060198050	18060198050		
2	13906735859	13906735859		
3	13930615515	13930615515		
4	13806989550	13806989550		
5	13563437579	13563437579		
6	13561465566	13561465566		
7	13629942078	13629942078		
8	13897880287	13897880287		
9	13873832566	13873832566		
10	13997779138		13997779138	
11	15873009090	15873009090		
12	15092059251	15092059251		
13	13961216690	13961216690		
14	13806663263	13806663263		
15	13587942738		 	13587942738
16	13933311898	13933311898		
17	13906737622	13906737622		
18	13518383255	13518383255		
19	13673124759	13673124759		
20	15532375676	15532375676		
21	13176625599			13176625599
22	057164254726	057164254726		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值