May 2, 2007

How to get the first not null column from a table?

Hey , really a tricky question , select all the values into a single column from a table which is first not null in that table.
For Exapmle
There is table test





idcol1col2col3col4
1NULLNULLWM
2QNULL RNULL
3 NULLWPT
4NULLNULL NULLS


Now I want to get the result like that in a single column
First Not NULL Values
W
Q
W
S

So what you think how can we get that,
First thing is that you can use cursor and loop each row and get them.
The second one is the use of COALESCE to get that

this query will get the result

SELECT COALESCE(col1,col2,colo3,col4) FROM TEST

If you have any query you can ask me at matespoint@gmail.com

No comments:

Post a Comment

SQL - Difference between CAST,CONVERT and PARSE

TODO---