The most difficult part when working with dates in an SQL server be sure that the format of the date we are trying to insert, matches the format of the date column in the database.
In this article, we will briefly explore various types of SQL Convert Date formats to use in writing SQL queries.
You need to work with date-type data in SQL. It can be a complicated thing to deal with, at times, for SQL Server developers. Suppose we have a BillingTransection table with a column timestamp. It creates a timestamp for each guest order. You might face the following issues with it.
- We fail to insert data in the BillingTransection table because the application tries to insert a different data date format.
- Suppose we have data in a table in the format YYYY-MM-DD hh:mm: ss. We have a daily Transaction report, and in that, we want data group by date. We want to have data in the report in the format YYYY-MM-DD
You have faced many such scenarios when you do not have a date format as per our requirements. You cannot change table properties to satisfy each requirement. In this case, you need to use the built-in functions in SQL Server to give the required date format.
Data Types for Date and Time in SQL
SQL Server comes with the given below data types for storing a date or a date/time value in the database:
- DATE - format YYYY-MM-DD
- Time - format hh:mm:ss[.nnnnnnn]
- DATETIME - format: YYYY-MM-DD HH:MI:SS
- SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS
- TIMESTAMP - format: a unique number
- DateTime2 -YYYY-MM-DD hh:mm:ss[.nnnnnnn]
- DateTimeOffset -format YYYY-MM-DD hh:mm:ss[.nnnnnnn] [+|-]hh:mm
In SQL Server, you have used built-in functions like SQL GETDATE() and GetUTCDate() to provide server date and format in various formats.
- SYSDATETIME()SYSDATETIME(): To return the server’s date and time
- SYSDATETIMEOffset(): It returns the server’s date and time, along with UTC offset
- GETUTCDATE(): It returns the date and GMT (Greenwich Mean Time ) time
- GETDATE(): It returns the server date and time
Execute the given below queries to get output in respective formats
- Select SYSDATETIME() as [SYSDATETIME]
- Select SYSDATETIMEOffset() as [SYSDATETIMEOffset]
- Select GETUTCDATE() as [GETUTCDATE]
- Select GETDATE() as [GETDATE]
SQL Convert Date Formats
As highlighted earlier, you might need to format a date in different formats as per our requirements. You can use the SQL CONVERT() function in SQL Server to format DateTime in various formats.
The syntax for the SQL: CONVERT() function is as given below.
SELECT CONVERT (DataType(length)),Date, DateFormatCode)
- DataType: You need to define data type along with length. In the date function, use Varchar(length) data types
- Date: You need to specify the date that you want to convert
- DateFormatCode: You need to specify DateFormatCode to convert a date into an appropriate form. You will explore more on this in the upcoming section
First, You declare a variable to hold the current DateTime using the SQL GETDATE() function with the given below query.
DECLARE @DemoDate DATETIME
SET @DemoDate=GETDATE()
PRINT @DemoDate
You can see various date formats in the given table. You can keep this table handy for reference purposes in the format of Date Time columns.
Date and Time Formats | SQL convert date query | Output |
Datetime format as | DECLARE @DemoDate DATETIME | MM/DD/YY |
Datetime format in | DECLARE @DemoDate DATETIME
| YY.MM.DD |
Datetime format in | DECLARE @DemoDate DATETIME | DD/MM/YY 23/04/22 |
Datetime format in DD.MM.YY format | DECLARE @DemoDate DATETIME | DD.MM.YY |
Datetime format in | DECLARE @DemoDate DATETIME
| DD-MM-YY |
Datetime format in | DECLARE @DemoDate DATETIME | DD MMM YY |
Datetime format in | DECLARE @DemoDate DATETIME | MMM DD,YY |
Datetime Format | DECLARE @DemoDate DATETIME | hh:mm:ss |
Datetime format as | DECLARE @DemoDate DATETIME | MMM DD YYYY hh:mm:ss:mmm(AM/PM) |
Datetime format in | DECLARE @DemoDate DATETIME | MM-DD-YY |
Datetime format in | DECLARE @DemoDate DATETIME
| YY/MM/DD 22/04/23 |
Datetime format in | DECLARE @DemoDate DATETIME | YYMMDD |
Datetime format in | DECLARE @DemoDate DATETIME | DD MMM YYYY HH:MM:SS:MMM |
Datetime format in | DECLARE @DemoDate DATETIME | HH:MM:SS:MMM |
Datetime format in | DECLARE @DemoDate DATETIME | HH:MM:SS:MMM |
Datetime format in | DECLARE @DemoDate DATETIME
| YYYY-MM-DD HH:MM:SS.mmm |
Datetime format in | DECLARE @DemoDate DATETIME | mm/dd/yy hh:mm:ss (AM/PM) |
Datetime format in | DECLARE @DemoDate DATETIME | yyyy-mm-dd |
Datetime format in | DECLARE @DemoDate DATETIME | hh:mm:ss |
Datetime format in [MMM DD YYYY HH: SS (AM/PM)] Standard: Default |
| MMM DD YYYY HH:SS (AM/PM) |
Datetime format in | DECLARE @DemoDate DATETIME | MM/DD/YYYY |
Datetime format in | DECLARE @DemoDate DATETIME | YYYY.MM.DD |
Datetime format in |
| DD/MM/YYYY |
Datetime format in | DECLARE @DemoDate DATETIME | DD/MM/YYYY |
Datetime format in | DECLARE @DemoDate DATETIME | DD/MM/YYYY |
Datetime format in | DECLARE @DemoDate DATETIME | DD MMM YYYY |
Datetime format in | DECLARE @DemoDate DATETIME | MMM DD,YYYY |
Datetime Format | DECLARE @DemoDate DATETIME | HH:MM:SS |
Datetime format as | DECLARE @DemoDate DATETIME
| MMM DD YYYY hh:mm:ss:mmm(AM/PM) |
Datetime format in | DECLARE @DemoDate DATETIME | MM-DD-YYYY |
Datetime format in | DECLARE @DemoDate DATETIME | YYYY/MM/DD |
Datetime format in | DECLARE @DemoDate DATETIME | YYYYMMDD |
Datetime format in | DECLARE @DemoDate DATETIME | DD MMM YYYY HH:MM:SS:MMM |
Datetime format in |
| DD MMM YYYY HH:MM:SS:MMM |
Datetime format in | DECLARE @DemoDate DATETIME | YYYY-MM-DD HH:MM:SS |
Datetime format in | DECLARE @DemoDate DATETIME | yyyy-mm-ddThh:mi:ss.mmm |
Datetime format in | DECLARE @DemoDate DATETIME | mm/dd/yy hh:mm:ss (AM/PM) |
Datetime format in | DECLARE @DemoDate DATETIME | dd mon yyyy hh:mi:ss:mmm(AM/PM) ?? 1443 9:50:57:340PM |
In the given table, you can see various formats to SQL Server convert date as per our requirements. In the given below table, we can see all SQL date formats together.
Date format option | SQL convert date output |
0 | Dec 30 2006 12:38AM |
1 | 12/30/06 |
2 | 06.12.30 |
3 | 30/12/2006 |
4 | 30.12.06 |
5 | 30/12/2006 |
6 | 30-Dec-06 |
7 | Dec 30, 06 |
8 | 00:38:54 |
9 | Dec 30 2006 12:38:54:840AM |
10 | 12-30-06 |
11 | 06/12/1930 |
12 | 61230 |
13 | 30 Dec 2006 00:38:54:840 |
14 | 00:38:54:840 |
20 | 30/12/2006 00:38 |
21 | 38:54.8 |
22 | 12/30/06 12:38:54 AM |
23 | 30/12/2006 |
24 | 00:38:54 |
25 | 38:54.8 |
26 | 2006-30-12 00:38:54.840 |
27 | 12-30-2006 00:38:54.840 |
28 | 12-2006-30 00:38:54.840 |
29 | 38:54.8 |
30 | 30-2006-12 00:38:54.840 |
31 | 2006-30-12 |
32 | 12-30-2006 |
33 | 12-2006-30 |
34 | 30/12/2006 |
35 | 30-2006-12 |
100 | Dec 30 2006 12:38AM |
101 | 12/30/2006 |
102 | 2006.12.30 |
103 | 30/12/2006 |
104 | 30.12.2006 |
105 | 30/12/2006 |
106 | 30-Dec-06 |
107 | Dec 30, 2006 |
108 | 00:38:54 |
109 | Dec 30 2006 12:38:54:840AM |
110 | 12-30-2006 |
111 | 30/12/2006 |
112 | 20061230 |
113 | 30 Dec 2006 00:38:54:840 |
114 | 00:38:54:840 |
120 | 30/12/2006 00:38 |
121 | 38:54.8 |
126 | 2006-12-30T00:38:54.840 |
127 | 2006-12-30T00:38:54.840 |
130 | 10 ?? ????? 1427 12:38:54:840A |
131 | 10/12/1427 12:38:54:840AM |
DATEADD
You can use the SQL DATEADD function to add a particular period to your date. Suppose you have a requirement to add 1 month to the current date. You can use the SQL DATEADD function to do this task.
The syntax for SQL DATEADD function
DATEADD(interval, number, date)
- Interval: You can specify an interval that needs to be added to the specified date. You can have values such as year, quarter, month, day, week, hour, minute, etc.
- Number: It specifies the number of intervals to add. For example, if you have a specified interval as Month and Number as 2, it means 2 months need to be added to the date.
.In the given below query, you want to add 2 months to the current date.
SELECT GETDATE() AS TodayDate
SELECT DATEADD(MONTH, 2, GETDATE()) AS TwoMonthAddedDate;
You can see the output as given below.
Similarly, lets us add 1 year to the current date using the given query.
SELECT GETDATE() AS TodayDate
SELECT DATEADD(YEAR, 1, GETDATE()) AS OneYearAddedYearDate;
You can combine the SQL DATEADD and CONVERT functions to get output in desired DateTime formats. Suppose, in the previous example; you want a date format in of DD, YYYY. You can use format code 107 to get output in this format like aen below
SELECT DATEADD(YEAR,1,GETDATE()) AS TodayDateTime
,CONVERT(varchar(110),DATEADD(YEAR,1,GETDATE()),107) AS ConvertedDateTime