?
Solved

Format a number (string data type) with comma

Posted on 2000-05-14
1
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 7

Accepted Solution

by:
spiridonov earned 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

719 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