Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

DATEPART

Example

Retrieve and return a specific component or segment of a date.

SELECT DATEPART(year, ‘2017/08/25’AS DatePartInt;

Definition and Usage

The DATEPART() function retrieves a specific part of a date and returns the result as an integer value.

Syntax

DATEPART(intervaldate)

Parameter Values

Parameter

Description

interval

Required: Specify the part of the date argument to retrieve. Use one of the following values:

  • year, yyyy, yy: Year
  • quarter, qq, q: Quarter
  • month, mm, m: Month
  • dayofyear, dy, y: Day of the year
  • day, dd, d: Day of the month
  • week, ww, wk: Week
  • weekday, dw, w: Weekday
  • hour, hh: Hour
  • minute, mi, n: Minute
  • second, ss, s: Second
  • millisecond, ms: Millisecond
  • microsecond, mcs: Microsecond
  • nanosecond, ns: Nanosecond
  • tzoffset, tz: Timezone offset
  • iso_week, isowk, isoww: ISO week

 

Date

Required: Specify the date to use. Acceptable data types include: date, datetime, datetimeoffset, datetime2, smalldatetime, or time.

Technical Details

Return type:

int

Works in:

SQL Server (beginning from 2008), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse

More Examples

Example

Retrieve and provide a specific segment or component of a date.

SELECT DATEPART(yy, ‘2017/08/25’AS DatePartInt;

Example

Retrieve and provide a specific segment or component of a date.

SELECT DATEPART(month, ‘2017/08/25’AS DatePartInt;

Example

Retrieve and provide a specific segment or component of a date.

SELECT DATEPART(hour, ‘2017/08/25 08:36’AS DatePartInt;

Example

Retrieve and provide a specific segment or component of a date.

SELECT DATEPART(minute, ‘2017/08/25 08:36’AS DatePartInt;