Unix Technical Forum

Formating Numbers with Commas

This is a discussion on Formating Numbers with Commas within the SQL Server forums, part of the Microsoft SQL Server category; --> I am pulling several numbers from a SQL table, adding them and doing various calculaitons. The numbers do not ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:24 AM
Cat
 
Posts: n/a
Default Formating Numbers with Commas

I am pulling several numbers from a SQL table, adding them and doing
various calculaitons. The numbers do not display a comma to separate
thousands. What is a way to format this?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:24 AM
Simon Hayes
 
Posts: n/a
Default Re: Formating Numbers with Commas

Presentation and formatting are usually done in the client, not the
server. In this case, for example, many countries do not use a comma
for separating thousands, so your client application can check the
user's locale and apply the correct formatting.

Simon

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:24 AM
jennifer1970@hotmail.com
 
Posts: n/a
Default Re: Formating Numbers with Commas

There's no easy way to do this. For some reason I did need output like
this and wasn't able to use a front end to do the formatting, so I made
my own function.

Use as so:

SELECT dbo.Format_Number(513434512.2344)

Output is $513,434,512.23

Yes, it rounds and adds a dollar sign. But you can change it around.


HTH,
Jennifer



CREATE FUNCTION Format_Number (@N decimal(18,2))
RETURNS nVarChar(30)

AS

BEGIN

Declare @NRnd Decimal(18,2)
Declare @Dollar nVarChar(30)
Declare @Dollar2 nVarChar(30)
Declare @L int
Declare @A int
Declare @B int
Declare @C int
Declare @Cents nvarchar(20)
Declare @NC nvarchar(30)


Set @NC = Cast(@N as Nvarchar(30))

Set @NRnd = Round(@N, 0, 1)
Set @Dollar2 = ''
Set @Dollar = Cast(@NRnd as NvarChar(30))
Set @Dollar = Substring(@Dollar,1, Len(@Dollar) - 3)

Set @C = PATINDEX('%.%',@NC)
Set @Cents = Substring(@NC, @C, 3)
Set @L = Len(@Dollar)
Set @A = @L/3

Set @B = 3
While @A >= 0
Begin
Set @Dollar2 = Substring(@Dollar,@L - @B + 1,3) + ',' + @Dollar2
Set @B = @B + 3
Set @A = @A - 1
End
If Left(@Dollar2,1) = ','
Set @Dollar2 = Substring(@Dollar2, 2, Len(@Dollar2))

Return '$' + Substring(@Dollar2,1, Len(@Dollar2)-1) + @Cents
END

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:24 AM
--CELKO--
 
Posts: n/a
Default Re: Formating Numbers with Commas

>> For some reason I did need output like this and wasn't able to use a
front end to do the formatting, so I made my own function. <<

Since this is a fundamental violation of software engineering
prtinciples, might you share with us WHAT that reason was? It is worth
a paper in a journal.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 07:25 AM
jennifer1970@hotmail.com
 
Posts: n/a
Default Re: Formating Numbers with Commas

It was a totally stupid reason, of course. My boss wanted an email
output of a query emailed to him on a daily basis, so I set up a job to
do that. And then he came back and said, it sure would be nice if
those dollar amounts looked like dollars, and could the output be
changed. So being completely new and straight out of school I did as
asked.

Let me know how that paper comes out, will you?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 07:25 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Formating Numbers with Commas

--CELKO-- (jcelko212@earthlink.net) writes:
> Since this is a fundamental violation of software engineering
> prtinciples, might you share with us WHAT that reason was? It is worth
> a paper in a journal.


The world is not always as ideal as you may want to be. There are probably
tons of business reports out there that are run from no other front end
than Query Analyzer, or similar tool. For some reason, someone started to
do it in QA, probably because it was a little urgent, and not possible to
pack into something better. Then that temporary hack became permaent etc.
Until one day, the requirements goes beyond what is really healthy to do
in SQL.

Anoher reason could be that the front-end tool is hopelessly difficult
to use...

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:45 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com