Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Number to string with comma

Author  Topic 

anderskd
Starting Member

25 Posts

Posted - 2010-08-11 : 15:37:00
Hello,
This might be an easy one, but I can't seem to find an answer.

I'm trying to concatenate a numeric value with some padding to make a string that should include commas and periods.

The example number value currently is: 1234

I need it to display like this - with the commas and decimal points:
****1,234.00

I've been trying some different functions, but can't seem to get it to format the string correctly:
SELECT RIGHT('*********' + RTRIM(CONVERT(CHAR(20), 1234)), 12)

I would like the string value to be (ignoring the * padding for now):
1,234.00

Any ideas?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-08-11 : 15:47:59
SELECT CONVERT(VARCHAR(30), CAST(1234 AS MONEY), 2) -- Or one of 0, 1, 2, 3. Read Books Online which is proper.




N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2010-08-11 : 16:00:46
declare @num INT
SET @num = 222222
SELECT CONVERT(varchar(11),CONVERT(money,@num),1)

Although the format side of things should be done on application side

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

anderskd
Starting Member

25 Posts

Posted - 2010-08-11 : 16:07:19
That's exactly what I was looking for.

Thanks for the quick solutions!

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-08-13 : 11:03:13
quote:
Originally posted by anderskd

That's exactly what I was looking for.

Thanks for the quick solutions!




Also as stated, if you use front end application, do this formation there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -