SQL DATEPART Function Use and Examples

Overview

The DATEPART function returns an integer value for the specified part of the date or time.

Explanation

Syntax

DATEPART(datepart, datetime)

Parameters

  • datepart – Is the part of the date we want to get. It can be a year (yy, yyyy), quarter (qq, q), month (mm, m), dayofyear(dy, y), day (dd, d), week (wk, ww), weekday (dw, w), hour (hh), minute (mi, n), second (ss, s), millisecond (ms), microsecond (mcs), nanosecond (ns), TZoffset (tz), ISO_WEEK (ISOWK,ISOWW). You can use the full name or the abbreviations in parenthesis.
  • datetime – Is the datetime, date or time used in the function to get part of it.

Simple DATEPART Example

The following example will show the year of the DATEPART specified.

SELECT DATEPART(year, '2020-03-05 2:10:30.123') as datepart
DATEPART SIMPLE EXAMPLE

NULL values with DATEPART

If a null parameter is used, the function will return a null value.

SELECT DATEPART(month, NULL) as datepart
DATEPART with NULL values

Conversion failed when converting date for DATEPART

A typical error is a conversion failure. The following code shows an example:

SELECT DATEPART(day, '2021, march 21')

The date is literal and a literal date is not supported with this function and therefore return this error message.

Conversion failed when converting date and/or time from character string.

Using DATEPART against a Table

The following example shows the year, month and day for birthdates from the employee table.

SELECT DATEPART(YY, BirthDate) as year, 
       DATEPART(MM, BirthDate) as month, 
       DATEPART(dd, BirthDate) as day
FROM HumanResources.Employee
DATEPART and a table

DATEPART Example with Different Units

The following example shows the DATEPART using different units.

SELECT 
   DATEPART(yy, '2020-03-05 2:10:30.123') as YEAR,
   DATEPART(mm, '2020-03-05 2:10:30.123') as MONTH,
   DATEPART(DD, '2020-03-05 2:10:30.123') as DAY,
   DATEPART(hh, '2020-03-05 2:10:30.123') as HOUR,
   DATEPART(mi, '2020-03-05 2:10:30.123') as MINUTE,
   DATEPART(ss, '2020-03-05 2:10:30.123') as SECOND,
   DATEPART(ms, '2020-03-05 2:10:30.123') as MILLISECOND
DATEPART units used

Here is another example showing all possible output.

DECLARE @date datetime2 = '2021-01-07 14:36:17.6222691'
DateGroupDatePartQueryResult
daydSELECT DATEPART(d, @date)7
daydaySELECT DATEPART(day, @date)7
dayddSELECT DATEPART(dd, @date)7
dayofyeardayofyearSELECT DATEPART(dayofyear, @date)7
dayofyeardySELECT DATEPART(dy, @date)7
dayofyearySELECT DATEPART(y, @date)7
hourhhSELECT DATEPART(hh, @date)14
hourhourSELECT DATEPART(hour, @date)14
microsecondmicrosecondSELECT DATEPART(microsecond, @date)622269
microsecondmcsSELECT DATEPART(mcs, @date)622269
millisecondmillisecondSELECT DATEPART(millisecond, @date)622
millisecondmsSELECT DATEPART(ms, @date)622
minutemiSELECT DATEPART(mi, @date)36
minuteminuteSELECT DATEPART(minute, @date)36
minutenSELECT DATEPART(n, @date)36
monthmSELECT DATEPART(m, @date)1
monthmmSELECT DATEPART(mm, @date)1
monthmonthSELECT DATEPART(month, @date)1
nanosecondnanosecondSELECT DATEPART(nanosecond, @date)622269100
nanosecondnsSELECT DATEPART(ns, @date)622269100
quarterqSELECT DATEPART(q, @date)1
quarterqqSELECT DATEPART(qq, @date)1
quarterquarterSELECT DATEPART(quarter, @date)1
secondsSELECT DATEPART(s, @date)17
secondsecondSELECT DATEPART(second, @date)17
secondssSELECT DATEPART(ss, @date)17
weekweekSELECT DATEPART(week, @date)2
weekwkSELECT DATEPART(wk, @date)2
weekwwSELECT DATEPART(ww, @date)2
weekdaydwSELECT DATEPART(dw, @date)5
weekdaywSELECT DATEPART(w, @date)5
weekdayweekdaySELECT DATEPART(weekday, @date)5
yearyearSELECT DATEPART(year, @date)2021
yearyySELECT DATEPART(yy, @date)2021
yearyyyySELECT DATEPART(yyyy, @date)2021
TZoffsetTZoffsetSELECT DATEPART(TZoffset, @date)0
TZoffsettzSELECT DATEPART(tz, @date)0
ISO_WEEKISO_WEEKSELECT DATEPART(ISO_WEEK, @date)1
ISO_WEEKISOWKSELECT DATEPART(ISOWK, @date)1
ISO_WEEKISOWWSELECT DATEPART(ISOWW, @date)1

DATENAME vs DATEPART Examples

The first example will work with DATENAME:

SELECT 'The year is: ' + DATENAME(yy, '2020-03-05') as example
CONTACT datename

However, it will fail with DATEPART.

SELECT 'The year is: ' + DATEPART(yy, '2020-03-05') as example

The error message is the following.

Conversion failed when converting the varchar value 'The year is: ' to data type int.

The following examples will display the same results with DATEPART and DATENAME:

SELECT CONCAT('The year is: ', DATEPART(yy, '2021-03-05')) as year
SELECT CONCAT('The year is: ', DATENAME(yy, '2021-03-05')) as year

Returns the same result for both.

concat with DATEPART and DATENAME
SELECT DATEPART(yy, '2021-03-05') + 4 as YEAR
SELECT DATENAME(yy, '2021-03-05') + 4 as YEAR

Returns the same result for both.

Additions with DATEPART and DATENAME

Additional Information

One comment

  1. You said in the article “The date is literal and a literal date is not supported with this function and therefore return this error message.” That’s not true. The 2nd operand WILL take a date string IF it’s a “legal” date string. For example, the following is a legal date string and it works just fine.

    SELECT DATEPART(day, ’21 march 2021′);

Leave a Reply

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