Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Changing column heading

Author  Topic 

misterjp
Starting Member

4 Posts

Posted - 2010-12-03 : 00:28:41
Hello,

I am trying to find a way to change a column heading dependant on the single result returned.

For example:

If AvailFunds >= 0, I want the column heading to display 'Account Surplus'
but if the AvailFunds < 0, I want the column heading to display 'Account Shortfall'

Results should look like this if the AvailFunds >= 0

Example #1
Client |AccountSurplus
123456 |546213.32

Example #2
Client |AccountShortfall
321654 |-1224.57

Any help would be greatly appreciated

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-12-03 : 02:05:12
you will always have 1 row return in your result ?



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-12-03 : 02:10:55
[code]
SELECT Client
, CASE WHEN AvailFunds >= 0 THEN AvailFunds ELSE NULL END AS AccountSurplus
, CASE WHEN AvailFunds < 0 THEN AvailFunds ELSE NULL END AS AccountShortfall
FROM YourTable
[/code]

How can you have two column heading for one column in one resultset ?

Vaibhav T

If I cant go back, I want to go fast...
Go to Top of Page

misterjp
Starting Member

4 Posts

Posted - 2010-12-05 : 16:33:07
KH: Yes always and only 1 result returned
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-12-05 : 16:45:24
change the heading in the client.
you could do
select ....
into #a

if (select AvailFunds from #a >= 0
select Client, AccountSurplus from #a
else
select Client, AccountShortfall from #a

but I suspect that would cause problems in the client due to the different resultset structures.
maybe

select Client, Amt, type = case when AMt >= 0 then 'AccountSurplus' else 'AccountShortfall' end
from
(
select Client, Amt
from ...
) a
from #a


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -