Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have table like below

name a b c
gopi NULL 6 5
Ram NULL 5 NULL
Balaji NULL NULL 3

And i need output like below

name asd
gopi 6
Ram 5
Balaji 3

I have used cross apply but i didn't exact output. can anyone help?

What I have tried:

text
Posted
Updated 13-Nov-17 4:28am
v2
Comments
ZurdoDev 10-Feb-15 9:59am    
Explain in English what you are trying to get. It looks like all you want is this:

SELECT name, COALESCE(a,b,c)
FROM table

which is very simple.
OriginalGriff 10-Feb-15 10:20am    
Suggest as solution?
ZurdoDev 10-Feb-15 10:23am    
Yes, was waiting on response to make sure I understood.

Thanks.
Gopijack89 10-Feb-15 10:07am    
Really thank you very much.

1 solution

You can use the COALESCE() function. It will return the first non null value in the list of values you pass in.

For example:
SQL
SELECT name, COALESCE(a,b,c) AS someField
FROM table


will return a if it is not null and if a is null it will return b if not null, etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900