• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1838
  • Last Modified:

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 !!!
0
tek07
Asked:
tek07
  • 4
  • 4
  • 4
  • +2
1 Solution
 
Patrick MatthewsCommented:
Hi tek07,

CONVERT(varchar(20), ROUND(acct.Min_Amt, 0))  + ' to ' + CONVERT(varchar(20), ROUND(acct.Max_Amt, 0)) AS Cash_Range

Regards,

Patrick
0
 
tek07Author Commented:
it's not working
0
 
tek07Author Commented:
it still gives me the output with .00 at the end .
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Scott PletcherSenior DBACommented:
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!]
0
 
MikeWalshCommented:
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
0
 
MikeWalshCommented:
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.
0
 
MikeWalshCommented:
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.
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
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','')

0
 
Scott PletcherSenior DBACommented:
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.
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT CONVERT(varchar(20), '$234,992.00')  + ' to ' + LEFT(CONVERT(varchar(20), '$234,992.00'),CHARINDEX('.','$234,992.00')-1) AS Cash_Range  
0
 
tek07Author Commented:
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
0
 
Scott PletcherSenior DBACommented:
I think my version gave what you req'd in the original q:

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]
0
 
MikeWalshCommented:
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.
0
 
tek07Author Commented:
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?
0
 
Scott PletcherSenior DBACommented:
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now