Solved

Format a number (string data type) with comma

Posted on 2000-05-14
1
190 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dynamics crm 2011 8 46
Sql query 34 22
Split Data in 1 column into 2 columns 8 21
login and database user 3 21
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

12 Experts available now in Live!

Get 1:1 Help Now