Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Number Formatting

I'm looking to use SQL to format a number with commas in the thousands, but no decimal (so can't use Money) - any suggestions?

I'm using SQL Server 2005, but feel free to answer for others as well (like MySQL)

like image 956
Seibar Avatar asked Dec 02 '25 21:12

Seibar


1 Answers

With TSQL you could cast to money and convert it will add the .00, but you could use replace or substring to remove.

replace(convert(varchar, cast(column as money), 1), '.00', '')

In SQL 2005 you could use a CLR function as well

[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString FormatNumber(SqlInt32 number)
{
    return number.Value.ToString("N0");
}

and call it as any other user-defined function

SELECT dbo.FormatNumber(value)
like image 185
Scott Nichols Avatar answered Dec 05 '25 13:12

Scott Nichols



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!