简体   繁体   中英

Replace a NULL value to Not Set if another column is NULL

I'm making a parameter of salespeople and I need a NULL value in the SalesID column but it needs to say Not Set in the Display name column. There are NULL values in the Display but they have SalesID that I want to use instead.

SELECT SalesID, 
ISNULL(DisplayName, SalesID) AS DisplayName
FROM SalesTable

use case when and COALESCE()

SELECT SalesID, 
case when DisplayName is null and  SalesID is null then 'Not Set' 
 else COALESCE(DisplayName,SalesID) end AS DisplayName
FROM SalesTable

Considering you are using SQL SERVER, You can extend COALESCE function -

SELECT SalesID, 
COALESCE(DisplayName, SalesID, 'Not Set') AS DisplayName
FROM SalesTable

Is this what you want?

SELECT SalesID, 
       COALESCE(DisplayName, 'Not Set') AS DisplayName
FROM SalesTable
SELECT SalesID, 
CASE WHEN (DisplayName IS NULL AND  SalesID IS NULL) THEN 'Not Set' ELSE DisplayName END AS DisplayName
FROM SalesTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM