Solved

Comma seperator in results

Posted on 2004-09-22
11
660 Views
Last Modified: 2012-08-13
While fetching the values from the table I want the result with comma seperator. The data type of the field is decimal

For ex the value in the table is 10000. While fetching I want the result as 10,000.
0
Comment
Question by:mvkannan
  • 4
  • 3
11 Comments
 
LVL 26

Expert Comment

by:Hilaire
ID: 12121182
This is a presentation issue.
It is generally admitted that display issue are best handled on the front-end side.

To transform a numeric value in SQL Server, you'd have to convert to another datatype (varchar)
The algorythm could be rather complex is the numeric values ahve decimal places.

Is there a particular reason why you'd need it in SQL Server ?

Hilaire


0
 

Author Comment

by:mvkannan
ID: 12129113
Hi Hilaire
I don't want the values in the table to have comma seperated. We need this only for the report. We are giving the SQL queries to the user. In the result users want to see the values with comma seperation. So it need to be done only in the query.
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 12130707
>>We are giving the SQL queries to the user.<<
Do you mean you give the resultset (the output of the query) or the query iself ?

If you give the query, I guess the user puts it in a reporting tool. Most reporting tools (that's what I called the front-end side) have standard functions to deal with presentation issue. You can even change display format (number of decimal places, thousands separator) with two button clics.

If you give the query output, just save the output as a CSV file in QA (or copy-paste to Excel),
open the file with Excel, change column format to "Number" and choose a display style that fits the user's requirements. Then give the XL file ...

If none of the solutions above fits your needs,
I should be able to come with a T-SQL function, but it will make query performance suck, (mostly if the query fetches a lot of data).
Just post sample numbers in both current and expected format (i need to know if there will be decimal places, if you need a fixed number of decimal places  ...)

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.

 
LVL 26

Expert Comment

by:Hilaire
ID: 12130714
I know your first sample has no decimal places,
but decimal datatype could be decimal(30,10) as well ...
0
 

Author Comment

by:mvkannan
ID: 12131457
>>We are giving the SQL queries to the user.<<
We give the SQL queries to the user. They will run the query in the query analyser and will save the result to excel file. They don't want to format the excel cells. So when I run the query the result should come with comma seperated.
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 12131529
>>They don't want to format the excel cells<<
If I give you code to build formatted strings,
EXCEL will "see" those numbers as strings, and thus the users will lose the ability to make a SUM, to sort numbers by value (values will sort alphabetically instead, ie "99" > "10000"), to build pivot tables, ....
And in the end they'll ask you to remove the commas or make a replace by themselves

Again, this should be handled on the front-end side.
You could set up a VBA macro to make the formatting so that they don't have to do it manually each and every time the reports is refreshed.

If you really need a T-SQL Solution (inappropriate IMHO), I need to know if the numbers have decimal places or are pure integers.

Hilaire
0
 

Author Comment

by:mvkannan
ID: 12139822
The data type for that field is decimal. But some values will have decimal and some will be whole no.
0
 
LVL 12

Accepted Solution

by:
kselvia earned 250 total points
ID: 12284562
I forget where I got it but I downloaded this from someplace long ago:

CREATE FUNCTION f_FormatIntegerString ( @Input varchar( 6000 ) )
RETURNS      varchar( 8000 )
AS

BEGIN

DECLARE      @TempData      varchar( 8000 ),
      @Output            varchar( 8000 ),
      @DecimalPortion      varchar( 2000 ),
      @Length            int,
      @HasDecimal      int,
      @Done            bit

-- Default some of our variables
SELECT      @Output      = '',
      @Done      = 0

-- Determine if we have a decimal point in the input string
SELECT      @HasDecimal = PATINDEX( '%.%', @Input )

IF ( @HasDecimal > 0 )
BEGIN
      -- If we do, store the relevant characters to the right of the decimal place
      SELECT      @TempData = RTRIM( SUBSTRING( @Input, 1, ( PATINDEX( '%.%', @Input ) - 1 ) ) )
      SELECT      @DecimalPortion = RTRIM( SUBSTRING( @Input, @HasDecimal, DATALENGTH( @Input ) - ( @HasDecimal - 1 ) ) )
END
ELSE
BEGIN
      -- No decimal place, grab the whole string
      SELECT      @TempData = RTRIM( @Input )
      SELECT      @DecimalPortion = ''
END

SELECT      @Length = DATALENGTH( RTRIM( @TempData ) )

-- If we have 3 or less numbers, return the string as-is
IF ( @Length <= 3 OR @Length IS NULL )
BEGIN
      SELECT      @Output = RTRIM( @TempData )
      SELECT      @Done = 1
END

-- Loop through the string, from back to front, adding , as needed,
-- until we have 3 or fewer characters left at the front of the string
WHILE ( @Done = 0 )
BEGIN
      SELECT      @Output = ','
                  + RTRIM( SUBSTRING( @TempData, @Length - 2, 3 ) )
                  + RTRIM( @Output )
      SELECT      @TempData = RTRIM( SUBSTRING( @TempData, 1, @Length - 3 ) )


      SELECT      @Length = DATALENGTH( RTRIM( @TempData ) )
      IF ( @Length <= 3 )
      BEGIN
            SELECT      @Output = RTRIM( SUBSTRING( @TempData, 1, @Length ) )
                        + RTRIM( @Output )
            SELECT      @Done = 1
      END
END

-- Add back the decimal portion, if any
SELECT      @Output = RTRIM( @Output )
            + RTRIM( @DecimalPortion )

-- Return the new "formatted" result
RETURN( @Output )

END



Or you might take a look at convert and the money datatype in BOL for a quicker option that may work for you;

select convert(varchar,convert(money,12234.12),1)

------------------------------
12,234.12
0

Featured Post

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.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

809 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