Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a number with commas but without decimals in SQL Server 2008 R2?

I am using the following to create a comma formatted number in T-SQL. How can I get rid of the decimal point and the digits after decimal. So if I get 1,112.00 after formatting, how would I get only 1,112?

SELECT CONVERT(varchar, CAST(1112 AS money), 1)
like image 695
Sunil Avatar asked Nov 03 '12 07:11

Sunil


2 Answers

https://database.guide/how-to-format-numbers-in-sql-server/

Starting on SQL 2012 you can write:

SELECT FORMAT(ColumnName, 'N0');
like image 81
Jorge Alejandro Ramirez Lopez Avatar answered Sep 30 '22 14:09

Jorge Alejandro Ramirez Lopez


PARSENAME(CONVERT(VARCHAR,CAST(1112 AS MONEY),1),2)

It will work nicely.

When convert number to money datatype automatically 2 zeros will be added after decimal. PARSENAME function will remove that zeros.

like image 45
Govind Avatar answered Sep 30 '22 13:09

Govind