Format SQL Server Dates with FORMAT Function
By: Daniel Calbimonte | Updated: 2021-10-13 | Comments (18) | Related: 1 | 2 | 3 | 4 | 5 | More > Dates
Microsoft SQL Server 2008 and earlier versions used the CONVERT functions to handle date formatting in SQL queries, SELECT statements, stored procedures and T-SQL scripts. In this tip, Date and Time Conversions Using SQL Server, we have a list of the available examples on how to use the CONVERT function to handle different date formats in a SQL database.
As you may know, the CONVERT function is not very flexible and we have limited date formats. In Microsoft SQL Server 2012 and later, the function FORMAT has been introduced which is much easier to use to format dates. This tutorial shows different examples of using this new function to format dates.
Starting with SQL Server 2012, a function to handle formatting dates was introduced which is similar to Oracle's to_date function. Many Oracle DBAs complained about the SQL Server CONVERT function and its poor flexibility and now we have a new way to format dates in SQL Server.
With the SQL Server FORMAT function we do not need to know the format number to use to get the right date format we want, we can just specify the display format we want and we get that format.
SQL Date Format with the FORMAT function
- Use the FORMAT function to format the date and time data types from a date column (date, datetime, datetime2, smalldatetime, datetimeoffset, etc. data type) in a table or a variable such as GETDATE()
- To get DD/MM/YYYY use SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date
- To get MM-DD-YY use SELECT FORMAT (getdate(), 'MM-dd-yy') as date
- Check out more examples below
The syntax of the SQL Server FORMAT function is the following:
FORMAT (value,format[,culture]) GO
SQL Server FORMAT Examples for Formatting Dates
Let's start with an example:
SELECT FORMAT (getdate(), 'dd-MM-yy') as date GO
The format will be as follows:
- dd - day number from 01-31
- MM - month number from 01-12
- yy - two digit year number
If this was run for March 21, 2021 the output would be: 21-03-21.
Let's try another one:
SELECT FORMAT (getdate(), 'hh:mm:ss') as time GO
The format will be as follows:
- hh - hour of day from 01-12
- mm - minutes of hour from 00-59
- ss - seconds of minute from 00-59
The output will be: 02:48:42.
SQL Server Date FORMAT output examples
Below is a list of date and datetime formats with an example of the output. The current date used for all of these examples is "2021-03-21 11:36:14.840".
|SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date||21/03/2021|
|SELECT FORMAT (getdate(), 'dd/MM/yyyy, hh:mm:ss ') as date||21/03/2021, 11:36:14|
|SELECT FORMAT (getdate(), 'dddd, MMMM, yyyy') as date||Wednesday, March, 2021|
|SELECT FORMAT (getdate(), 'MMM dd yyyy') as date||Mar 21 2021|
|SELECT FORMAT (getdate(), 'MM.dd.yy') as date||03.21.21|
|SELECT FORMAT (getdate(), 'MM-dd-yy') as date||03-21-21|
|SELECT FORMAT (getdate(), 'hh:mm:ss tt') as date||11:36:14 AM|
|SELECT FORMAT (getdate(), 'd','us') as date||03/21/2021|
|SELECT FORMAT (getdate(), 'yyyy-MM-dd hh:mm:ss tt') as date||2021-03-21 11:36:14 AM|
|SELECT FORMAT (getdate(), 'yyyy.MM.dd hh:mm:ss t') as date||2021.03.21 11:36:14 A|
|SELECT FORMAT (getdate(), 'dddd, MMMM, yyyy','es-es') as date --Spanish||domingo, marzo, 2021|
|SELECT FORMAT (getdate(), 'dddd dd, MMMM, yyyy','ja-jp') as date --Japanese||日曜日 21, 3月, 2021|
As you can see, we used a lot of options for the date and time formatting, which are listed below.
- dd - this is day of month from 01-31
- dddd - this is the day spelled out
- MM - this is the month number from 01-12
- MMM - month name abbreviated
- MMMM - this is the month spelled out
- yy - this is the year with two digits
- yyyy - this is the year with four digits
- hh - this is the hour from 01-12
- HH - this is the hour from 00-23
- mm - this is the minute from 00-59
- ss - this is the second from 00-59
- tt - this shows either AM or PM
- d - this is day of month from 1-31 (if this is used on its own it will display the entire date)
- us - this shows the date using the US culture which is MM/DD/YYYY
For all the different custom date and time format strings to use with the SQL Server FORMAT command, check out this list.
SQL Server Date FORMAT with Culture
Another option for the FORMAT function is culture. With the culture option you can obtain regional formatting. Here is a list of culture codes to use with FORMAT.
For example in the USA, the format would be like:
SELECT FORMAT (getdate(), 'd', 'en-us') as date GO
In the USA the format is month, day, year.
If this was run for March 21, 2018 the output would be: 3/21/2018
Another example where we will use the Spanish culture in Bolivia (es-bo):
SELECT FORMAT (getdate(), 'd', 'es-bo') as date GO
In Bolivia the format is day, month, year.
If this was run for March 21, 2021 the output would be: 21/03/2021.
The following table contains different examples for different cultures for October 11, 2021:
|English-USA||SELECT FORMAT (getdate(), 'd', 'en-US') as date||10/11/2021|
|French-France||SELECT FORMAT (getdate(), 'd', 'fr-FR') as date||11/10/2021|
|Armenian-Armenian||SELECT FORMAT (getdate(), 'd', 'hy-AM') as date||11.10.2021|
|Bosnian Latin||SELECT FORMAT (getdate(), 'd', 'bs-Latn-BA') as date||11. 10. 2021.|
|Simplified Chinese||SELECT FORMAT (getdate(), 'd', 'zh-CN') as date||2021/10/11|
|Danish - Denmark||SELECT FORMAT (getdate(), 'MM.dd.yy') as date||11-10-2021|
|Dari - Afghanistan||SELECT FORMAT (getdate(), 'd', 'prs-AF') as date||1400/7/19|
|Divehi - Maldives||SELECT FORMAT (getdate(), 'd', 'dv-MV') as date||11/10/21|
|French - Belgium||SELECT FORMAT (getdate(), 'd', 'fr-BE') as date||11-10-21|
|French - Canada||SELECT FORMAT (getdate(), 'd', 'fr-CA') as date||2021-10-11|
|Hungarian - Hungary||SELECT FORMAT (getdate(), 'd', 'hu-HU') as date||2021. 10. 11.|
|isiXhosa / Xhosa - South Africa||SELECT FORMAT (getdate(), 'd', 'xh-ZA') as date||2021-10-11|
For a complete list of possible languages, refer to the following link:
SQL Format Number Examples
The format also allows to format numbers according to the culture. The following table will show different examples.
|Currency-English-USA||SELECT FORMAT(200.36, 'C', 'en-us') AS 'Currency Format'||$200.36|
|Currency-Germany||SELECT FORMAT(200.36, 'C', 'de-DE') AS 'Currency Format'||200,36 €|
|Currency-Japan||SELECT FORMAT(200.36, 'C', 'ja-JP') AS 'Currency Format'||¥200|
|General Format||SELECT FORMAT(200.3625, 'G', 'en-us') AS 'Format'||200.3625|
|Numeric Format||SELECT FORMAT(200.3625, 'N', 'en-us') AS 'Format'||200.36|
|Numeric 3 decimals||SELECT FORMAT(11.0, 'N3', 'EN-US') AS 'Format'||11.000|
|Decimal||SELECT FORMAT(12, 'D', 'en-us') AS 'Format'||12|
|Decimal 4||SELECT FORMAT(12, 'D4', 'en-us') AS 'Format'||0012|
|Exponential||SELECT FORMAT(120, 'E', 'EN-US') AS 'Format'||1.200000E+002|
|Percent||SELECT FORMAT(0.25, 'P', 'EN-US') AS 'Format'||25.00%|
|Hexadecimal||SELECT FORMAT(11, 'X', 'EN-US') AS 'Format'||B|
In this article, we saw different examples to change the output for different formats in an MS SQL database.
Note: The FORMAT function uses Common Language Runtime (CLR) and there have been noticeable performance differences between other approaches (CONVERT Function, CAST Function, etc.) showing that FORMAT is much slower.
- You can now work with a more flexible and intuitive function to handle date formats.
- Here is more information on the FORMAT function
- Learn about more SQL Server Functions for date values:
- SELECT CONVERT Examples
- SQL Server Date and Time Functions with Examples
- DATEDIFF SQL Server Function
- Determine SQL Server Date and Time Parts with DATEPART and DATENAME Functions
- Add and Subtract Dates using DATEADD in SQL Server
- Update only Year, Month or Day in a SQL Server Date
- SQL Convert Date to YYYY-MM-DD HH:MM:SS
- Compare SQL Server Features - Decimal vs Numeric, Timestamp vs Rowversion, Unique Index vs Unique Constraint
- New Date and Time Value Functions in SQL Server 2012
About the author
View all my tips
Article Last Updated: 2021-10-13