0

This:

SELECT 'R$ '+convert(varchar(50), CAST(998547 as money), -1) MONEYVALUE

Will output: R$ 998,547.00

In Brazil the right is: R$ 998.547,00

How can I output the right format?

5
  • 9
    don't have the db do the formatting. have your db output 998547.00, then do the formatting in your client-side code. the db should never have to worry about who's sitting at the keyboard and what their national format is.
    – Marc B
    Feb 23, 2015 at 18:04
  • Or if you absolutely must, you can write your own UDF that will use SQL string functions to convert the money datatype to the desired string format. Feb 23, 2015 at 18:08
  • @Marc B, Ok you got me.
    – Khrys
    Feb 23, 2015 at 18:12
  • @Khrys - Though Marc B, makes a good point, I believe the correct answer to the question you asked was posted below by Andrew and probably should be marked as so.
    – dcary
    Feb 23, 2015 at 20:30
  • 1
    @dcary, not because FORMAT isn't an option in sql-server-2008-r2
    – Khrys
    Feb 25, 2015 at 12:00

1 Answer 1

7

You need to use the currency format using a selected culture by parameter for SQL Server 2012.

Like this:

select
    'R$ ' + CONVERT(varchar(50), cast(MONEYVAL as MONEY), -1) MONEYVALUE -- Custom Format
    , FORMAT(MONEYVAL, 'C', 'en-us') USA
    , FORMAT(MONEYVAL, 'C', 'pt-br') Brazil
from ACCOUNT;

Look for currency format at the official site

You can see the example at the SQL Fiddle

For Sever 2008 R2 dosen't have a native function to format currency, but you could download some library like this or write your own format function or simple just use replace to make it easy but not so fency.

SELECT 
    'R$ ' + convert(varchar(50), CAST(moneyVal as money), -1) MONEYVALUE,
    'RS ' + REPLACE(REPLACE(REPLACE(CONVERT(varchar(50), (CAST(moneyVal AS money)), -1), ',', ';'), '.', ','), ';', '.')
from Account
1
  • FORMAT isn't avaiable in sql-server-2008-r2
    – Khrys
    Feb 25, 2015 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.