Solved

Format a SELECT column with $, comma right align

Posted on 2009-05-06
10
1,020 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

820 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