SQL Server
SELECT FORMAT(1234567.891, 'N2');
FORMAT uses .NET format strings ('N2' = thousands separators and 2 decimals) and calls into the CLR for every row, so it is much slower than CONVERT(VARCHAR, x), which has no mask.
Five engines, four different formatting idioms for the same 1,234,567.89.
Filed under Types & casting
There is no portable formatted conversion: TO_CHAR masks in PostgreSQL and Oracle, a costly FORMAT in SQL Server, a FORMAT in MySQL that always adds separators, and printf-style FORMAT in SQLite.
Casting a number to text is easy; formatting it with thousand separators and decimals is where the dialects diverge completely. Each row formats the same number to 1,234,567.89 in its engine's native idiom.
SELECT FORMAT(1234567.891, 'N2');
FORMAT uses .NET format strings ('N2' = thousands separators and 2 decimals) and calls into the CLR for every row, so it is much slower than CONVERT(VARCHAR, x), which has no mask.
SELECT TO_CHAR(1234567.891, 'FM9,999,999.00');
Mask syntax: 9 is a digit, 0 is a forced digit, FM trims padding. TO_CHAR is the idiomatic tool.
SELECT FORMAT(1234567.891, 2);
FORMAT always adds thousands separators and rounds to the given decimals; CAST(42 AS CHAR) is the plain, maskless conversion.
SELECT FORMAT('%,.2f', 1234567.891);
FORMAT (3.38+, the new name for printf) takes C-style masks. The ',' flag is a SQLite extension that adds thousands separators, so this returns 1,234,567.89.
SELECT TO_CHAR(1234567.891, 'FM9,999,999.00') FROM dual;
Same mask family as PostgreSQL. Group separator and decimal character follow NLS settings.
9 placeholders reserve room for a sign and pad to the mask width. Prefix the mask with FM to trim the padding.
In Oracle and PostgreSQL the D and G mask characters follow NLS_NUMERIC_CHARACTERS and lc_numeric. MySQL's FORMAT takes an optional locale, FORMAT(x, 2, 'de_DE'), and defaults to en_US. SQL Server's FORMAT takes an optional culture such as 'de-DE' and otherwise follows the session language.
No. It invokes the .NET runtime per row. Use CONVERT for plain casts and reserve FORMAT for display-level queries.
CAST runs on all five engines; the native forms add format masks, style codes or a shorter spelling.
The same format call does not translate across engines: the masks themselves are different languages.
Text '42' becomes 42 everywhere, but 'abc' raises an error in three engines and quietly becomes 0 in two.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.