下圖中數(shù)據(jù)庫(kù)中查詢到的值有空值,包括空白值(“”)和null
![](/d/20211018/091000c7a1ff48b7effc87bb95bb7a30.gif)
如何將上圖中的null和空白值替換為其他的值呢??
有人建議使用isnull()函數(shù),但是該函數(shù)只能替換null無(wú)法替換空白的值。
可以使用下面的sql 語(yǔ)句對(duì)null和空白值都替換為其他的值。
select (CASE when (TelPhone IS NULL OR TelPhone='') then '暫無(wú)' else TelPhone end) as TelPhone,(CASE when (Name is null or Name='') then '暫無(wú)' else Name end) as name,(CASE when (CreateDate IS NULL OR CreateDate='') then '暫無(wú)' else CreateDate end) as CreateDate,(CASE when ([Address] IS NULL OR [Address]='') then '暫無(wú)' else [Address] end) as [Address] from User_Detail
執(zhí)行sql語(yǔ)句后效果如下:
![](/d/20211018/7c57c231ade2a0075eeaabda230d2269.gif)
上圖中我們可以看到所有的null和空白值都替換為了“暫無(wú)”。
補(bǔ)充:SQL查詢時(shí)替換空值
目前我所知道的有三種方法:
1. 使用if語(yǔ)句
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
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- postgresql coalesce函數(shù)數(shù)據(jù)轉(zhuǎn)換方式
- postgresql 中的COALESCE()函數(shù)使用小技巧
- postgresql 實(shí)現(xiàn)修改jsonb字段中的某一個(gè)值
- postgresql 實(shí)現(xiàn)將字段為空的值替換為指定值
- PostgreSQL 禁用全表掃描的實(shí)現(xiàn)
- 解決PostgreSQL Array使用中的一些小問(wèn)題
- postgresql 中的 like 查詢優(yōu)化方案