January 16, 2013 at 9:48 am
Hi,
I have a variable @amt = 5000 or @amt = -5000. I use the following code to conver to money. However, i am not able to get a negetive symbol '-' to show a negetive amount.
Select '$ + Convert (Varchar, CAST(Amount as Money), -1)
Result:
$5,000
$-5,000 (this should be -$5,000).
How do i do this?
January 16, 2013 at 10:29 am
Maybe this?
DECLARE @amt VARCHAR(10) = '-5000';
SELECT CASE WHEN LEFT(@amt, 1) = '-' THEN '-'
ELSE ''
END + '$' + CAST(ABS(CAST(@amt AS MONEY)) AS VARCHAR(100));
GO
DECLARE @amt VARCHAR(10) = '5000';
SELECT CASE WHEN LEFT(@amt, 1) = '-' THEN '-'
ELSE ''
END + '$' + CAST(ABS(CAST(@amt AS MONEY)) AS VARCHAR(100));
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
January 16, 2013 at 10:30 am
But, really, that kind of formatting should be done in the presentation layer, not in a query. Should be formatted by the report/web page/application.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
January 16, 2013 at 10:37 am
DECLARE @amt VARCHAR(20) = '5000'
SELECT REPLACE('$' + CONVERT(VARCHAR(20),(CAST(@amt AS MONEY))),'$-','-$')
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 16, 2013 at 6:36 pm
The Replace() version is better than the Case version. But you should still seriously consider moving that kind of thing to the presentation layer of the application.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
January 16, 2013 at 8:28 pm
GSquared (1/16/2013)
The Replace() version is better than the Case version. But you should still seriously consider moving that kind of thing to the presentation layer of the application.
I agree this should be a presentation thing and not so much done via tsql.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 18, 2013 at 7:52 am
I agree things like this are best left to a presentation layer but the appetite for features like this is obviously present as Microsoft added FORMAT() to T-SQL in SQL 2012 for just this kind of thing:
http://msdn.microsoft.com/en-us/library/hh213505.aspx
USE AdventureWorks2012;
GO
SELECT TOP (5)
EndOfDayRate,
FORMAT(EndOfDayRate, 'C', 'en-us') AS 'Currency Format'
FROM Sales.CurrencyRate
ORDER BY CurrencyRateID;
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply