Return Date and Time Difference Values

1. Overview

The return date and time difference value functions covered in this section are:

DATEDIFF

DATDIFF function returns the count of the specified datepart boundaries crossed between the specified startdate and enddate.

This function is not currently supported in PostgreSQL deployments of the Cinchy platform.

New function translations are actively being worked on by the development team; please check back at a later time.

You can review the full list of in-progress function translations here.

Syntax

DATEDIFF ( datepart , startdate , enddate )

Arguments

datepart The units in which DATEDIFF reports the difference between the startdate and enddate. Commonly used datepart units include month or second.

The datepart value cannot be specified in a variable, nor as a quoted string like 'month'.

The following table lists all the valid datepart values. DATEDIFF accepts either the full name of the datepart, or any listed abbreviation of the full name.

startdate An expression that can resolve to one of the following values:

  • date

  • datetime

  • datetimeoffset

  • smalldatetime

  • time

Use four-digit years to avoid ambiguity.

Return Types

int

Remarks

Use DATEDIFF in the SELECT <list>, WHERE, HAVING, GROUP BY and ORDER BY clauses.

DATEDIFF implicitly casts string literals as a datetime2 type. This means that DATEDIFF does not support the format YDM when the date is passed as a string. You must explicitly cast the string to a datetime or smalldatetime type to use the YDM format.

Specifying SET DATEFIRST has no effect on DATEDIFF. DATEDIFF always uses Sunday as the first day of the week to ensure the function operates in a deterministic way.

DATEDIFF may overflow with a precision of minute or higher if the difference between enddate and startdate returns a value that is out of range for int.

Example 1

Finding the number of days between two dates.

SELECT
    DATEDIFF(day, [Created], [Modified]) AS 'Duration'
FROM
    [Cinchy].[Tables]
WHERE
	[Deleted] IS NULL

Example 2

Specifying user-defined variables for startdate and enddate

DECLARE @startdate datetime2 = '2007-05-05 12:10:09.3312722'
DECLARE @enddate datetime2 = '2007-05-04 12:10:09.3312722'
SELECT DATEDIFF(day, @startdate, @enddate)

Example 3: Specifying scalar system functions for startdate and enddate

SELECT DATEDIFF(millisecond, GETDATE(), SYSDATETIME())

DATEDIFF_BIG

DATEIFF_BIG function returns the count of the specified datepart boundaries crossed between the specified startdate and enddate.

This function is not currently supported in PostgreSQL deployments of the Cinchy platform.

New function translations are actively being worked on by the development team; please check back at a later time.

You can review the full list of in-progress function translations here.

Syntax

DATEDIFF_BIG ( datepart , startdate , enddate )

Arguments

datepart The part of startdate and enddate that specifies the type of boundary crossed.

This table lists all valid datepart argument names and abbreviations.

startdate An expression that can resolve to one of the following values:

  • date

  • datetime

  • datetimeoffset

  • smalldatetime

  • time

For date, DATEDIFF_BIG will accept a column expression, expression, string literal, or user-defined variable. A string literal value must resolve to a datetime. Use four-digit years to avoid ambiguity issues. DATEDIFF_BIG subtracts startdate from enddate. To avoid ambiguity, use four-digit years.

Return Types

Signed bigint

Remarks

Use DATEDIFF_BIG in the SELECT <list>, WHERE, HAVING, GROUP BY and ORDER BY clauses.

DATEDIFF_BIG implicitly casts string literals as a datetime2 type. This means that DATEDIFF_BIG does not support the format YDM when the date is passed as a string. You must explicitly cast the string to a datetime or smalldatetime type to use the YDM format.

Specifying SET DATEFIRST has no effect on DATEDIFF_BIG. DATEDIFF_BIG always uses Sunday as the first day of the week to ensure the function operates in a deterministic way.

DATEDIFF_BIG may overflow with a precision of nanosecond if the difference between enddate and startdate returns a value that is out of range for bigint.

Example

This example uses different types of expressions as arguments for the startdate and enddate parameters. It calculates the number of day boundaries crossed between dates in two columns of a table.

SELECT
    DATEDIFF_BIG(day, [Created], [Modified]) AS 'Duration'
FROM
    [Cinchy].[Tables]
WHERE
	[Deleted] IS NULL

Last updated