datediff

Description

Returns the difference between two dates.

Syntax

datediff(datepart, date1, date2)

Parameters

datepart

is a date part or abbreviation. For a list of the date parts and abbreviations recognized by Adaptive Server, see “Date parts”.

date1

can be either the function getdate, a character string in an acceptable date format, an expression that evaluates to a valid date format, or the name of a datetime column.

date2

can be either the function getdate, a character string in an acceptable date format, an expression that evaluates to a valid date format, or the name of a datetime or smalldatetime column.

Examples

Example 1

This query finds the number of days that have elapsed between pubdate and the current date (obtained with the getdate function):

select newdate = datediff(day, pubdate, getdate()) 
    from titles 

Usage

Standards

SQL92 – Compliance level: Transact-SQL extension.

Permissions

Any user can execute datediff.

See also

Datatypes Date and time datatypes

Commands select, where clause

Functions dateadd, datename, datepart, getdate