- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
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 | +-----+--------+-----------------+-------------------------+
To Continue Learning Please Login
Login with Google