SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 formatting a select of a numeric column as format -9999.9999
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AskSQLTeam
Ask SQLTeam Question

USA
0 Posts

Posted - 08/25/2006 :  10:38:23  Show Profile  Visit AskSQLTeam's Homepage  Reply with Quote
scott writes "I am relatively new to SQL Server, but after a couple of hours searching help, I'm stuck.

I am trying to select a numeric column from a table and format it using sql. Desired format "-9999.9999" with leading sign and zeros. IS_NEGATIVE is a function to determine sign. The code below works, but seems inefficient.

Is there a better way?

SELECT
RIGHT(dbo.IS_NEGATIVE(ISNULL(COST,0)),1) + RIGHT(REPLICATE('0',10) + RIGHT(abs(CONVERT(decimal(9,5),COST)),10),10) AS 'PRICE'
FROM ORDERS"

Srinika
Flowing Fount of Yak Knowledge

Sri Lanka
1378 Posts

Posted - 08/25/2006 :  11:21:43  Show Profile  Reply with Quote
Give some sample data and expected results

Srinika
Go to Top of Page

harsh_athalye
Flowing Fount of Yak Knowledge

India
5509 Posts

Posted - 08/25/2006 :  11:24:56  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
Better place to do this kind of formatting stuff is in the front-end...SQL may not be the best candidate for it !

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 08/25/2006 :  23:44:18  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Where do you want to show data?
As suggested if you use front end application, use format function there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30114 Posts

Posted - 08/26/2006 :  10:48:16  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT CASE WHEN Cost > 0 THEN '+' ELSE '' END + CONVERT(VARCHAR, CAST(Cost AS MONEY), 2)

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000