Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for Multiple Null Values Across Fields

Tags:

sql

sql-server

Trying to check multiple fields for null values in order to create a master field with no null values. In the case below I am attempting to check several "city" fields until I get to a non-null value but this statement is not working as expected.

CASE
WHEN b.BillingCity IS NULL THEN b.ShippingCity
WHEN b.BillingCity IS NULL AND b.ShippingCity IS NULL THEN c.BillingCity
WHEN b.BillingCity IS NULL AND b.ShippingCity IS NULL AND c.BillingCity IS NULL THEN b.ES_APP__ESCity__c
WHEN b.BillingCity IS NULL AND b.ShippingCity IS NULL AND c.BillingCity IS NULL AND b.ES_APP__ESCity__c IS NULL THEN b.Avn_City__c
ELSE Null
END
As MasterCity
like image 285
hansolo Avatar asked Mar 20 '26 10:03

hansolo


1 Answers

You can just use COALESCE:

SELECT COALESCE(b.BillingCity,b.ShippingCity,c.BillingCity,.....)
like image 98
Lamak Avatar answered Mar 23 '26 01:03

Lamak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!