Format SQL Server Dates with FORMAT Function

Problem

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.

Solution

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

  1. 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()
  2. To get DD/MM/YYYY use SELECT FORMAT (getdate(), ‘dd/MM/yyyy ‘) as date
  3. To get MM-DD-YY use SELECT FORMAT (getdate(), ‘MM-dd-yy’) as date
  4. 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”.

QuerySample output
SELECT FORMAT (getdate(), ‘dd/MM/yyyy ‘) as date21/03/2021
SELECT FORMAT (getdate(), ‘dd/MM/yyyy, hh:mm:ss ‘) as date21/03/2021, 11:36:14
SELECT FORMAT (getdate(), ‘dddd, MMMM, yyyy’) as dateWednesday, March, 2021
SELECT FORMAT (getdate(), ‘MMM dd yyyy’) as dateMar 21 2021
SELECT FORMAT (getdate(), ‘MM.dd.yy’) as date03.21.21
SELECT FORMAT (getdate(), ‘MM-dd-yy’) as date03-21-21
SELECT FORMAT (getdate(), ‘hh:mm:ss tt’) as date11:36:14 AM
SELECT FORMAT (getdate(), ‘d’,’us’) as date03/21/2021
SELECT FORMAT (getdate(), ‘yyyy-MM-dd hh:mm:ss tt’) as date2021-03-21 11:36:14 AM
SELECT FORMAT (getdate(), ‘yyyy.MM.dd hh:mm:ss t’) as date2021.03.21 11:36:14 A
SELECT FORMAT (getdate(), ‘dddd, MMMM, yyyy’,’es-es’) as date –Spanishdomingo, marzo, 2021
SELECT FORMAT (getdate(), ‘dddd dd, MMMM, yyyy’,’ja-jp’) as date
–Japanese
日曜日 21, 3月, 2021
SELECT FORMAT (getdate(), ‘MM-dd-yyyy ‘) as date03-21-2021
SELECT FORMAT (getdate(), ‘MM dd yyyy ‘) as date03 21 2021
SELECT FORMAT (getdate(), ‘yyyyMMdd’) as date20231011
SELECT FORMAT (getdate(), ‘HH:mm:dd’) as time11:36:14
SELECT FORMAT (getdate(), ‘HH:mm:dd.ffffff’) as time11:36:14.84000

SQL Date Format Legend

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.

Below we show examples of how to get different formats using these options:

  • dd – this is day of month from 01-31
  • MM – this is the month number from 01-12
  • yyyy – this is the year with four digits

SQL date format dd/MM/yyyy with SQL FORMAT

The following example shows how to get a dd/MM/yyyy date format, such as 30/04/2008 for April 4, 2008:

--MSSQLTips.com 
 
SELECT 
    CurrencyCode,
    Name,
    FORMAT(ModifiedDate, 'dd/MM/yyyy') AS FormattedDate
FROM 
    Sales.Currency;

The output is the following:

SQL date format dd/MM/yyyy with SQL FORMAT query results

Format date SQL MM/dd/yyyy with SQL FORMAT

The following example shows how to get a MM/dd/yyyy date format, such as 04/30/2008 for April 4, 2008:

--MSSQLTips.com 
 
SELECT 
    CurrencyCode,
    Name,
    FORMAT(ModifiedDate, 'MM/dd/yyyy') AS FormattedDate
FROM 
    Sales.Currency;

The output is the following:

Format date SQL MM/dd/yyyy with SQL FORMAT query results

SQL date format yyyy MM dd with SQL FORMAT

If we want to change to the yyyy MM dd FORMAT using the format function, the following example can help you to do it.

--MSSQLTips.com
 
SELECT 
    CurrencyCode,
    Name,
    FORMAT(ModifiedDate, 'yyyy MM dd') AS FormattedDate
FROM 
    Sales.Currency;

SQL date format yyyyMMdd with SQL FORMAT

The yyyyMMdd format is also a commonly used format to store data in the database, for software development comparisons, financial systems, etc.

The following example, shows how to use this format.

--MSSQLTips.com 
 
SELECT 
    CurrencyCode,
    Name,
    FORMAT(ModifiedDate, 'yyyyMMdd') AS FormattedDate
FROM 
    Sales.Currency;

The output of the query is the following:

SQL date format yyyyMMdd

SQL format date ddMMyyyy with SQL FORMAT

The ddMMyyyy format is common in countries like England, Ireland, Australia, New Zealand, Nepal, Malasia, Hong Kong, Qatar, Arabia Saudi and several other countries.

The following example shows how to use it.

--MSSQLTips.com 
SELECT 
    CurrencyCode,
    Name,
    FORMAT(ModifiedDate, 'ddMMyyyy') AS FormattedDate
FROM 
    Sales.Currency;

The output of the query is the following:

SQL format date ddMMyyyy

SQL format date yyyy-MM-dd with SQL FORMAT

The yyyy-MM-dd format is commonly used in USA, Canada, Mexico, Central America and other countries.

The following example shows how to use this format:

--MSSQLTips.com 
 
SELECT 
    CurrencyCode,
    Name,
    FORMAT(ModifiedDate, 'yyyy-MM-dd') AS FormattedDate
FROM 
    Sales.Currency;

The output of this format is the following:

SQL format date yyyy-MM-dd

The next example will create a view with the yyyy-MM-dd format.

--MSSQLTips.com 
 
CREATE VIEW dbo.CurrencyView
AS
SELECT
    CurrencyCode,
    Name,
    FORMAT(ModifiedDate, 'yyyy-MM-dd') AS FormattedDate
FROM
    Sales.Currency;

The output is the following:

SELECT * FROM dbo.CurrencyView;
yyyy-MM-dd date format final results

There are some considerations that you must take with the yyyy-mm-dd format.

SQL date format MM/dd/yyyy with SQL FORMAT

Finally, we have the MM/dd/yyyy commonly used in USA, Canada, Mexico, Central America and other countries.

The following example shows how to use it:

--MSSQLTips.com 
 
SELECT 
    CurrencyCode,
    Name,
    FORMAT(ModifiedDate, 'MM/dd/yyyy') AS FormattedDate
FROM 
    Sales.Currency;

The format displayed is the following:

SQL Server date format MM/dd/yyyy query results

SQL Server FORMAT Date Data Type Examples

The following shows how to format dates to mimic with different data types like Date, DateTime, DateTime2, smalldatetime, and time.

Data TypeQuerySample Output
DateSELECT FORMAT(GetDate(), ‘yyyy-MM-dd’) AS Date;2021-03-21
DateTimeSELECT FORMAT(GetDate(), ‘yyyy-MM-dd HH:mm:ss.fff’) AS Date;2021-03-21 19:36:22.640
SmallDateTimeSELECT FORMAT( CASE WHEN DATEPART(SECOND, GetDate()) >= 30 THEN DATEADD(MINUTE, 1, GetDate()) ELSE GetDate() END, ‘yyyy-MM-dd HH:mm:00’ ) AS Date; — this rounds up to the nearest minute2021-03-21 19:44:00
DateTime2SELECT FORMAT(SYSDATETIME(), ‘yyyy-MM-dd HH:mm:ss.fffffff’) AS Date;2021-03-21 19:47:15.2734243
TimeSELECT FORMAT(SYSDATETIME(), ‘HH:mm:ss.fffffff’) AS Date;19:55:58.5134326

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, 2021 the output would be: 3/21/2021

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:

CultureQuerySample output
English-USASELECT FORMAT (getdate(), ‘d’, ‘en-US’) as date10/11/2021
French-FranceSELECT FORMAT (getdate(), ‘d’, ‘fr-FR’) as date11/10/2021
Armenian-ArmenianSELECT FORMAT (getdate(), ‘d’, ‘hy-AM’) as date11.10.2021
Bosnian LatinSELECT FORMAT (getdate(), ‘d’, ‘bs-Latn-BA’) as date11. 10. 2021.
Simplified ChineseSELECT FORMAT (getdate(), ‘d’, ‘zh-CN’) as date2021/10/11
Danish – DenmarkSELECT FORMAT (getdate(), ‘MM.dd.yy’) as date11-10-2021
Dari – AfghanistanSELECT FORMAT (getdate(), ‘d’, ‘prs-AF’) as date1400/7/19
Divehi – MaldivesSELECT FORMAT (getdate(), ‘d’, ‘dv-MV’) as date11/10/21
French – BelgiumSELECT FORMAT (getdate(), ‘d’, ‘fr-BE’) as date11-10-21
French – CanadaSELECT FORMAT (getdate(), ‘d’, ‘fr-CA’) as date2021-10-11
Hungarian – HungarySELECT FORMAT (getdate(), ‘d’, ‘hu-HU’) as date2021. 10. 11.
isiXhosa / Xhosa – South AfricaSELECT FORMAT (getdate(), ‘d’, ‘xh-ZA’) as date2021-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.

FormatQuerySample output
Currency-English-USASELECT FORMAT(200.36, ‘C’, ‘en-us’) AS ‘Currency Format’$200.36
Currency-GermanySELECT FORMAT(200.36, ‘C’, ‘de-DE’) AS ‘Currency Format’200,36 €
Currency-JapanSELECT FORMAT(200.36, ‘C’, ‘ja-JP’) AS ‘Currency Format’¥200
General FormatSELECT FORMAT(200.3625, ‘G’, ‘en-us’) AS ‘Format’200.3625
Numeric FormatSELECT FORMAT(200.3625, ‘N’, ‘en-us’) AS ‘Format’200.36
Numeric 3 decimalsSELECT FORMAT(11.0, ‘N3’, ‘EN-US’) AS ‘Format’11.000
DecimalSELECT FORMAT(12, ‘D’, ‘en-us’) AS ‘Format’12
Decimal 4SELECT FORMAT(12, ‘D4’, ‘en-us’) AS ‘Format’0012
ExponentialSELECT FORMAT(120, ‘E’, ‘EN-US’) AS ‘Format’1.200000E+002
PercentSELECT FORMAT(0.25, ‘P’, ‘EN-US’) AS ‘Format’25.00%
HexadecimalSELECT FORMAT(11, ‘X’, ‘EN-US’) AS ‘Format’B

Performance Comparison of SQL FORMAT versus SQL CONVERT

To compare the performance, let’s use the Convert and Format function to format a column [TransactionDate] from the sample table PerformanceReviewTest. This table contains 3,472,459 records.

I have enabled Statistics IO and Time to get the query running statistics. The queries below insert data into the temporary tables using a SELECT statement that utilizes FORMAT and CONVERT functions separately.

SET NOCOUNT ON;
SET STATISTICS IO,TIME ON;
 
-- CONVERT test
SELECT CONVERT(VARCHAR(10), TranactionDate, 120)  AS ConvertedDate
INTO #tmp1_PerformanceReviewTest
FROM dbo.PerformanceReviewTest;
 
-- FORMAT test
SELECT FORMAT(TranactionDate, 'yyyy-MM-dd') as FormattedDate
INTO #tmp2_PerformanceReviewTest
FROM dbo.PerformanceReviewTest;
compare convert and format

Comparison of SQL CONVERT and SQL FORMAT

Below is a comparison of the two different approaches.

FunctionCPU Time (ms)Elapsed Time (ms)Logical ReadsScan CountNotes
CONVERT()3,7979737,8255Native and efficient
FORMAT()15,1104,4467,8255~4× CPU, 4.5× elapsed time

As demonstrated in the table above, the CONVERT() function has lower CPU and elapsed time compared to the FORMAT() function. Therefore, for optimal query performance, particularly when handling large datasets, you should not use the FORMAT function unless there is a specific requirement.

You should avoid using FORMAT() in the following cases:

  • Queries dealing with large datasets
  • Using the function inside UDFs or stored procedures that are called frequently
  • Real-time or transactional queries that are optimized for performance.

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.

Frequently Asked Questions

My date data is stored as (dd.mm.yyyy), how can I display as mm/dd/yyyy with SQL FORMAT?

Use this code:

SELECT FORMAT(yourdate, ‘MM/dd/yyyy’) as ‘yourdate’ FROM yourtable;

I want to display only hours and minute for the SQL date, what’s the SQL FORMAT code?

Use this code:

declare @yourdate datetime = ‘2026-06-05 22:30:25’;
select FORMAT(@yourdate, ‘HH:mm’) as ‘yourdate’;

How can I show the time with AM or PM format with the SQL FORMAT function?

Use this code:

SELECT FORMAT(yourdate, ‘MM/dd/yyyy hh:mm:ss tt’) AS AMPM FROM yourtable;    

What is the difference between the SQL CONVERT and SQL FORMAT function?

SQL CONVERT is an old function used to convert dates in different formats. It is available in SQL Server 2000 and later versions. FORMAT was introduced in SQL Server 2012. It uses .NET CLR technology. It is slower, but more friendly. For big queries with a lot of data, it is better to use the SQL CONVERT function.  FORMAT is recommended for small data sets. For more information about the report function, refer to this link: SQL Convert Function In Depth.

Why is month not displayed correctly when I use the SQL FORMAT function with the mm parameter?


SELECT FORMAT (GETDATE(), ‘dd/mm/yyyyy’) as date;

The correct SQL is the following using uppercase MM. The lowercase mm is used to display minutes.

SELECT FORMAT (GETDATE(), ‘dd/MM/yyyy’) as date;

How do I show the USD currency sign with SQL FORMAT?

SELECT FORMAT(200.36, ‘C’, ‘en-us’) AS ‘Currency Format’;

How do I change the currency format to the Canadian format with SQL FORMAT?

SELECT FORMAT(200.36, ‘C’, ‘en-ca’) AS ‘Currency Format’;

How can I have 4 decimals for my number with the SQL FORMAT function?

Use the keyword N4 for 4 decimals. Here is sample code:

SELECT FORMAT(100, ‘N4’) AS Format;

How can I convert a number to hexadecimal format using the SQL FORMAT function?

The following example converts the number 100 to the hexadecimal format.

SELECT FORMAT(100, ‘X’) AS ‘Format’;

How can I convert a number to binary format using the SQL FORMAT function?

There is no Binary option with the format function. You would need to use the CONVERT function instead.

SELECT CONVERT(VARBINARY(4), 100) AS BinaryData;

Is the performance of the SQL FORMAT function really slow?

Yes, it is not recommended for large datasets because it is not a native function and uses the CLR functionality.

How can I escape reserved words like y and M in the SQL FORMAT function?

You can use a \ to escape the parameters as shown below.
 
SELECT FORMAT(GETDATE(), ‘yyyy \ye\ar, MM \mon\t\h’) AS mydate;

Key Takeaways

  • SQL Server 2012 introduced the FORMAT function, offering more flexibility than the previous CONVERT function for date formatting.
  • The FORMAT function allows you to specify desired date formats directly instead of using format numbers.
  • Examples using FORMAT include getting date outputs like ‘dd/MM/yyyy’ or ‘MM-dd-yy’.
  • Performance comparisons show that FORMAT is slower than CONVERT, especially with large datasets, and should be used cautiously.
  • Utilize FORMAT for intuitive date handling, but prefer CONVERT for optimal performance in large or transactional queries.

Next Steps

Last updated by Rajendra Gupta 2025-10-31

9 Comments

  1. Hi Jeff, thanks for your continued education on the use of FORMAT. There is a statement in the article that lets people know this is an issue. I agree that if this is used against millions of rows there will be a big performance hit and that there are other options in SQL Server as the article points out.

    Thanks
    Greg

  2. They keep republishing this article without any warnings about how the use of FORMAT will make the code at least 20 times slower than using CONVERT or any of another myriad ways to format dates. Caveat Emptor, folks!

  3. Here is a list of the commands I use on a regular basis to convert dates and times.

    /*
    Formatting dates and times

    ctrdateinfodescr
    100Nov 17 2021 10:23AMconvert(varchar(50),getdate(),100)
    10111/17/2021convert(varchar(50),getdate(),101)
    1022021.11.17convert(varchar(50),getdate(),102)
    10317/11/2021convert(varchar(50),getdate(),103)
    10417.11.2021convert(varchar(50),getdate(),104)
    10517-11-2021convert(varchar(50),getdate(),105)
    10617 Nov 2021convert(varchar(50),getdate(),106)
    107Nov 17, 2021convert(varchar(50),getdate(),107)
    10810:23:43convert(varchar(50),getdate(),108)
    109Nov 17 2021 10:23:43:110AMconvert(varchar(50),getdate(),109)
    11011-17-2021convert(varchar(50),getdate(),110)
    1112021/11/17convert(varchar(50),getdate(),111)
    11220211117convert(varchar(50),getdate(),112)
    11317 Nov 2021 10:23:43:110convert(varchar(50),getdate(),113)
    11410:23:43:110convert(varchar(50),getdate(),114)
    1202021-11-17 10:23:43convert(varchar(50),getdate(),120)
    1212021-11-17 10:23:43.110convert(varchar(50),getdate(),121)
    1262021-11-17T10:23:43.110convert(varchar(50),getdate(),126)
    13012 ???? ?????? 1443 10:23:43:110AMconvert(varchar(50),getdate(),130)
    13112/04/1443 10:23:43:110AMconvert(varchar(50),getdate(),131)
    020211117102343replace(replace(replace(convert(varchar(50),getdate(),120),’ ‘,”),’-‘,”),’:’,”)
    11023replace(convert(varchar(5),getdate(),108),’:’,”)

    */
    declare @ctr int
    select @ctr = 100
    create table #temp (ctr int, dateinfo varchar(50), descr varchar(100))

    while @ctr < 115
    begin
    insert into #temp
    select @ctr as ‘Counter’, convert(varchar(50),getdate(),@ctr), ‘convert(varchar(50),getdate(),’+convert(varchar(3),@ctr)+’)’
    select @ctr = @ctr + 1
    end
    select @ctr = 120
    while @ctr < 122
    begin
    insert into #temp
    select @ctr as ‘Counter’, convert(varchar(50),getdate(),@ctr), ‘convert(varchar(50),getdate(),’+convert(varchar(3),@ctr)+’)’
    select @ctr = @ctr + 1
    end
    select @ctr = 126
    insert into #temp
    select @ctr as ‘Counter’, convert(varchar(50),getdate(),@ctr), ‘convert(varchar(50),getdate(),’+convert(varchar(3),@ctr)+’)’
    select @ctr = 130
    insert into #temp
    select 130 as ‘Counter’, convert(varchar(50),getdate(),130), ‘convert(varchar(50),getdate(),’+convert(varchar(3),@ctr)+’)’
    select @ctr = 131
    insert into #temp
    select 131 as ‘Counter’, convert(varchar(50),getdate(),131), ‘convert(varchar(50),getdate(),’+convert(varchar(3),@ctr)+’)’
    select @ctr = 120
    insert into #temp
    select 0, replace(replace(replace(convert(varchar(50),getdate(),120),’ ‘,”),’-‘,”),’:’,”),’replace(replace(replace(convert(varchar(50),getdate(),120),” ”,””),”-”,””),”:”,””)’
    select @ctr = 108
    insert into #temp
    select 1 as ‘Counter’, replace(convert(varchar(5),getdate(),108),’:’,”), ‘replace(convert(varchar(5),getdate(),108),”:”,””)’

    select * from #temp

    drop table #temp

  4. Can you update your second date sample? SELECT FORMAT (getdate(), ‘dd/MM/yyyy, hh:mm:ss ‘) as date for 2 PM would return 02:00:00. It needs either HH for hours or tt for AM/PM, preferably HH since no other examples have it.

  5. @Greg,

    I might be missing it but, if I am, others will miss it, as well.

    You said back in December of 2018 that you would add a warning (and it should be a significant warning) about how comparatively terrible FORMAT is for just about everything even compared to relative complex CONVERT and SUBSTRING (etc) functionality.

    Some of the Developers that work with me have referenced this very article and I’m constantly having to prove to people that FORMAT should generally be avoided.

    Can you add the performance warning in nice bold letters at the beginning of this article, please? Thank you.

  6. Hi.
    How to display timebetween, eg. 1. january 06:23PM until 3. january 09:34PM
    I would like output to be in
    <number of days> – <hh:mm:ss>
    2 – 03:11:00
    is that posible?

Leave a Reply

Your email address will not be published. Required fields are marked *