SQL - DATEADD Function



The SQL DATEADD() function is a built-in function which adds a specified number (a signed integer) of intervals to a given date or time, and returns a modified date/time value.

This function accepts three parameters − the interval to add (such as year, quarter, month, hour, minute, etc.), the number of intervals to add (which can be positive or negative integer value), and the date or time value that specifies the starting point for the addition.

Note − The data type of the return value for this function is dynamic and it depends on the argument supplied for the date.

Syntax

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

DATEADD(datepart, number, date)

Parameters

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

  • datepart − This specifies the segment of the date or time to add. 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
  • number − This specifies the interval to add to the date. This interval can be positive (will get the date or datetime in the future) or a negative (will get the date or datetime in the past) integer.
  • date − This specifies the date to which the intervals are added.

Example

In the following example, we are trying to add 7 years to the given date −

SQL> SELECT DATEADD(YEAR, 7, '2023/02/14') AS RESULT_DATE;

Output

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

+-------------------------+
| RESULT_DATE             |
+-------------------------+
| 2030-02-14 00:00:00.000 |
+-------------------------+

Example

Here, we are trying to add 7 months to the given date using the following query −

SQL> SELECT DATEADD(MONTH, 7, '2023/02/14') AS RESULT_DATE;

Output

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

+-------------------------+
| RESULT_DATE             |
+-------------------------+
| 2023-09-14 00:00:00.000 |
+-------------------------+

Example

The following query will add 1 week to the given date −

SQL> SELECT DATEADD(WEEK, 1, '2023/02/14') AS RESULT_DATE;

Output

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

+-------------------------+
| RESULT_DATE             |
+-------------------------+
| 2023-02-21 00:00:00.000 |
+-------------------------+

Example

Here, we are adding 1 hour to the provided time using the following query −

SQL> SELECT DATEADD(HOUR, 1, '2023/02/14 09:00:30.430') AS RESULT_DATE;

Output

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

+-------------------------+
| RESULT_DATE             |
+-------------------------+
| 2023-02-14 10:00:30.430 |
+-------------------------+

Example

The following query will add 30 seconds to the specified time −

SQL> SELECT DATEADD(SECOND, 30, '2023/02/14 09:00:30.430') AS RESULT_DATE;

Output

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

+-------------------------+
| RESULT_DATE             |
+-------------------------+
| 2023-02-14 09:01:00.430 |
+-------------------------+

Example

In the following example, we are trying to subtract 1 month from the specified date −

SQL> SELECT DATEADD(MONTH, -1, '2023/02/14') AS RESULT_DATE;

Output

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

+-------------------------+
| RESULT_DATE             |
+-------------------------+
| 2023-01-14 00:00:00.000 |
+-------------------------+

Example

Here, we are adding 2 years to the specified date using the following query −

SQL> SELECT DATEADD(YEAR, +2, '2023/02/14') AS RESULT_DATE;

Output

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

+-------------------------+
| RESULT_DATE             |
+-------------------------+
| 2025-04-14 00:00:00.000 |
+-------------------------+

Example

If the values that are passed as parameters to this function are invalid, it will result in an error.

SQL> SELECT DATEADD(YEAR, 2, '2023/15/76') AS RESULT_DATE;

Error

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

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

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, AGE INT NOT NULL, SUBMISSION_DATE VARCHAR (25));

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, AGE, SUBMISSION_DATE) VALUES(1, 'Dhruv', 32, '2020-02-10');
INSERT INTO EMPLOYEE(ID, NAME, AGE, SUBMISSION_DATE) VALUES(2, 'Arjun', 25, '2020-12-15');
INSERT INTO EMPLOYEE(ID, NAME, AGE, SUBMISSION_DATE) VALUES(3, 'Dev', 23, '2021-03-11');
INSERT INTO EMPLOYEE(ID, NAME, AGE, SUBMISSION_DATE) VALUES(4, 'Riya', 25, '2019-02-05');
INSERT INTO EMPLOYEE(ID, NAME, AGE, SUBMISSION_DATE) VALUES(5, 'Aarohi', 27, '2018-06-16');
INSERT INTO EMPLOYEE(ID, NAME, AGE, SUBMISSION_DATE) VALUES(6, 'Lisa', 22, '2020-11-13');
INSERT INTO EMPLOYEE(ID, NAME, AGE, SUBMISSION_DATE) VALUES(7, 'Roy', 24, '2020-01-01');

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   | AGE  | SUBMISSION_DATE    | 
+-----+--------+------+--------------------+
| 1   | Dhruv  | 32   | 2020-02-10         | 
| 2   | Arjun  | 25   | 2020-12-15         |  
| 3   | Dev    | 23   | 2021-03-11         |
| 4   | Riya   | 25   | 2019-02-05         |
| 5   | Aarohi | 27   | 2018-06-16         |
| 6   | Lisa   | 22   | 2020-11-13         |
| 7   | Roy    | 24   | 2020-01-01         |
+-----+--------+------+--------------------+

The following query will add 10 years to the entities of the SUBMISSION_DATE column −

Note − The CURRENT_TIMESTAMP retrieves the date and time at the moment.

SQL> SELECT ID, NAME, SUBMISSION_DATE, DATEADD(YEAR, 10, SUBMISSION_DATE) AS DATE_ADD FROM EMPLOYEE;

Output

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

+-----+--------+-----------------+-------------------------+
| ID  | NAME   | SUBMISSION_DATE | DATE_ADD                | 
+-----+--------+-----------------+-------------------------+
| 1   | Dhruv  | 2020-02-10      | 2030-02-10 00:00:00.000 |
| 2   | Arjun  | 2020-12-15      | 2030-12-15 00:00:00.000 |
| 3   | Dev    | 2021-03-11      | 2031-03-11 00:00:00.000 |
| 4   | Riya   | 2019-02-05      | 2029-02-05 00:00:00.000 |
| 5   | Aarohi | 2018-06-16      | 2028-06-16 00:00:00.000 |
| 6   | Lisa   | 2020-11-13      | 2030-11-13 00:00:00.000 |
| 7   | Roy    | 2020-01-01      | 2030-01-01 00:00:00.000 |
+-----+--------+-----------------+-------------------------+

Example

Consider the previously created table and let us subtract 2 quarters from the entities of the SUBMISSION_DATE column −

SQL> SELECT ID, NAME, SUBMISSION_DATE, DATEADD(QUARTER, -2, SUBMISSION_DATE) AS DATE_ADD FROM EMPLOYEE;

Output

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

+-----+--------+-----------------+-------------------------+
| ID  | NAME   | SUBMISSION_DATE | DATE_ADD                |
+-----+--------+-----------------+-------------------------+
| 1   | Dhruv  | 2020-02-10      | 2019-08-10 00:00:00.000 |
| 2   | Arjun  | 2020-12-15      | 2020-06-15 00:00:00.000 |
| 3   | Dev    | 2021-03-11      | 2020-09-11 00:00:00.000 |
| 4   | Riya   | 2019-02-05      | 2018-08-05 00:00:00.000 |
| 5   | Aarohi | 2018-06-16      | 2017-12-16 00:00:00.000 |
| 6   | Lisa   | 2020-11-13      | 2020-05-13 00:00:00.000 |
| 7   | Roy    | 2020-01-01      | 2019-07-01 00:00:00.000 |
+-----+--------+-----------------+-------------------------+
sql-date-functions.htm
Advertisements