Comma seperator in results

Posted on 2004-09-22
Medium Priority
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.
Question by:mvkannan
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
  • 4
  • 3
LVL 26

Expert Comment

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 ?



Author Comment

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.
LVL 26

Expert Comment

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  ...)

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

LVL 26

Expert Comment

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

Author Comment

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.
LVL 26

Expert Comment

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.


Author Comment

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

Accepted Solution

kselvia earned 1000 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 )


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 )
      -- 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 ) ) )
      -- No decimal place, grab the whole string
      SELECT      @TempData = RTRIM( @Input )
      SELECT      @DecimalPortion = ''

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

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

-- 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 )
      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 )
            SELECT      @Output = RTRIM( SUBSTRING( @TempData, 1, @Length ) )
                        + RTRIM( @Output )
            SELECT      @Done = 1

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

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


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)


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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

752 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