Solved

how to convert decimal value in to comma seperated value.

Posted on 2004-10-07
7
619 Views
Last Modified: 2009-07-29
How to write select statment for DECIMAL(28,2) field with "," every thousands place.

For Example

Select QTY from mytable
QTY
----------------------
256110054720.21

Output should be
QTY
-----
256,110,054,720.21

0
Comment
Question by:Bharat Guru
7 Comments
 
LVL 26

Expert Comment

by:Hilaire
ID: 12250026
Presentation issues should be handled on the front-end side.
If you change amounts into strings, they wont sort properly, they won't sum any more , ...
0
 

Author Comment

by:Bharat Guru
ID: 12250071
you are right byt how can i convert in select statment.
somethinkg like this....
select convert(qty,...) from my table
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12250102
if you are looking at the result in Query Analyser, you can have it formatted according to your local settings by going to tools->options->connections and select the "use regional settings" option. Then, make sure your reginal settings (from Control Panel) have comma seperator etc.
If you are passing the results on to an application, you are much better off have the application do the formatting, as otherwise you would have to change the data type from decimal to varchar()  etc.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 26

Expert Comment

by:Hilaire
ID: 12250158
There's no built-in support for this format
instead, most programming languages have formatNumber or formatCurrency functions,
and most reporting tools including excel have standard supprto for htis kind of display format

This could be achieved with a user-defined funciton that would
- cast the number as a string
- find decimal point, if any
- loop every 3 digits left
- build the string in the loop

The function will be ugly and performances will be affected badly

Do you really want this ?
0
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 20 total points
ID: 12250304
I agree that it's better to do it in the front end, but you can do it in a SELECT statement. Use CONVERT with a style argument of 1, and the value being converted has to be a money datatype, so if the original value isn't money, you have to CAST it before the CONVERT function does it's thing:

DECLARE @d as decimal(20,2)
SET @d = 256110054720.21
SELECT Convert(varchar(50), Cast(@d as money) ,1)
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 12250501
>>The function will be ugly and performances will be affected badly<<
Forgive me for this mis-appreciation

jdlambert has a cool way to get the output you need, and I've learnt something new today ;-)
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12250520
My memory's so bad, everything I learn is new...  :)
0

Featured Post

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.

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

896 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

13 Experts available now in Live!

Get 1:1 Help Now