DATEDIFF

functionsupportedCore SQL functions

docs.snowflake.com ↗

Date & time functions

Calculates the difference between two date, time, or timestamp expressions based on the date or time part requested. The function returns the result of subtracting the second argument from the third argument. Note Difference calculations compare the specified date or time part, not the complete date or time. For example, the month difference between November 28, 2024 and December 5, 2024 is 1, because the difference between the two months November and December, both in 2024, is 1. To reflect the fact that the difference between the two dates is less than a full month, calculate the difference in days instead. You can also use the minus sign (-) to calculate the difference between two dates by subtracting one date from another. To add units of time to a date, time, or timestamp (for example, add two days to a date) or subtract units of time from them, you can use the DATEADD, TIMEADD, or TIMESTAMPADD function.

Syntax

For DATEDIFF

DATEDIFF( <date_or_time_part>, <date_or_time_expr1>, <date_or_time_expr2> )

For minus sign

<date_expr2> - <date_expr1>

Parameters

NameRequiredDescription
date_or_time_partrequiredThe unit of time. Must be one of the values listed in Supported date and time parts (for example, month). The value can be a string literal or can be unquoted (for example, 'month' or month).
date_or_time_expr1, date_or_time_expr2requiredThe values to compare. Must be a date, a time, a timestamp, or an expression that can be evaluated to a date, a time, or a timestamp. The value date_or_time_expr1 is subtracted from date_or_time_expr2.
date_expr1, date_expr2requiredThe values to compare. Must be a date, or an expression that can be evaluated to a date. The value date_expr1 is subtracted from date_expr2.

Returns

For DATEDIFF: Returns an integer representing the difference in the number of units (seconds, days, and so on) between date_or_time_expr2 and date_or_time_expr1. Returns NULL if any argument is NULL. For minus sign: Returns an integer representing the number of days difference between date_expr2 and date_expr1. (The units are always days.) Returns an error if date_expr2 or date_expr1 is NULL.

Usage notes

See also

Referenced by