7.13 DATEDIFF

Use the @DATEDIFF function to calculate the difference between two dates or datetimes, in days or seconds.

Syntax

@DATEDIFF ('difference', 'date', 'date')
difference

The difference between the specified dates. Valid values can be:

  • DD, which computes the difference in days.

  • SS, which computes the difference in seconds.

date

A string within single quote marks, in the format of 'YYYY-MM-DD[*HH:MI[:SS]]', where * can be a colon (:) or a blank space, or the @DATENOW function without quotes to return the current date.

Examples

Example 1   

The following calculates the number of days since the beginning of the year 2011.

YTD = @DATEDIFF ('DD', '2011-01-01', @DATENOW ())
Example 2   

The following calculates the numerical day of the year. (@DATEDIFF returns 0 for 2011-01-01):

todays_day = @COMPUTE (@DATEDIFF ('DD', '2011-01-01', @DATENOW ()) +1)