Solved

Format a SELECT column with $, comma right align

Posted on 2009-05-06
10
1,021 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 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