下圖中數據庫中查詢到的值有空值,包括空白值(“”)和null
如何將上圖中的null和空白值替換為其他的值呢??
有人建議使用isnull()函數,但是該函數只能替換null無法替換空白的值。
可以使用下面的sql 語句對null和空白值都替換為其他的值。
1
|
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 |
執行sql語句后效果如下:
上圖中我們可以看到所有的null和空白值都替換為了“暫無”。
補充:SQL查詢時替換空值
目前我所知道的有三種方法:
1. 使用if語句
1
|
select if(age is null ,18,age) from student |
2. 使用函數:
2.1 isnull
1
|
SELECT isnull (age,18) from Student |
2.2 coalesce
1
|
select coalesce (age,18) from student |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/qq_16990363/article/details/49250079