How to Specify the Invariant Culture when using FORMAT() in SQL Server

In SQL Server, you can use the FORMAT() function to format date/time and number values as strings. In particular, the function provides “locale-aware” formatting, and the function accepts a “culture” argument, which allows you to specify a culture or language to use for the actual format. For example, you can pass en-us to ensure the results are formatted in US English format.

The culture argument is optional, so if you don’t provide it, the output will be determined by the language of the current session.

The FORMAT() function accepts any culture supported by the .NET Framework as an argument (you’re not limited to the languages explicitly supported by SQL Server).

One of the cultures supported by the .NET Framework is the invariant culture. The invariant culture is culture-insensitive. More specifically, this culture is associated with the English language but not with any country/region.

To specify that FORMAT() should output the results using the invariant culture, simply use "iv" for the culture argument (the third argument).

Example 1 – Currency

Here’s an example of formatting a value as a currency using the invariant culture.

SELECT FORMAT(123, 'C', 'iv') Result;

Results:

+----------+
| Result   |
|----------|
| ¤123.00  |
+----------+

This results in the output having a fractional part with a decimal place. It’s also prepended with the currency sign (¤), which is used to denote an unspecified currency (this is Unicode character U+00A4).

Example 2 – Date

Here’s an example of formatting a date using the invariant culture along with a short date format.

DECLARE @date date = '2030-05-25';
SELECT FORMAT(@date, 'd', 'iv') Result;

Results:

+------------+
| Result     |
|------------|
| 05/25/2030 |
+------------+

The short date format for the invariant culture is MM/dd/yyyy.

Here it is again, but this time I compare it to the US English, British, and German (Deutsch) cultures.

DECLARE @date date = '2030-05-25';
SELECT 
  FORMAT(@date, 'd', 'iv') 'Invariant Culture',
  FORMAT(@date, 'd', 'en-us') 'US English',
  FORMAT(@date, 'd', 'en-gb') 'British',
  FORMAT(@date, 'd', 'de-de') 'German (Deutsch)';

Results:

+---------------------+--------------+------------+--------------------+
| Invariant Culture   | US English   | British    | German (Deutsch)   |
|---------------------+--------------+------------+--------------------|
| 05/25/2030          | 5/25/2030    | 25/05/2030 | 25.05.2030         |
+---------------------+--------------+------------+--------------------+

Example 3 – All Standard Date and Time Formats

Here’s another one using the invariant culture, but this time I format a datetimeoffset value using all of the standard date and time format strings supported by the .NET Framework.

DECLARE @date datetimeoffset, @culture char(2); 
SET @date = '2030-05-25 23:59:30.1234567 +07:00';
SET @culture = 'iv';
SELECT 
  FORMAT(@date, 'd', @culture) AS 'd',
  FORMAT(@date, 'D', @culture) AS 'D',
  FORMAT(@date, 'f', @culture) AS 'f',
  FORMAT(@date, 'F', @culture) AS 'F',
  FORMAT(@date, 'g', @culture) AS 'g',
  FORMAT(@date, 'G', @culture) AS 'G',
  FORMAT(@date, 'm', @culture) AS 'm',
  FORMAT(@date, 'M', @culture) AS 'M',
  FORMAT(@date, 'o', @culture) AS 'o',
  FORMAT(@date, 'O', @culture) AS 'O',
  FORMAT(@date, 'r', @culture) AS 'r',
  FORMAT(@date, 'R', @culture) AS 'R',
  FORMAT(@date, 's', @culture) AS 's',
  FORMAT(@date, 't', @culture) AS 't',
  FORMAT(@date, 'T', @culture) AS 'T',
  FORMAT(@date, 'u', @culture) AS 'u',
  FORMAT(@date, 'U', @culture) AS 'U',
  FORMAT(@date, 'y', @culture) AS 'y',
  FORMAT(@date, 'Y', @culture) AS 'Y';

Result (using vertical output):

d | 05/25/2030
D | Saturday, 25 May 2030
f | Saturday, 25 May 2030 23:59
F | Saturday, 25 May 2030 23:59:30
g | 05/25/2030 23:59
G | 05/25/2030 23:59:30
m | May 25
M | May 25
o | 2030-05-25T23:59:30.1234567+07:00
O | 2030-05-25T23:59:30.1234567+07:00
r | Sat, 25 May 2030 16:59:30 GMT
R | Sat, 25 May 2030 16:59:30 GMT
s | 2030-05-25T23:59:30
t | 23:59
T | 23:59:30
u | 2030-05-25 16:59:30Z
U | NULL
y | 2030 May
Y | 2030 May

The Invariant Culture Doesn’t Work on your System?

If you’re unable to get the invariant culture to work on your system, it could be due to an older version of the .NET Framework being installed. The documentation for the .NET Framework 4.8 explicitly states that the invariant culture is available using its ISO 639-1 two-letter code "iv".

However, on this Stack Overflow question, the answer from Solomon Rutzky suggests that this might only have been available from .NET 4.5.

If you can’t get it to work using an earlier version of .NET, try upgrading to the latest version (or a version from at least 4.5).