Home
> Uncategorized > Format prices correctly in #MSSQL using currencies in #ISO4127 format.
Format prices correctly in #MSSQL using currencies in #ISO4127 format.
If you want to specify a price including a currency, you can always write EUR 1,234 or USD 1,234, but it’s more concise to write $1,234 or €1,234, and users expect that format. You can always handle this at application layer, but here’s how to do it at Database layer.
Here, I’ve created a table of the currencies I needed, – it’s not complete, and I hope that someone can post a link to a complete table.
create table CurrencyLocales
(
id int identity(1,1),
currency varchar(3),
locale varchar(5)
)
insert into CurrencyLocales (currency,locale) values ('AUD','en-AU')
insert into CurrencyLocales (currency,locale) values ('BRL','pt-BR')
insert into CurrencyLocales (currency,locale) values ('CAD','en-CA')
insert into CurrencyLocales (currency,locale) values ('CZK','cs-CZ')
insert into CurrencyLocales (currency,locale) values ('DKK','da-DK')
insert into CurrencyLocales (currency,locale) values ('EUR','en-IE')
insert into CurrencyLocales (currency,locale) values ('GBP','en-GB')
insert into CurrencyLocales (currency,locale) values ('HUF','hu-HU')
insert into CurrencyLocales (currency,locale) values ('MXN','es-MX')
insert into CurrencyLocales (currency,locale) values ('MYR','en-MY')
insert into CurrencyLocales (currency,locale) values ('NOK','nb-NO')
insert into CurrencyLocales (currency,locale) values ('NZD','en-NZ')
insert into CurrencyLocales (currency,locale) values ('RUB','ru-RU')
insert into CurrencyLocales (currency,locale) values ('SEK','sv-SE')
insert into CurrencyLocales (currency,locale) values ('SGD','en-SG')
insert into CurrencyLocales (currency,locale) values ('USD','en-US')
create index idxCurrency on CurrencyLocales(currency)
-- example
select *, FORMAT(1234, 'C', locale) as formattedPrice from CurrencyLocales
id | Currency | Locale | Formatted |
1 | AUD | en-AU | $1,234.00 |
2 | BRL | pt-BR | R$1.234,00 |
3 | CAD | en-CA | $1,234.00 |
4 | CZK | cs-CZ | 1 234,00 Kč |
5 | DKK | da-DK | 1.234,00 kr. |
6 | EUR | en-IE | €1,234.00 |
7 | GBP | en-GB | £1,234.00 |
8 | HUF | hu-HU | 1 234,00 Ft |
9 | MXN | es-MX | $1,234.00 |
10 | MYR | en-MY | RM1,234.00 |
11 | NOK | nb-NO | kr 1 234,00 |
12 | NZD | en-NZ | $1,234.00 |
13 | RUB | ru-RU | 1 234,00 ₽ |
14 | SEK | sv-SE | 1.234,00 kr |
15 | SGD | en-SG | $1,234.00 |
16 | USD | en-US | $1,234.00 |
Categories: Uncategorized
Comments (0)
Trackbacks (0)
Leave a comment
Trackback