Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
My table structure is
Particulars    col1          col2
aaa             10           null
bb              20           null
ccc            null           70
dd              50           null
ee             null           20
ff             null           60

one of the column from col1 and col2 can contain value, another remain null.
Now I want an out put like this
Particulars    Amount
aaa             10  
bb              20  
ccc             70
dd              50
ee              20
ff              60

What will be the query?
Please help.
Posted
Comments
ridoy 19-Aug-12 16:41pm    
Do you try anything or where is your code?

1 solution

Coalesce lets you specify a second column to get a value from, if the first is null. Your data format looks a bit stupid, but it's easy to write the SQL to select it.

select particulars, coalesce(column1, column2) as myval from mytable

I know from experience that you have paying customers, yet you are asking the most basic questions and refuse to do any research or try to do your work. You really should stop taking people's money, it's basically fraud.
 
Share this answer
 
Comments
[no name] 19-Aug-12 16:23pm    
and the code is returning value like

Collapse | Copy Code

Particulars Amount
aaa 10
bb 20
ccc 0
dd 50
ee 0
ff 0
Christian Graus 19-Aug-12 16:23pm    
I help because I like it. Even when people are rude to me :-)
Sergey Alexandrovich Kryukov 19-Aug-12 23:29pm    
Rudeness is unpleasant, but the helping is hard to stop, right? :-)
A 5.
--SA
Christian Graus 19-Aug-12 16:24pm    
http://msdn.microsoft.com/en-us/library/ms190349.aspx - the documentation says that coalesce returns the first non null values, and it should. If col1 is 0, it will return 0. If it is null, it will return the value in col2.
[no name] 19-Aug-12 16:36pm    
Done success.

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