Format Number on Select

I have a price field that when I query it, it returns the price as
"5.0000". What function can I use to return the price as "5.00"?

THANKS
erich93063 [ Mi, 26 Dezember 2007 20:30 ] [ ID #1893838 ]

Re: Format Number on Select

Formatting is normally the job of the presentation/reporting layer, where
this is done best.

If the price is numeric then one way is to cast it to the correct decimal
precision (note that rounding will occur):
SELECT CAST(5.0000 AS NUMERIC(10, 2))

If it is character data type, then:
SELECT LEFT('5.0000', LEN('5.0000') - 2),
STUFF('5.0000', LEN('5.0000') - 2, 2, '')

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Plamen Ratchev [ Mi, 26 Dezember 2007 21:02 ] [ ID #1893839 ]

Re: Format Number on Select

Erich93063 (erich93063 [at] gmail.com) writes:
> I have a price field that when I query it, it returns the price as
> "5.0000". What function can I use to return the price as "5.00"?

str(). The syntax is str(val[, width[, scale]]) if memory serves. Look it up
in Books Online for the exact details.


--
Erland Sommarskog, SQL Server MVP, esquel [at] sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx
Erland Sommarskog [ Do, 27 Dezember 2007 00:00 ] [ ID #1893841 ]

Re: Format Number on Select

On Dec 27, 12:30=A0am, Erich93063 <erich93... [at] gmail.com> wrote:
> I have a price field that when I query it, it returns the price as
> "5.0000". What function can I use to return the price as "5.00"?
>
> THANKS

Where do you want to show data?
If you use front end application, use format function there
Madhivanan [ Do, 27 Dezember 2007 14:31 ] [ ID #1894408 ]

Re: Format Number on Select

cast( '5.0000' as decimal(9,2))

--
Sincerely,
John K
Knowledgy Consulting, LLC
knowledgy.org

Atlanta's Business Intelligence and Data Warehouse Experts


"Erich93063" <erich93063 [at] gmail.com> wrote in message
news:5c976abd-e0a0-4f5d-8f02-ac697a516f75 [at] t1g2000pra.googleg roups.com...
>I have a price field that when I query it, it returns the price as
> "5.0000". What function can I use to return the price as "5.00"?
>
> THANKS
Knowledgy [ Mi, 09 Januar 2008 21:16 ] [ ID #1903555 ]
Datenbanken » comp.databases.ms-sqlserver » Format Number on Select

Vorheriges Thema: Selecting SUM and COUNT, but not straightforward..
Nächstes Thema: Query - Select two tables where a.id=b.id