SQL SET DATEFORMAT

The SQL SET DATEFORMAT is one of the Set Functions, which will set the Order of the Date: Month, Year, Day. This Statement supports formats like dmy, mdy, ymd, ydm, and dym where d = Date, m = month, and y = Year.

SQL DATEFORMAT Syntax

The basic syntax of the DATEFORMAT in SQL Server is as shown below:

SET DATEFORMAT DateFormat_Name

-- For example,
SET DATEFORMAT dmy;

SET DATEFORMAT in SQL Example

In this example, we will show how the SQL Server DATEFORMAT will affect the Date. Remember, SET LANGUAGE will implicitly change the date format based on the Language you select. But, SET DATEFORMAT will override that format.

-- Setting the Format as date/Month/Year
SET DATEFORMAT dmy
DECLARE @dt DATETIME2 = '01-02-2016 12:03:28.000'
SELECT @dt AS 'date from DMY Format'

DECLARE @dt1 DATETIME2 = '02/05/2016 12:03:28.000'
SELECT @dt1 AS 'date from DMY Format'

-- Setting the format as Month/date/year
SET DATEFORMAT mdy
DECLARE @dt2 DATETIME2 = '01-02-2016 12:03:28.000'
SELECT @dt2 AS 'date from MDY Format'
SQL DATEFORMAT Example