tek07
asked on
Convert currency to varchar (thousands separator without decimals)
Im trying to convert one of the lines in my select statement, this code works but I'm trying to format it in a different way. Can anyone help?
CONVERT(varchar(20), acct.Min_Amt) + ' to ' + CONVERT(varchar(20), acct.Max_Amt) AS Cash_Range ---> The output is " $234,992.00 to $532,248.00 "
I want the output to be displayed as "$234,992 to $532,249" <-- without decimals just whole numbers separated by commas.
Thanks !!!
CONVERT(varchar(20), acct.Min_Amt) + ' to ' + CONVERT(varchar(20), acct.Max_Amt) AS Cash_Range ---> The output is " $234,992.00 to $532,248.00 "
I want the output to be displayed as "$234,992 to $532,249" <-- without decimals just whole numbers separated by commas.
Thanks !!!
ASKER
it's not working
ASKER
it still gives me the output with .00 at the end .
SELECT '$' + REPLACE(CONVERT(VARCHAR(20 ), CAST(ROUND(acct.Min_Amt, 0) AS MONEY), 1), '.00', '') + ' to $' + REPLACE(CONVERT(VARCHAR(20 ), CAST(ROUND(acct.Max_Amt, 0) AS MONEY), 1), '.00', '') AS [Cash_Range]
For example:
declare @amt decimal(9, 2)
declare @amt2 decimal(9, 2)
set @amt = 234992.45
set @amt2 = 532248.79
SELECT '$' + REPLACE(CONVERT(VARCHAR(20 ), CAST(ROUND(@Amt, 0) AS MONEY), 1), '.00', '') +' to $' + REPLACE(CONVERT(VARCHAR(20 ), CAST(ROUND(@Amt2, 0) AS MONEY), 1), '.00', '')
[That was much harder than you think it should be!]
For example:
declare @amt decimal(9, 2)
declare @amt2 decimal(9, 2)
set @amt = 234992.45
set @amt2 = 532248.79
SELECT '$' + REPLACE(CONVERT(VARCHAR(20
[That was much harder than you think it should be!]
SELECT
CONVERT(varchar(20),(CAST (ROUND(4.25,0) as int))) +
' to ' + CONVERT(varchar(20),(CAST (ROUND(25.51,0) AS INT))) AS Cash_Range
CONVERT(varchar(20),(CAST (ROUND(4.25,0) as int))) +
' to ' + CONVERT(varchar(20),(CAST (ROUND(25.51,0) AS INT))) AS Cash_Range
sorry, just change the 4.25 to the min and 25.51 to the max, I was testing... And you will need to add your dollar signs through concatenation.
Nevermind me.. I didn't fully read. Most be a symptom of the time and the fact that I am still working on a project. I won't give you the commas with my method.. I also see that Scott answered as I was typing my two posts, his looks like what you are after.
If you wanna remove '.00' you can use this
CONVERT(varchar(20), acct.Min_Amt) + ' to ' + REPLACE(CONVERT(varchar(20 ), acct.Max_Amt) AS Cash_Range,'.00','')
CONVERT(varchar(20), acct.Min_Amt) + ' to ' + REPLACE(CONVERT(varchar(20
Btw, if you prefer, you can use FLOOR() or CEILING() in place of ROUND(), depending on exactly what you want to see for non-zero cents amount.
SELECT CONVERT(varchar(20), '$234,992.00') + ' to ' + LEFT(CONVERT(varchar(20), '$234,992.00'),CHARINDEX(' .','$234,9 92.00')-1) AS Cash_Range
ASKER
im so confused, lol. This si the statement.
SELECT
'$' + CONVERT(varchar(20), ROUND(b.Min_Amt, 0)) + ' to ' + '$' + CONVERT(varchar(20), ROUND(b.Max_Amt, 0)) AS Range_amt
from
bank b
the output is " $100000.00 to $200000.00 " and I need it to have no commas and no decimals so it'll look like this "$100,000 to $200,000".
I dont want to create variables or anything just a simple convert statement.
Appreciate everyone's input. Thanks
SELECT
'$' + CONVERT(varchar(20), ROUND(b.Min_Amt, 0)) + ' to ' + '$' + CONVERT(varchar(20), ROUND(b.Max_Amt, 0)) AS Range_amt
from
bank b
the output is " $100000.00 to $200000.00 " and I need it to have no commas and no decimals so it'll look like this "$100,000 to $200,000".
I dont want to create variables or anything just a simple convert statement.
Appreciate everyone's input. Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I would go with Scott's the variables were just for illustration. His makes the most sense and definitely works. Aneeshatingal's first one took some of what scott already supplued anyway. Personally I would use what scott put in the first part of his answer should do you fine.
ASKER
thanks it works! can u explain the whole statement scottpletcher? whats with the replace and AS Money? then covert and cast at the same time?
Sorry for the delay, I worked all week-end on SQL recoveries so was out yesterday.
The CAST(... AS MONEY) is so that SQL will "understand" that the third parameter tells it to insert commas; if the datatype isn't MONEY, SQL doesn't recognize the third parameter correctly.
However, the only format that includes comma *also* includes decimal places. The REPLACE() gets rid of them, by forcing the cents amount to 00, and then replacing the cents in the amount, ".00", with an empty string, "", thus getting rid of the cents.
The CAST(... AS MONEY) is so that SQL will "understand" that the third parameter tells it to insert commas; if the datatype isn't MONEY, SQL doesn't recognize the third parameter correctly.
However, the only format that includes comma *also* includes decimal places. The REPLACE() gets rid of them, by forcing the cents amount to 00, and then replacing the cents in the amount, ".00", with an empty string, "", thus getting rid of the cents.
CONVERT(varchar(20), ROUND(acct.Min_Amt, 0)) + ' to ' + CONVERT(varchar(20), ROUND(acct.Max_Amt, 0)) AS Cash_Range
Regards,
Patrick