Avatar of TrialUser
TrialUser
Flag for Afghanistan asked on

formatting sql column as currency

1) I would like to return a price column as currency $10.50 format. How can I do it in my select statement:

select itemnmbr, price from item

2) would this be faster than doing it in the html using string.format("c", price) after returning the price as such from the sql table

Thanks

Thanks
Microsoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
Anthony Perkins

8/22/2022 - Mon
sventhan

<  I would like to return a price column as currency $10.50 format. How can I do it in my select statement:

select itemnmbr, price from item

answer

select itemnmbr, cast(price as decimal(10,2)) price from item
sventhan

< would this be faster than doing it in the html using string.format("c", price) after returning the price as such from the sql table
It should be
dj_alik

It depends on the server cpu and browser/client  rendering speed


Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Anthony Perkins

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
prajapati84

>> would this be faster than doing it in the html using string.format("c", price) after returning the price as such from the sql table

In this case, using sql query will be faster than by doing it at html side.

>> I would like to return a price column as currency $10.50 format. How can I do it in my select statement

As acperkins stated, you need to concate the currency symbol with your currency value.
SELECT '$' + CONVERT(varchar(20), @YourColumnName)
Anthony Perkins

>>In this case, using sql query will be faster than by doing it at html side.<<
How can you possibly know this without knowing their code in the front-end?

T-SQL is notoriously slow when doing any type of string manipulation, combine that with the added network traffic generated and I cannot see how that can be true.

But quite aside from the nanosecond you may or may not save, this is a very bad idea and is best relegated to the Presentation layer.