Solved

Format a SELECT column with $, comma right align

Posted on 2009-05-06
10
1,017 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 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

15 Experts available now in Live!

Get 1:1 Help Now