Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table with following data in the table.
cola,colb,colc
1, NULL, NULL
NULL, 1, NULL
NULL, NULL, 1

I need output like below

1,1,1

What I have tried:

select coalesce(colun)

I would appreciate if any one helps me to get desired output. which function would help on this.
Posted
Updated 4-Jul-17 0:46am
Comments
Kornfeld Eliyahu Peter 4-Jul-17 6:49am    
And what if your data is
NULL, NULL, NULL
NULL, 1 , NULL
NULL, NULL, NULL

???
Member 11337367 4-Jul-17 6:57am    
It should work for all type os scenarios
like any column or having non null values shoud come from queries
Kornfeld Eliyahu Peter 4-Jul-17 7:52am    
But according to your sample if a column has null values in every row it should not show at all in the result table!
Member 11337367 4-Jul-17 7:56am    
yesh that's what I wanted, only non null values from all columns in a table

1 solution

One way to do it:
SQL
SELECT a.cola, b.colb, c.colc FROM MyTable a
JOIN MyTable b on b.colb IS NOT NULL
JOIN MyTable c on c.colc IS NOT NULL
WHERE a.cola IS NOT NULL

How well that scales to your actual application depends on your real table design and data, and we have no idea what that looks like.
 
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