Unlimited Web Space

Enjoy Unlimited web hosting

Nov 3, 2011

How to put comma(,) seperations in a large numeric value of SQL Server Database using select statement?

inserting commas in number SQL
 Suppose you have a large numeric value that you want to make more readable in SQL select query, that is, you have 1000000000 numeric data and you want to display it as 1,000,000,000.00 or may be just 1,000,000,000 if it is not a money. It can be easily done in SQL Server by joining two/three functions: CAST and CONVERT and PARSENAME


1. First lets create a table or simply decleare it as

CREATE TABLE Table_1(COLUMN_1 NVARCHAR(20), COLUMN_2 NVARCHAR (20))
INSERT INTO Table_1 VALUES(1230000000.45,3250000000.87)

Now we have the table T1 as:

COLUMN_1        COLUMN_2

1230000000.45        3250000000.87

2. USE THE QUERY BELOW

CORRECT QUERY:

SELECT (select convert(varchar,cast(COLUMN_1 as money),1)) FROM Table_1
This will give 1,230,000,000.45

SELECT PARSENAME((select convert(varchar,cast(COLUMN_2 as money),1)), 2) FROM Table_1
This will give 3,250,000,000

It can also be done by a select statement as:

SELECT PARSENAME((select convert(varchar,cast(10000000 as money),1)), 2)
10,000,000

No comments:

Post a Comment