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

DateDiff

Example

Retrieve the difference between two dates, measured in years.

SELECT DateDiff(“yyyy”, #13/01/1998#, #09/05/2017#);

Definition and Usage

The DATEDIFF() function computes the difference between two dates.

Syntax

DateDiff(datepartdate1date2firstdayofweekfirstweekofyear)

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

date1 and date2

Mandatory. Specifies the two dates used to compute the difference.

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 difference between two dates, measured in months.

SELECT DateDiff(“m”, #13/01/1998#, #09/05/2017#);

Example

Retrieve the difference in days between a specified date and today’s date.

SELECT DateDiff(“d”, #13/01/1998#, Date());