Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to select first non Null value from multiple fields Expand / Collapse
Author
Message
Posted Tuesday, April 16, 2013 9:54 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 2:34 PM
Points: 196, Visits: 487
Hi,
what is the best way to select fist non Null value from mutliple fiels K1,.... K3, I heard about case, but still can't figure out how to use single case fro multiple columns?

Appreciate your help.
Mario

select * from T01-- source table::::
PK K1 K2 K3
2 Bravo
3 Charlie
1 Alpha
48 Whiskey

--wanted output:
select PK, case (.....) from T01
PK K
1 Alpha
2 Bravo
3 Charlie
48 Whiskey

Post #1442826
Posted Tuesday, April 16, 2013 10:10 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:10 AM
Points: 13,230, Visits: 12,709
Look at coalesce. That is exactly what it is intended for.

http://msdn.microsoft.com/en-us/library/ms190349.aspx


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1442837
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse