Solved

Format a number (string data type) with comma

Posted on 2000-05-14
1
196 Views
Last Modified: 2008-02-26
Is there a built-in function to format a number with commas? like '12345' to '12,345'.
0
Comment
Question by:yurrea
1 Comment
 
LVL 7

Accepted Solution

by:
spiridonov earned 50 total points
ID: 2809526
If you mean built into Transact-SQL, the answer is no. You can try to implement formating by yourself using SUBSTRING,DATALENGTH,etc functions.
For example

CREATE PROCEDURE test1
@num_string varchar(20)
AS
DECLARE @new_string VARCHAR(50)
DECLARE @num_len int
DECLARE @position int

SELECT @num_len=DATALENGTH(@num_string)
SELECT @position=@num_len-3
WHILE @num_len>3
 BEGIN
  SELECT @position=@num_len-3
  SELECT @new_string=','+SUBSTRING(@num_string,@position+1,3)+@new_string
  SELECT @num_len=@num_len-3
 END
 SELECT @new_string=SUBSTRING(@num_string,1,@num_len)+@new_string
 SELECT @new_string
GO

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

821 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