Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Format a number (string data type) with comma

Posted on 2000-05-14
1
Medium Priority
?
213 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:
Victor 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

916 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