Quick Sheet – Date Time Formats

My quick reference for TSQL date/time formats that I use often.

More information may be found at: https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-ver16

PARTFORMATTSQL CODENOTEOUTPUT
PARSEparseSELECT PARSE(’02/12/2012′ AS datetime USING ‘de-DE’);2012-12-02 00:00:00.000
try_parseSELECT TRY_PARSE(’02/12/20122′ AS datetime USING ‘en-US’);NULL
First Day of WeekSELECT DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6);current2023-11-12 00:00:00.000
Last Day of WeekSELECT DATEADD(wk, DATEDIFF(wk, 6, CURRENT_TIMESTAMP), 6 + 6);current2023-11-18 00:00:00.000
First Day of MonthSELECT DATEADD(DAY,1,EOMONTH(getdate(),-1))current2023-11-01
SELECT DATEADD(DAY,1,EOMONTH(getdate(),-2))last month2023-10-01
SELECT DATEADD(DAY,1,EOMONTH(getdate(),0)) next month2023-12-01
Last Day of MonthSELECT EOMONTH(getdate())current2023-11-30
SELECT EOMONTH(getdate(), -1)last month2023-10-31
SELECT EOMONTH(getdate(), 1) next month2023-12-31
Week Day NameSELECT DATENAME(dw,GETDATE());Tuesday
Full Text DateSELECT FORMAT (getdate(), ‘dddd, MMMM dd, yyyy’);EnglishTuesday, November 14, 2023
SELECT FORMAT (getdate(), ‘dddd, MMMM dd, yyyy’,’es-es’);Spanishmartes, noviembre 14, 2023
Date FormatsyyyyMMddSELECT FORMAT (getdate(), ‘yyyyMMdd HHmmss’);with time20231114 085329
SELECT FORMAT (getdate(), ‘yyyyMMdd’);20231114
SELECT CONVERT(VARCHAR(10), GETDATE(),112);20231114
MM/dd/yyyySELECT FORMAT (getdate(), ‘d’,’us’);11/14/2023
SELECT FORMAT (getdate(), ‘MM/dd/yyyy’);11/14/2023
SELECT FORMAT (getdate(), ‘hh:mm:ss tt’);08:54:37 AM
SELECT FORMAT (getdate(), ‘MM/dd/yyyy hh:mm:ss tt’); with time11/14/2023 08:54:37 AM
yyyy:MM:dd HH:mmSELECT FORMAT(GETDATE(), ‘yyyy:MM:dd HH:mm’);with time2023:11:14 08:57
yyyy-MM-ddSELECT FORMAT (getdate(), ‘yyyy-MM-dd’);2023-11-14
SELECT CONVERT(VARCHAR(10), GETDATE(),120);2023-11-14
Reversed Date FormatsMM/dd/yyyySELECT CONVERT(NVARCHAR(12),(convert(date,CONVERT(CHAR(10), 20221101), 101)),101);11/01/2022
yyyy-MM-ddSELECT CAST(CAST(20221101 AS VARCHAR(50)) AS DATE) 2022-11-01
Time FormatsHHmmssSELECT FORMAT (getdate(), ‘HHmmss’);090122
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(),108),’:’,”);090122
hh:mm:ssSELECT FORMAT(SYSDATETIME(), N’hh:mm:ss tt’); 09:01:22 AM
SELECT FORMAT(SYSDATETIME(), N’hh:mm t’); 09:01 A
AGEDECLARE @DOB AS date = ‘1955-11-05’
SELECT (CONVERT(int,CONVERT(char(8),GETDATE(),112))-CONVERT(char(8),@Dob,112))/10000;
68