Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Format a SELECT column with $, comma right align

Posted on 2009-05-06
10
Medium Priority
?
1,025 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 1200 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 800 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 1200 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 800 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

916 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