sql 實現(xiàn)將空白值替換為其他值
發(fā)布日期:2022-01-01 17:52 | 文章來源:源碼中國
下圖中數(shù)據(jù)庫中查詢到的值有空值,包括空白值(“”)和null
如何將上圖中的null和空白值替換為其他的值呢??
有人建議使用isnull()函數(shù),但是該函數(shù)只能替換null無法替換空白的值。
可以使用下面的sql 語句對null和空白值都替換為其他的值。
select (CASE when (TelPhone IS NULL OR TelPhone='') then '暫無' else TelPhone end) as TelPhone,(CASE when (Name is null or Name='') then '暫無' else Name end) as name,(CASE when (CreateDate IS NULL OR CreateDate='') then '暫無' else CreateDate end) as CreateDate,(CASE when ([Address] IS NULL OR [Address]='') then '暫無' else [Address] end) as [Address] from User_Detail
執(zhí)行sql語句后效果如下:
上圖中我們可以看到所有的null和空白值都替換為了“暫無”。
補充:SQL查詢時替換空值
目前我所知道的有三種方法:
1. 使用if語句
select if(age is null,18,age) from student
2. 使用函數(shù):
2.1 isnull
SELECT isnull(age,18) from Student
2.2 coalesce
select coalesce(age,18) from student
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持本站。如有錯誤或未考慮完全的地方,望不吝賜教。
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章