All Products
Search
Document Center

MaxCompute:DATEDIFF

Last Updated:Jul 24, 2023

Calculates the difference between date1 and date2. The difference is measured in the time unit specified by datepart.

Syntax

bigint datediff(date|datetime|timestamp <date1>, date|datetime|timestamp <date2>, string <datepart>)

Parameters

  • date1 and date2: required. The minuend and subtrahend, which are of the DATE, DATETIME, or TIMESTAMP type. If the input values are of the STRING type and the MaxCompute V1.0 data type edition is used in your project, the input values are implicitly converted into the DATETIME type before calculation.

  • datepart: optional. The time unit, which is a constant of the STRING type.

    If you enable the MaxCompute V2.0 data type edition, you can leave datepart empty. Default value: day. For more information about the MaxCompute V2.0 data type edition, see Data type editions. The EDTF is also supported, such as -year, -month, -mon, -day, or -hour.

    Note

    This function omits the lower unit based on the time unit specified by datepart and then calculates the result.

Return value

A value of the BIGINT type is returned. The return value varies based on the values of the input parameters.

  • If the value of date1 or date2 is not of the DATE, DATETIME, or TIMESTAMP type, an error is returned.

  • If date1 is earlier than date2, a negative value is returned.

  • If the value of date1 or date2 is null, the return value is null.

  • If the value of datepart is null, the return value is null.

Sample data

This section provides sample source data for you to understand how to use date functions. In this topic, a table named mf_date_fun_t is created and data is inserted into the table. Sample statements:

create table if not exists mf_date_fun_t(
    id      int,
    date1   date,
    datetime1   datetime,
    timestamp1 timestamp,
    date2   date,
    datetime2   datetime,
    timestamp2 timestamp,
    date3 string,
    date4 bigint);
insert into mf_date_fun_t values
(1,DATE'2021-11-29',DATETIME'2021-11-29 00:01:00',TIMESTAMP'2021-01-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-20',123456780),
(2,DATE'2021-11-28',DATETIME'2021-11-28 00:02:00',TIMESTAMP'2021-02-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-21',123456781),
(3,DATE'2021-11-27',DATETIME'2021-11-27 00:03:00',TIMESTAMP'2021-03-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-22',123456782),
(4,DATE'2021-11-26',DATETIME'2021-11-26 00:04:00',TIMESTAMP'2021-04-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-23',123456783),
(5,DATE'2021-11-25',DATETIME'2021-11-25 00:05:00',TIMESTAMP'2021-05-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-24',123456784),
(6,DATE'2021-11-24',DATETIME'2021-11-24 00:06:00',TIMESTAMP'2021-06-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-25',123456785),
(7,DATE'2021-11-23',DATETIME'2021-11-23 00:07:00',TIMESTAMP'2021-07-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-26',123456786),
(8,DATE'2021-11-22',DATETIME'2021-11-22 00:08:00',TIMESTAMP'2021-08-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-27',123456787),
(9,DATE'2021-11-21',DATETIME'2021-11-21 00:09:00',TIMESTAMP'2021-09-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-28',123456788),
(10,DATE'2021-11-20',DATETIME'2021-11-20 00:10:00',TIMESTAMP'2021-10-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-29',123456789);

Query data from the mf_date_fun_t table. Sample statement:

select * from mf_date_fun_t;
-- The following result is returned: 
+------+-------+------------+------------+-------+------------+------------+-------+------------+
| id   | date1 | datetime1  | timestamp1 | date2 | datetime2  | timestamp2 | date3 | date4      |
+------+-------+------------+------------+-------+------------+------------+-------+------------+
| 1    | 2021-11-29 | 2021-11-29 00:01:00 | 2021-01-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-20 | 123456780  |
| 2    | 2021-11-28 | 2021-11-28 00:02:00 | 2021-02-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-21 | 123456781  |
| 3    | 2021-11-27 | 2021-11-27 00:03:00 | 2021-03-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-22 | 123456782  |
| 4    | 2021-11-26 | 2021-11-26 00:04:00 | 2021-04-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-23 | 123456783  |
| 5    | 2021-11-25 | 2021-11-25 00:05:00 | 2021-05-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-24 | 123456784  |
| 6    | 2021-11-24 | 2021-11-24 00:06:00 | 2021-06-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-25 | 123456785  |
| 7    | 2021-11-23 | 2021-11-23 00:07:00 | 2021-07-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-26 | 123456786  |
| 8    | 2021-11-22 | 2021-11-22 00:08:00 | 2021-08-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-27 | 123456787  |
| 9    | 2021-11-21 | 2021-11-21 00:09:00 | 2021-09-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-28 | 123456788  |
| 10   | 2021-11-20 | 2021-11-20 00:10:00 | 2021-10-11 00:00:00.123456789 | 2021-10-29 | 2021-10-29 00:00:00 | 2021-10-11 00:00:00.123456789 | 2021-11-29 | 123456789  |
+------+-------+------------+------------+-------+------------+------------+-------+------------+

Examples: static data

-- The start time is 2005-12-31 23:59:59 and the end time is 2006-01-01 00:00:00.
    -- The return value is 1. 
    select datediff(end, start, 'dd'); 
    -- The return value is 1. 
    select datediff(end, start, 'mm');
    -- The return value is 1. 
    select datediff(end, start, 'yyyy');
    -- The return value is 1. 
    select datediff(end, start, 'hh');
    -- The return value is 1. 
    select datediff(end, start, 'mi');
    -- The return value is 1. 
    select datediff(end, start, 'ss');
-- The return value is 1800. 
select datediff(datetime'2013-05-31 13:00:00', datetime'2013-05-31 12:30:00', 'ss');
-- The return value is 30. 
set odps.sql.type.system.odps2=false; 
select datediff('2013-05-31 13:00:00', '2013-05-31 12:30:00', 'mi');
-- The return value is 11. 
select datediff(date '2013-05-21', date '2013-05-10', 'dd');
-- Enable the MaxCompute V2.0 data type edition. Commit the following SET statement along with the SQL statement. The return value is 1800. 
set odps.sql.type.system.odps2=true;
select datediff(timestamp '2013-05-31 13:00:00', timestamp '2013-05-31 12:30:00', 'ss');
-- The start time is 2018-06-04 19:33:23.234 and the end time is 2018-06-04 19:33:23.250. Date values that are accurate to the millisecond do not adopt the standard DATETIME type and cannot be implicitly converted into the DATETIME type. In this case, an explicit conversion is required. The return value is 16. 
select datediff(to_date('2018-06-04 19:33:23.250', 'yyyy-mm-dd hh:mi:ss.ff3'), to_date('2018-06-04 19:33:23.234', 'yyyy-mm-dd hh:mi:ss.ff3') , 'ff3');
-- The return value is null. 
select datediff(date '2013-05-21', date '2013-05-10', null);
-- The return value is null. 
select datediff(date '2013-05-21', null, 'dd');

Examples: table data

Calculate the differences between the values in the date1 and date2 columns, between the values in the datetime1 and datetime2 columns, and between the values in the timestamp1 and timestamp2 columns. The differences are measured in the specified time unit. Data in Sample data is used in this example. Sample statements:

-- Enable the MaxCompute V2.0 data type edition. Commit the following SET statement along with the SQL statement. 
set odps.sql.type.system.odps2=true;
select date1, date2, datediff(date1,date2,'dd') as date1_date2_datediff, datetime1, datetime2, datediff(datetime1, datetime2,'dd') as datetime1_datetime2_datediff, timestamp1, timestamp2, datediff(timestamp1, timestamp2,'mm') as timestamp1_timestamp2_datediff from mf_date_fun_t;

The following result is returned:

+------------+------------+----------------------+---------------------+---------------------+------------------------------+-------------------------------+-------------------------------+--------------------------------+
| date1      | date2      | date1_date2_datediff | datetime1           | datetime2           | datetime1_datetime2_datediff | timestamp1                    | timestamp2                    | timestamp1_timestamp2_datediff |
+------------+------------+----------------------+---------------------+---------------------+------------------------------+-------------------------------+-------------------------------+--------------------------------+
| 2021-11-29 | 2021-10-29 | 31                   | 2021-11-29 00:01:00 | 2021-10-29 00:00:00 | 31                           | 2021-01-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | -9                             |
| 2021-11-28 | 2021-10-29 | 30                   | 2021-11-28 00:02:00 | 2021-10-29 00:00:00 | 30                           | 2021-02-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | -8                             |
| 2021-11-27 | 2021-10-29 | 29                   | 2021-11-27 00:03:00 | 2021-10-29 00:00:00 | 29                           | 2021-03-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | -7                             |
| 2021-11-26 | 2021-10-29 | 28                   | 2021-11-26 00:04:00 | 2021-10-29 00:00:00 | 28                           | 2021-04-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | -6                             |
| 2021-11-25 | 2021-10-29 | 27                   | 2021-11-25 00:05:00 | 2021-10-29 00:00:00 | 27                           | 2021-05-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | -5                             |
| 2021-11-24 | 2021-10-29 | 26                   | 2021-11-24 00:06:00 | 2021-10-29 00:00:00 | 26                           | 2021-06-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | -4                             |
| 2021-11-23 | 2021-10-29 | 25                   | 2021-11-23 00:07:00 | 2021-10-29 00:00:00 | 25                           | 2021-07-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | -3                             |
| 2021-11-22 | 2021-10-29 | 24                   | 2021-11-22 00:08:00 | 2021-10-29 00:00:00 | 24                           | 2021-08-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | -2                             |
| 2021-11-21 | 2021-10-29 | 23                   | 2021-11-21 00:09:00 | 2021-10-29 00:00:00 | 23                           | 2021-09-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | -1                             |
| 2021-11-20 | 2021-10-29 | 22                   | 2021-11-20 00:10:00 | 2021-10-29 00:00:00 | 22                           | 2021-10-11 00:00:00.123456789 | 2021-10-11 00:00:00.123456789 | 0                              |
+------------+------------+----------------------+---------------------+---------------------+------------------------------+-------------------------------+-------------------------------+--------------------------------+

Related functions

DATEDIFF is a date function. For more information about functions related to date computing and conversion, see Date functions.