dbTalk Databases Forums  

Format Number on Select

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Format Number on Select in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Erich93063
 
Posts: n/a

Default Format Number on Select - 12-26-2007 , 01:30 PM






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

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Format Number on Select - 12-26-2007 , 02:02 PM






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


Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Format Number on Select - 12-26-2007 , 05:00 PM



Erich93063 (erich93063 (AT) gmail (DOT) com) writes:
Quote:
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 (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #4  
Old   
Madhivanan
 
Posts: n/a

Default Re: Format Number on Select - 12-27-2007 , 07:31 AM



On Dec 27, 12:30*am, Erich93063 <erich93... (AT) gmail (DOT) com> wrote:
Quote:
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


Reply With Quote
  #5  
Old   
Knowledgy
 
Posts: n/a

Default Re: Format Number on Select - 01-09-2008 , 02:16 PM



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 (DOT) com> wrote

Quote:
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



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.