Thankfully(?), in SQL Server 2012+, you can now use FORMAT() to achieve this:

FORMAT(@s,'#,0.0000')


In prior versions, at the risk of looking real ugly

[Query]:

declare @s decimal(18,10);
set @s = 1234.1234567;
select replace(convert(varchar,cast(floor(@s) as money),1),'.00',
    '.'+right(cast(@s * 10000 +10000.5 as int),4))

In the first part, we use MONEY->VARCHAR to produce the commas, but FLOOR() is used to ensure the decimals go to .00. This is easily identifiable and replaced with the 4 digits after the decimal place using a mixture of shifting (*10000) and CAST as INT (truncation) to derive the digits.

[Results]:

|   COLUMN_0 |
--------------
| 1,234.1235 |

But unless you have to deliver business reports using SQL Server Management Studio or SQLCMD, this is NEVER the correct solution, even if it can be done. Any front-end or reporting environment has proper functions to handle display formatting.

Answer from RichardTheKiwi on Stack Overflow
Top answer
1 of 5
40

Thankfully(?), in SQL Server 2012+, you can now use FORMAT() to achieve this:

FORMAT(@s,'#,0.0000')


In prior versions, at the risk of looking real ugly

[Query]:

declare @s decimal(18,10);
set @s = 1234.1234567;
select replace(convert(varchar,cast(floor(@s) as money),1),'.00',
    '.'+right(cast(@s * 10000 +10000.5 as int),4))

In the first part, we use MONEY->VARCHAR to produce the commas, but FLOOR() is used to ensure the decimals go to .00. This is easily identifiable and replaced with the 4 digits after the decimal place using a mixture of shifting (*10000) and CAST as INT (truncation) to derive the digits.

[Results]:

|   COLUMN_0 |
--------------
| 1,234.1235 |

But unless you have to deliver business reports using SQL Server Management Studio or SQLCMD, this is NEVER the correct solution, even if it can be done. Any front-end or reporting environment has proper functions to handle display formatting.

2 of 5
15

without considering this to be a good idea...

select dbo.F_AddThousandSeparators(convert(varchar, convert(decimal(18, 4), 1234.1234567), 1))

Function

-- Author:      bummi
-- Create date: 20121106
CREATE FUNCTION F_AddThousandSeparators(@NumStr varchar(50)) 
RETURNS Varchar(50)
AS
BEGIN
declare @OutStr varchar(50)
declare @i int
declare @run int

Select @i=CHARINDEX('.',@NumStr)
if @i=0 
    begin
    set @i=LEN(@NumStr)
    Set @Outstr=''
    end
else
    begin   
     Set @Outstr=SUBSTRING(@NUmStr,@i,50)
     Set @i=@i -1
    end 


Set @run=0

While @i>0
    begin
      if @Run=3
        begin
          Set @Outstr=','+@Outstr
          Set @run=0
        end
      Set @Outstr=SUBSTRING(@NumStr,@i,1) +@Outstr  
      Set @i=@i-1
      Set @run=@run + 1     
    end

    RETURN @OutStr

END
GO
🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › t-sql › functions › format-transact-sql
FORMAT (Transact-SQL) - SQL Server | Microsoft Learn
... This example uses the N format specifier. The N specifier is used for numeric values, and the number of decimal places can be adjusted by changing the format string (for example, N2 for two decimal places).
Discussions

sql server - How do I format a number with commas in T-SQL? - Stack Overflow
While I agree with everyone, including ... in T-SQL by casting to money and then converting to varchar. This does include trailing decimals, though, that could be looped off with SUBSTRING. SELECT CONVERT(varchar, CAST(987654321 AS money), 1) ... While I agree that generally formatting should happen elsewhere, we all take the date formatting functions for granted. Comma insertion ... More on stackoverflow.com
🌐 stackoverflow.com
sql server - Best way to put commas into large numbers - Database Administrators Stack Exchange
I've started a new job and it involves looking at a bunch of big numbers. Is there an easy way to add commas to an int or decimal field to make it readable? For example, SQL Server outputs the column on the left, but for my own sanity, I need it to look like the one on the right: ... The perfect thing would be commas in the display grid, then plain integers in the output. ... TSQL's FORMAT ... More on dba.stackexchange.com
🌐 dba.stackexchange.com
September 5, 2018
Select convert to 2 decimal places, comma, right aligned – SQLServerCentral Forums
Formatting is usually best done in the front end tool. ... Need an answer? No, you need a question My blog at https://sqlkover.com. MCSE Business Intelligence - Microsoft Data Platform MVP ... I agree this should be done in the frontend app. This code will give you the commas and decimal places More on sqlservercentral.com
🌐 sqlservercentral.com
May 29, 2014
Format a number with commas but without decimals in SQL Server 2008 R2? - Stack Overflow
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 onl... More on stackoverflow.com
🌐 stackoverflow.com
November 14, 2023
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › formatting-numbers-commas-and-decimal
Formatting numbers (commas and decimal) – SQLServerCentral Forums
September 22, 2007 - -- Frank Kalis Microsoft SQL Server MVP Webmaster: http://www.insidesql.org/blogs My blog: http://www.insidesql.org/blogs/frankkalis/[/url] ... SSC Eights! ... After confirming what Frank says about 'resentation issue...' I just add that sometimes a 'vb format in tsql' can be usefule... ... Peter E. Kierstead ... I use this quick and dirty function (emphasis on adjectives) for integer and decimal values.
🌐
My Tec Bits
mytecbits.com › home › microsoft › sql server › how to format a number with commas in sql server?
How to format a number with commas in SQL Server? | My Tec Bits
October 18, 2023 - The FORMAT function was introduced in SQL Server 2012. This is the most flexible and straightforward way to format numbers using patterns and culture settings. DECLARE @Number Numeric(14,5) = 78587547.3489; SELECT @Number, FORMAT(@Number, 'N', 'en-US') as 'US English', FORMAT(@Number, 'N', 'en-IN') as 'India English', FORMAT(@Number, 'N4', 'en-US') as 'US English 4 decimals' GO
🌐
Sqlmatters
sqlmatters.com › Articles › Formatting a number with thousand separators.aspx
Formatting a number with thousand separators - SQLMatters
Post by Darren on Mon 03 Dec 2018 16:19. Report Inappropriate Post Website : Another way to get commas, which is easier (for me) to remember: SELECT FORMAT(123456789, 'N0') --that's a zero, specifying the number of decimal places Result: 123,456,789
🌐
MSSQLTips
mssqltips.com › home › sql format number with cast, convert and more
SQL Format Number with CAST, CONVERT and more
October 31, 2025 - Don’t ever use the FORMAT function in SQL Server. The performance is abysmal. Do some testing… you will find that it is not just a little bit slower. It’s a whole lot slower than anything you can probably imagine doing with CONVERT or using other options. somehow SELECT FORMAT(5634.6334, ‘N’, ‘en-us’) AS ‘Number’ doesn’t give the same answer, it rounded the decimal
Find elsewhere
🌐
DataCamp
datacamp.com › doc › mysql › mysql-format
MySQL FORMAT Expressions: Usage & Examples
In this syntax, `number` is the numeric value to be formatted, and `decimal_places` specifies the number of decimal places to retain. ... This example formats the number `1234567.89` to `1,234,567.89`, adding commas as thousands separators and retaining two decimal places.
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › select-convert-to-2-decimal-places-comma-right-aligned
Select convert to 2 decimal places, comma, right aligned – SQLServerCentral Forums
May 29, 2014 - NOTE: Due to the use of two REVERSE functions and the FORMAT function, this is not recommended for use on queries that will produce a large number of rows. I'm personally using this for a query that outputs a small number of rows (<5000 rows) so for me, the performance hit is not a problem. I'm adding commas using the FORMAT() function, reversing it, then converting to a CHAR(10) to pad spaces, then reversing again.
🌐
W3Schools
w3schools.com › sql › func_mysql_format.asp
MySQL FORMAT() Function
SQL Examples SQL Editor SQL Quiz ... Training ... The FORMAT() function formats a number to a format like "#,###,###.##", rounded to a specified number of decimal places, then it returns the result as a string....
🌐
Database Guide
database.guide › how-to-format-numbers-with-commas-in-sql
How to Format Numbers with Commas in SQL
December 13, 2021 - It’s true that we could just ... information and examples for formatting numbers with commas in Oracle. In SQL Server we can use the FORMAT() function to format numbers with commas....
🌐
Database Guide
database.guide › how-to-format-numbers-with-commas-in-sql-server
How to Format Numbers with Commas in SQL Server
October 27, 2021 - It’s also possible to use custom format specifiers to construct your own custom format strings. For example, the # character is a digit placeholder, the 0 is a zero placeholder, the comma (,) is a placeholder for the group separator, and the full stop (.) is a placeholder for the decimal ... This is true regardless of the locale being used – SQL Server will work out which characters to use for the group and decimal separators based on the current locale.
🌐
YouTube
youtube.com › watch
How to Format Numeric Values with Commas in SQL - YouTube
Adding commas to numeric values is a very common task when querying in SQL, but you may be confused as how to actually go about this.I cover this scenario us...
Published   October 28, 2021
🌐
SQL Server Guides
sqlserverguides.com › format-number-with-commas-and-decimal-in-sql-server
How to Format Number with Commas and Decimal in SQL Server? - SQL Server Guides
November 6, 2023 - If you use the format pattern as N2 in the FORMAT() function, then it displays the number value with two decimal places with a thousand separators. So here, the number (2, 3, etc.) after the N represents the number of digits you want to put ...
🌐
Scaler
scaler.com › home › topics › mysql format() function
MySQL FORMAT() function - Scaler Topics
January 19, 2024 - The numeric_value parameter in ... two decimal places, the final formatted string will be '12,345.68'. Overall, MySQL's FORMAT() function is a valuable tool for formatting numeric values in various ways, and its clear syntax ...
🌐
Narkive
microsoft.public.sqlserver.programming.narkive.com › dxbn3B7O › format-number-with-commas
format number with commas
January 21, 2023 - Post by rodchar how do i take a decimal number and format with a comma for every thousandths place? SQL Server doesn't have a reliable way to do that across all regional settings except to convert to money and back with specific styles. It's not pretty. http://databases.aspfaq.com/database/how-do-i-deal-with-memo-text-hyperlink-and-currency-columns.html Why don't you perform this formatting at the client?
Top answer
1 of 3
15

(If you are using SQL Server 2012 or newer, please see @wBob's answer for a cleaner approach. The approach outlined in my answer below is only required if you are using SQL Server 2008 R2 or older.)

You don't need (or want) the thousands' separator when converting to NUMERIC, regardless if it is comma, period, or space, so just get rid of them first. Then convert the comma into a period / decimal and you are done:

SELECT CONVERT(NUMERIC(10, 2), 
               REPLACE(
                       REPLACE('7.000,45', '.', ''),
                       ',', '.'
                      )
              ) AS [Converted];

Returns:

7000.45

For the sake of completeness, I should mention that I also tried:

  • SET LANGUAGE Greek;

  • Looking at various format styles for CONVERT, but nothing applies here.

  • The FORMAT function, but the input type must be a numeric or date/time/datetime value (that and it was introduced in SQL Server 2012, so not applicable to SQL Server 2008 R2 or older).

And nothing else seemed to work. I was hoping to find something more elegant than two REPLACE calls, but so far no such luck.


Also, just to mention, while not a pure T-SQL solution, this can also be accomplished via SQLCLR. And, there is a pre-done function that does this in the SQL# library (that I wrote) named String_TryParseToDecimal. This function is available in the Free version, and works in every version of SQL Server starting with SQL Server 2005:

SELECT SQL#.String_TryParseToDecimal('7.000,45', 'el-GR');

Returns:

7000.45000000000000000000
2 of 3
14

What version of SQL Server are you using? From SQL Server 2012 onwards you can use TRY_PARSE with its USING culture argument. You can also use PARSE, the difference being PARSE will fail if the conversion fails and TRY_PARSE will return a NULL, eg

DECLARE @t TABLE ( x VARCHAR(10) )

INSERT INTO @t
VALUES ( '7.000,45' ), ( 'xxx' )

SELECT x, 
    TRY_PARSE( x AS NUMERIC(10,2) USING 'El-GR' ) x
FROM @t

HTH

🌐
Actian
docs.actian.com › psql › psqlv13 › sqlref › sqlref.SET_DECIMALSEPARATORCOMMA.htm
SET DECIMALSEPARATORCOMMA
September 19, 2020 - The PSQL database engine by default displays decimal data using a period (.) as the separator between ones and tenths (for example, 100.95). The SET DECIMALSEPARATORCOMMA statement allows you to specify that results should be displayed using a comma to separate ones and tenths (for example, 100,95).