Solved

Format a SELECT column with $, comma right align

Posted on 2009-05-06
10
1,018 Views
Last Modified: 2012-05-06
We are trying to format a select to show the result in Money, comma 2 decimal and right align.

select Department, Sales from Region

sales column is defined as Money

Department      Sales
-------------- -------------
San Fran          6559.36
New York        256314.1
Jersey             501.06

We want this result:

Department      Sales
-------------- -------------
San Fran         $65,59.36
New York    $256,314.10
Jersey                 $501.06

We are testing with varios methos including Cast() but no succes.

0
Comment
Question by:rayluvs
  • 5
  • 3
  • 2
10 Comments
 
LVL 15

Accepted Solution

by:
mohan_sekar earned 300 total points
ID: 24321525
SELECT Department, '$' + CONVERT(varchar(12), Sales, 1) 'Sales' FROM myTable
0
 

Author Comment

by:rayluvs
ID: 24322105
Excellent!

Can it be right align...
0
 

Assisted Solution

by:Debasis126
Debasis126 earned 200 total points
ID: 24322731
  Can you please try this?

100 is the width you assign to the column. Replicate would repeat blank space the number of times specified before the start of 'sales' values. in this case it would repeat blank space 100 - LEN(Sales) times.

SELECT Department, REPLICATE(' ', 100 - LEN(Sales)) + '$' + CONVERT(varchar(12), Sales, 1) 'Sales' FROM myTable
0
 

Author Comment

by:rayluvs
ID: 24324001
Gave me an error:

Server: Msg 257, Level 16, State 3, Line 1
Implicit conversion from data type money to varchar is not allowed. Use the CONVERT function to run this query.

FYI: I'musing SQL 2000
0
 
LVL 15

Assisted Solution

by:mohan_sekar
mohan_sekar earned 300 total points
ID: 24324257
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:rayluvs
ID: 24324694
Read the link, very helpful, thanx.

The data column are money in my table and it gives an error.  Almost all I have read in EE are if values are CHAR/VARCHAR, my values are money.

Whats the command or function that will format money values to $x,xxx.xx?
0
 

Author Comment

by:rayluvs
ID: 24324710
Also, going by the same line, since my values are Money, LEN also doesn't work.

So maybe I have to convert the value of Money to varchar?

How can we use LEN with money values?
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24324800
Server: Msg 257, Level 16, State 3, Line 1
Implicit conversion from data type money to varchar is not allowed. Use the CONVERT function to run this query

Yes, convert Money to Varchar as the above error indicates that implicit conversion is not allowed
0
 

Assisted Solution

by:Debasis126
Debasis126 earned 200 total points
ID: 24333240
Buddy,

i hope this link below helps:

http://www.sqlusa.com/bestpractices2005/moneyformat/

Please check it.
0
 

Author Comment

by:rayluvs
ID: 24334563
Thanx!  Excellent!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about achieving the basic levels of HRIS security in the workplace.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now