簡體   English   中英

如何使用case語句捕獲NULL值

[英]how to catch NULL values using case statement

在這個查詢中,我想用一些新的值替換Adventureworks數據庫的Person.Contact中的值。 以下查詢case語句適用於其他值,但我無法更改NULL值。 我正在使用SQL Server。 任何幫助表示贊賞。

select contactid,Title,FirstName,MiddleName,
case MiddleName
when 'R.' then 'Robert'
when 'B.' then 'Bids'
when 'J.' then 'John'
when is null then 'New Name'
else 'No Name'
end, LastName from Person.Contact
case 
when MiddleName is null then ...
when MiddleName = 'R' then ...
end

我將使用ISNULL函數 - 如果字段為NULL,它將返回給定的值:

select contactid,Title,FirstName,MiddleName,
case ISNULL(MiddleName, 'NULLVALUE')
when 'R.' then 'Robert'
when 'B.' then 'Bids'
when 'J.' then 'John'
when 'NULLVALUE' then 'New Name'
else 'No Name'
end, LastName from Person.Contact

很抱歉發布7年后,但我一直在努力尋找Interbase / Firebird的解決方案,這篇文章不斷涌現。 這里沒有任何解決方案可行,因為沒有ISNULL,所以我想我會幫助其他任何可能來這里尋找的人:

select contactid,Title,FirstName,MiddleName,
case COALESCE(MiddleName, 'NULLVALUE')
when 'R.' then 'Robert'
when 'B.' then 'Bids'
when 'J.' then 'John'
when 'NULLVALUE' then 'New Name'
else 'No Name'
end, LastName from Person.Contact

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM