SQL - DATEDIFF_BIG() Function



The SQL DATEDIFF_BIG() is almost similar to the DATEDIFF() function but it is used to calculate the difference (as a signed big integer value) between the specified startdate and enddate.

To use this function, we need to provide three parameters: the type of interval to measure (such as year, quarter, month, hour, minute, etc.), the start date or time that specifies the beginning of the period to measure, and the end date or time that specifies the end of the period to measure.

This function returns the result in a 64-bit bigint data type, that can store values up to 9,223,372,036,854,775,807. Whereas DATEDIFF() function can store values up to 2,147,483,647 only.

Syntax

Following is the syntax of the SQL DATEDIFF_BIG() function −

DATEDIFF_BIG(datepart, startdate, enddate)

Parameters

This function accepts three parameters. The same is described below −

  • datepart − This specifies the type of (date or time) segment to return. The following are the possible values −

    • year, yyyy, yy = Year
    • quarter, qq, q = Quarter
    • month, mm, m = Month
    • dayofyear, dy, y = Day of the year
    • day, dd, d = Day
    • week, ww, wk = Week
    • weekday, dw, w = Weekday
    • hour, hh = Hour
    • minute, mi, n = Minute
    • second, ss, s = Second
    • millisecond, ms = Millisecond
  • startdate, enddate − These specify the two dates that we want to compare.

Example

In the following example, we are calculating the difference between two date values in years using the following query −

SQL> SELECT DATEDIFF_BIG(YEAR, '2023/03/15', '2033/02/15') AS DATE_DIFF_IN_YEARS;

Output

When we execute the above query, the output is obtained as follows −

+--------------------+
| DATE_DIFF_IN_YEARS |
+--------------------+
| 10                 |
+--------------------+

Example

Here, we are calculating the difference between two date values in quarters using the following query −

SQL> SELECT DATEDIFF_BIG(QUARTER, '2023/03/15', '2024/02/15') AS DATE_DIFF_IN_QUARTER;

Output

On executing the above query, the output is displayed as follows −

+----------------------+
| DATE_DIFF_IN_QUARTER |
+----------------------+
| 4                    |
+----------------------+

Example

We can calculate the difference between two specified times in seconds using the following query −

Note − The CURRENT_TIMESTAMP retrieves the date and time at the moment and also be used as an argument to the function.

SQL> SELECT DATEDIFF_BIG(SECOND, '2023/02/15 07:30:00:000', CURRENT_TIMESTAMP) AS TIME_DIFF_IN_SECONDS;

Output

The output for the above query is produced as given below −

+----------------------+
| TIME_DIFF_IN_SECONDS |
+----------------------+
| 368610               |
+----------------------+

Example

In this example, we are calculating the difference between two specified times in minutes

Note − The GETDATE() function retrieves the date and time at the moment and also be used as an argument to the function.

SQL> SELECT DATEDIFF_BIG(MINUTE, '2023/02/01 07:30', GETDATE()) AS TIME_DIFF_IN_MINUTE

Output

If we execute the above query, the result is produced as follows −

+---------------------+
| TIME_DIFF_IN_MINUTE |
+---------------------+
| 26304               |
+---------------------+

Example

Assume we have created a table with the name EMPLOYEE in the SQL database using the CREATE statement as shown in the query below −

SQL> CREATE TABLE EMPLOYEE(ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, DATE_OF_BIRTH VARCHAR (20));

Now, let us insert some records in the EMPLOYEE table using INSERT statements as shown in the query below −

SQL> INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(1, 'Dhruv', '2000-12-05');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(2, 'Arjun', '2000-03-01');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(3, 'Dev', '2001-03-15');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(4, 'Riya', '2003-12-05');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(5, 'Aarohi', '2000-05-02');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(6, 'Lisa', '1999-11-25');
INSERT INTO EMPLOYEE(ID, NAME, DATE_OF_BIRTH) VALUES(7, 'Roy', '2001-05-30');

We can verify whether the table is created or not using the following query −

SQL> SELECT * FROM EMPLOYEE;

The table EMPLOYEE is successfully created in the SQL database.

+-----+--------+---------------+
| ID  | NAME   | DATE_OF_BIRTH |
+-----+--------+---------------+
| 1   | Dhruv  | 2000-12-05    |
| 2   | Arjun  | 2000-03-01    |
| 3   | Dev    | 2001-03-15    |
| 4   | Riya   | 2003-12-05    |
| 5   | Aarohi | 2000-05-02    |
| 6   | Lisa   | 1999-11-25    |
| 7   | Roy    | 2001-05-30    |
+-----+--------+---------------+

Here, we are calculating the age of every employee in years using the following query −

SQL> SELECT ID, NAME, DATE_OF_BIRTH, DATEDIFF_BIG(YEAR, DATE_OF_BIRTH, GETDATE()) AS AGE_IN_YEARS FROM EMPLOYEE;

Output

When we execute the above query, the output is obtained as follows −

+-----+--------+----------------+--------------+
| ID  | NAME   | DATE_OF_BIRTH  | AGE_IN_YEARS |   
+-----+--------+----------------+--------------+
| 1   | Dhruv  | 2000-12-05     | 23           |
| 2   | Arjun  | 2000-03-01     | 23           |
| 3   | Dev    | 2001-03-15     | 22           |
| 4   | Riya   | 2003-12-05     | 20           |
| 5   | Aarohi | 2000-05-02     | 23           |
| 6   | Lisa   | 1999-11-25     | 24           |
| 7   | Roy    | 2001-05-30     | 22           |
+-----+--------+----------------+--------------+

Example

Let us create another table with the name OTT in the SQL database using the CREATE statement as shown in the query below −

SQL> CREATE TABLE OTT(ID INT NOT NULL, SUBSCRIBER_NAME VARCHAR (200) NOT NULL, MEMBERSHIP VARCHAR (200), SUBCRIPTION_DATE DATE NOT NULL);

Now, let us insert some records in the OTT table using INSERT statements as shown in the query below −

SQL> INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(1, 'Dhruv', 'Silver', '2022-12-05');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(2, 'Arjun','Platinum', '2021-03-01');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(3, 'Dev','Silver', '2021-03-15');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(4, 'Riya','Gold', '2022-12-05');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(5, 'Aarohi','Platinum', '2020-05-02');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(6, 'Lisa','Platinum', '2022-11-25');
INSERT INTO OTT(ID, SUBSCRIBER_NAME, MEMBERSHIP, SUBCRIPTION_DATE) VALUES(7, 'Roy','Gold', '2021-05-30');

We can verify whether the table OTT is created or not using the following query −

SQL> SELECT * FROM OTT;

The table OTT is successfully created in the SQL database.

+-----+-----------------+------------+------------------+
| ID  | SUBSCRIBER_NAME | MEMBERSHIP | SUBSCRIPTION_DATE|   
+-----+-----------------+------------+------------------+
| 1   | Dhruv           | Silver     | 2022-12-05       |
| 2   | Arjun           | Platinum   | 2021-03-01       |
| 3   | Dev             | Silver     | 2021-03-15       |
| 4   | Riya            | Gold       | 2022-12-05       |
| 5   | Aarohi          | Platinum   | 2020-05-02       |
| 6   | Lisa            | Platinum   | 2022-11-25       |
| 7   | Roy             | Gold       | 2021-05-30       |
+-----+-----------------+------------+------------------+

We can display the remaining number of days for the subscription plans to be complete using the following query −

SQL> SELECT SUBSCRIBER_NAME, SUBCRIPTION_DATE, DATEDIFF_BIG(DAY, SUBCRIPTION_DATE, CURRENT_TIMESTAMP) AS REMAINING_DAYS FROM OTT;

Output

If we execute the above query, the result is produced as follows −

+-----------------+-------------------+-----------------+
| SUBSCRIBER_NAME | SUBSCRIPTION_DATE | REMAINING_DAYS  |   
+-----------------+-------------------+-----------------+
| Dhruv           | 2022-12-05        | 72              |
| Arjun           | 2021-03-01        | 716             |
| Dev             | 2021-03-15        | 702             |
| Riya            | 2022-12-05        | 72              |
| Aarohi          | 2020-05-02        | 1019            |
| Lisa            | 2022-11-25        | 82              |
| Roy             | 2021-05-30        | 626             |
+-----------------+-------------------+-----------------+

Difference between DATEDIFF() and DATEDIFF_BIG()

Both DATEDIFF() and DATEDIFF_BIG() functions in SQL are used to calculate the difference between two dates. The main difference between them is the data type of the result that they return.

  • The DATEDIFF() function returns a 32-bit integer data type, that can store values up to 2,147,483,647. If the result of the function exceeds the maximum value that can be stored in an integer data type, it returns an overflow error.

  • The DATEDIFF_BIG() function returns a 64-bit bigint data type, that can store values up to 9,223,372,036,854,775,807. This function can be used when the function results in a number larger than the maximum value that can be stored in an integer data type.

Let us look into the difference between the above two functions using appropriate examples −

Example

In the example below, we are trying to calculate the difference of seconds between two date values using the DATEDIFF() function.

SQL> SELECT DATEDIFF(SECOND, '0001-01-01 00:00:00', '9999-01-02 00:00:00') AS RESULT

Error

Here, the result of the DATEDIFF() function throws an error because it exceeds the maximum value that can be stored in an integer datatype.

The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.

Example

In the example below, we are trying to calculate the difference of seconds between the same two date values using the DATEDIFF_BIG() function..

SQL> SELECT DATEDIFF_BIG(SECOND, '0001-01-01 00:00:00', '9999-01-02 00:00:00') AS RESULT

Output

Here, the DATEDIFF_BIG() function can handle the larger result without an error.

+--------------+
| RESULT       |
+--------------+
| 315506448000 |
+--------------+
sql-date-functions.htm
Advertisements