When formatting a date using the FORMAT()
function in SQL Server, the date will be formatted according to the language of your local session. However, you can override this by specifying a culture to use, or using a custom date format.
This article demonstrates how to explicitly specify a German date format by using the optional “culture” argument of the FORMAT()
function. It also demonstrates how to use your own custom date format if that is more desirable.
Example 1 – Short German Date Format
To explicitly specify that a date should be displayed in German format, use de-de
as the third argument. This (optional) third argument specifies which culture to use.
DECLARE @thedate date = '2020-12-01' SELECT FORMAT(@thedate, 'd', 'de-de') Result;
Result:
+------------+ | Result | |------------| | 01.12.2020 | +------------+
In this case, I used a lowercase d
as the second argument. This results in a relatively short date format, with the day and month being displayed as numbers.
And because we’re using German format, the day comes before the month, and each date component is separated by periods.
Example 2 – Long German Date Format
You can change the second argument to an uppercase D
to result in a longer date format, with the month spelled out:
DECLARE @thedate date = '2020-12-01' SELECT FORMAT(@thedate, 'D', 'de-de') Result;
Result:
+----------------------------+ | Result | |----------------------------| | Dienstag, 1. Dezember 2020 | +----------------------------+
Example 3 – Custom German Date Format
You can also use a custom date format if required. This allows you to explicitly state exactly how and where each date component goes.
Example:
DECLARE @thedate date = '2020-12-01' SELECT FORMAT(@thedate, 'ddd, d. MMM, yyyy', 'de-de') Result;
Result:
+------------------+ | Result | |------------------| | Di, 1. Dez, 2020 | +------------------+
In this case, I still use the culture argument to explicitly specify which language to use.
My system currently uses US English, so if I omit the culture argument from this example, I get the following:
DECLARE @thedate date = '2020-12-01' SELECT FORMAT(@thedate, 'ddd, d. MMM, yyyy') Result;
Result:
+-------------------+ | Result | |-------------------| | Tue, 1. Dec, 2020 | +-------------------+
Checking your Current Session
When using the FORMAT()
function, if the culture argument is not provided, the language of the current session is used. This language is set either implicitly, or explicitly by using the SET LANGUAGE
statement.
For more info, here are 3 Ways to Get the Language of the Current Session in SQL Server (T-SQL).
Also see How to Set the Current Language in SQL Server (T-SQL).