Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > sql server programming > march 2009 >

sql server programming : format number with thousand-separator


JTL
1/25/2007 11:52:05 AM
is there a built in function in sql server 2005 that will format a number
with commas as the thousand-separator?

ie., here's what i want to do:
input=1000
output=1,000

or
input=1000000
output=1,000,000

if there isn't a built-in function, can someone point me to a
user-defined-function that might do the trick?

thanks so much,

jt



JTL
1/25/2007 12:23:05 PM
BECAUSE IM PULLING DATA FOR MS REPORTING SERVICES AND THE ONLY FORMAT
OPTIONS AVAILABLE ARE:

1234
1,234.00 (this one is close, but not what i want- this particular number
will never have decimals so i don't want to waste space showing ".00")
1234.00
1.234123e+003

its not helpful to reply when you don't have an answer, b/c then other
people assume, by seeing a response next to the posting, that the problem
has been solved.



[quoted text, click to view]

Vern Rabe
1/25/2007 12:51:01 PM
its not helpful to reply when you don't have an answer, b/c then other
people assume, by seeing a response next to the posting, that the problem
has been solved.

You're absolutely right. And every time I see that Aaron has posted a
response, I open it to learn something new.

Vern Rabe

[quoted text, click to view]
kgerritsen
1/25/2007 1:06:35 PM

First of all, Aaron's answer was perfect given the amount of
description of the issue in the original posting. Since Aaron's reply
has now elucidated from you that your issue is in Microsoft Reporting
Services (the presentation layer in your system), there is no better
demonstration that his reply was perfect at pushing YOU to think
through your system and accurately identify where the problem is.

Now that we understand where the issue is, I found the following in BOL
in seconds:

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/rptsrvr9/html/7852b4c7-3b45-406d-8e43-69c8cf2b710e.htm

Formatting Numeric and Date Values in a Report
You can specify a format for numeric and date values by updating the
Format property of its text box with a formatting string. For example,
you can set a text box for a numeric field to display the number as
currency. Reporting Services uses Microsoft .NET Framework formatting
strings or you can create a custom formatting string for the Format
property.
....
You can modify many of the format strings to include a precision
specifier that defines the number of digits to the right of the decimal
point. For example, a formatting string of D0 formats the number so
that it has no digits after the decimal point. You can also use custom
formatting strings, for example, #,###.

Have you tried defining a custom formatting string as per the manual?


[quoted text, click to view]
Stijn Verrept
1/25/2007 2:25:43 PM
[quoted text, click to view]

And Delphi has FloatToStrF

And I agree with you :)


--
Kind regards,

Stijn Verrept
1/25/2007 2:33:14 PM
[quoted text, click to view]

I'm sure this must be possible in Reporting services! I'm gonna look
at it later tonight, gotta go now.

[quoted text, click to view]

And it's also not polite to start shouting at someone who is trying to
help.

--
HTH,

Tracy McKibben
1/25/2007 2:36:22 PM
[quoted text, click to view]

It's not helpful to post questions and then argue about the answers
before you've researched the tools that you're using:
http://msdn2.microsoft.com/en-US/library/ms157406.aspx

See the part about custom format strings...


--
Tracy McKibben
MCDBA
Aaron Bertrand [SQL Server MVP]
1/25/2007 3:00:36 PM
VB has format()
VBScript has formatnumber()
C# has string.format()

Why do you need to perform this presentation layer task in the database?





[quoted text, click to view]

JTL
1/25/2007 3:43:36 PM
ok ok- i'm sorry to have started the argument-thank you all for helping
solve my problem.

aaron, im also sorry about yelling in my response- that was rude, but i was
frustrated at the moment, and it has been my experience that when a response
is posted that does not answer the question at hand, the likelihood of
getting a valid answer dramatically decreases. obviously, this was not the
case in this posting.

at any rate, by applying the formatting string "N0", i was able to get the
exact format i needed. i had researched the issue prior to posting, but
had not been thorough enough. the example in BOL describes that "a
formatting string of D0 formats the number so that it has no digits after
the decimal point." In my hastiness, I didn't realize that this same logic
could be applied to the "N" formatting string, as well.




[quoted text, click to view]

Aaron Bertrand [SQL Server MVP]
1/25/2007 4:01:13 PM
[quoted text, click to view]

Well, maybe you can't see that my question was meant to derive more
information to help you better.

[quoted text, click to view]

Maybe you assume that, but I don't know anyone who does.

And since you'd rather yell at me than do your own research, good luck with
your problem!

folderol
1/25/2007 9:02:31 PM
[quoted text, click to view]

declare @test as money
set @test = 19250500
select @test, left(convert(varchar(20), @test, 1), len(convert(varchar(20),
@test, 1)) -3)

or

declare @test as decimal
set @test = 19250500
select @test, left(cast(convert(varchar(20),cast(@test as money),1) as
varchar), len(convert(varchar(20),cast(@test as money),1)) -3)
fafenail
3/10/2009 11:26:35 PM
--Other Alternative
-------------------------
declare @test as money
set @test = 19250500
SELECT CAST(CONVERT(varchar, CAST(@test AS money), 1) AS varchar)

From http://www.developmentnow.com/g/113_2007_1_0_0_925917/format-number-with-thousand-separator.htm

Posted via DevelopmentNow.com Groups
fafenail
3/11/2009 6:27:50 PM
--Very Simple
---------------------------------
declare @test as money
set @test = 19250500
Select Convert(varchar, @test, 1)


From http://www.developmentnow.com/g/113_2007_1_0_0_925917/format-number-with-thousand-separator.htm

Posted via DevelopmentNow.com Groups
Les
11/10/2009 8:05:50 AM
try
Select convert(varchar,cast(123456.01 as money),1)

works only for Money (2 decimal)

From http://www.developmentnow.com/g/113_2009_3_0_0_925917/format-number-with-thousand-separator.htm

Posted via DevelopmentNow.com Groups
Kenje
10/31/2011 2:36:56 AM
Lets us seek peace always, what matter is that we work together towards helping each other, when this world is over it will not matter who had what reporting server issue, but what will matter is if we were able to live together in love.

From http://www.developmentnow.com/g/113_2009_11_0_0_925917/format-number-with-thousand-separator.htm

Posted via DevelopmentNow.com Groups
AddThis Social Bookmark Button