Solved

Comma seperator in results

Posted on 2004-09-22
11
643 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
 
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 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

19 Experts available now in Live!

Get 1:1 Help Now