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
 case question

Author  Topic 

stumbling
Posting Yak Master

104 Posts

Posted - 2007-04-16 : 07:21:06
Hi All
I have a column in a table of type float it contains supplied qty's.
I would like to run the following statement thru a case statment when the value is < 0

SELECT '-' + right(replace(replicate('0', 10) + (cast(qtysupplied as varchar)), '-', ''),10) FROM table_name

Having not used case statements before in any detail i have gotten this far but as someone out there can obviously see i am lost. Any help would be appreciated

select CASE
When qtysupplied > = 0
Then qtysupplied
ELSE (SELECT '-' + right(replace(replicate('0', 10) + (cast(qtysupplied as varchar)), '-', ''),10) FROM table_name)
END
from table_name

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-04-16 : 07:29:47
quote:
Originally posted by stumbling

Hi All
I have a column in a table of type float it contains supplied qty's.
I would like to run the following statement thru a case statment when the value is < 0

SELECT '-' + right(replace(replicate('0', 10) + (cast(qtysupplied as varchar)), '-', ''),10) FROM table_name

Having not used case statements before in any detail i have gotten this far but as someone out there can obviously see i am lost. Any help would be appreciated

select CASE
When qtysupplied > = 0
Then qtysupplied
ELSE (SELECT '-' + right(replace(replicate('0', 10) + (cast(qtysupplied as varchar)), '-', ''),10) FROM table_name)
END
from table_name





select 
CASE
When qtysupplied > = 0 Then cast(qtysupplied as varchar)
ELSE '-' + right(replace(replicate('0', 10) + (cast(qtysupplied as varchar)), '-', ''),10)
END as qtysupplied
from table_name



Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

stumbling
Posting Yak Master

104 Posts

Posted - 2007-04-16 : 07:43:35
Thank you very much for this, i can now see where i was going wrong. Very much appreciated.
Cheers.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-04-16 : 09:43:09
If you want to show this formatted data at front end, you can use format function there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -