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 the year component from the specified date.

SELECT DatePart(“yyyy”, #09/05/2017#);

Definition and Usage

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

Syntax

DatePart(datepartdatefirstdayofweekfirstweekofyear)

Parameter Values

Parameter

Description

datepart

Mandatory. Specifies the unit of measurement for the difference to be returned. Options include:

  • yyyy = Year
  • q = Quarter
  • m = Month
  • y = Day of the year
  • d = Day
  • w = Weekday
  • ww = Week
  • h = Hour
  • n = Minute
  • s = Second

date

Required. The date from which a specific part will be extracted.

firstdayofweek

Optional. Specifies the first day of the week, which can be set to:

  • 0 = Use the NLS API setting
  • 1 = Sunday (default)
  • 2 = Monday
  • 3 = Tuesday
  • 4 = Wednesday
  • 5 = Thursday
  • 6 = Friday
  • 7 = Saturday

firstdayofyear

Optional. Specifies the definition of the first week of the year, which can be set to:

  • 0 = Use the NLS API setting
  • 1 = Use the first week that includes January 1st (default)
  • 2 = Use the first week in the year that has at least 4 days
  • 3 = Use the first full week of the year

Technical Details

WORKS IN

From Access 2000

More Examples

Example

Retrieve the month component from the specified date.

SELECT DatePart(“m”, #09/05/2017#);

Example

Retrieve the weekday of the current date.

SELECT DatePart(“w”, Date());