Solved

Comma seperator in results

Posted on 2004-09-22
11
665 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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

726 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