ADDDATE(date, INTERVAL value unit)
OR
ADDDATE(date, days)
DATE_ADD(date, INTERVAL value unit)
DATE_SUB(date, INTERVAL value unit)
Where,
date = Date Value
days = in ADDDATE() function, days equal to the number of days added to the date
value = Integer Number
unit = Unit of time such as DAY
, WEEK
, MONTH
, YEAR
, etc.
UNIT | |
MINUTE | MINUTES |
HOUR | HOURS |
DAY | DAYS |
WEEK | WEEKS |
MONTH | MONTHS |
QUARTER | QUARTERS |
YEAR | YEARS |
-- --------- ADD Unit of Time To a Specific Date ----------
-- DECLARE a DATETIME Variable
SET @DateVAl = CAST('2020-04-01 10:37:57.100000' AS DATETIME);
-- ADD Days to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL 30 DAY) AS 'ADD Days To a DATE';
-- Output ==> 2020-05-01 10:37:57
-- ADD Weeks to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL 2 WEEK) AS 'ADD Weeks To a DATE';
-- Output ==> 2020-04-15 10:37:57
-- ADD Months to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL 2 MONTH) AS 'ADD Months To a DATE';
-- Output ==> 2020-06-01 10:37:57
-- ADD Quarters to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL 2 QUARTER) AS 'ADD Quarters To a DATE';
-- Output ==> 2020-10-01 10:37:57
-- ADD Years to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL 2 YEAR) AS 'ADD Years To a DATE';
-- Output ==> 2022-04-01 10:37:57
-- ADD Hours to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL 2 HOUR) AS 'ADD Hours To a DATE';
-- Output ==> 2020-04-01 12:37:57
-- ADD Minutes to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL 15 MINUTE) AS 'ADD Minutes To a DATE';
-- Output ==> 2020-04-01 10:52:57
-- ADD Seconds to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL 1500 SECOND) AS 'ADD Seconds To a DATE';
-- Output ==> 2020-04-01 11:02:57
-- ADD MicroSeconds to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL 150000 MICROSECOND) AS 'ADD MicroSeconds To a DATE';
-- Output ==> 2020-04-01 10:37:57.150000
-- --------- Subtract Unit of Time To a Specific Date ----------
-- DECLARE a DATETIME Variable
SET @DateVAl = CAST('2020-04-01 10:37:57.100000' AS DATETIME);
-- Subtract Days to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL -30 DAY) AS 'Subtract Days To a DATE';
-- Output ==> 2020-03-02 10:37:57
-- Subtract Weeks to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL -2 WEEK) AS 'Subtract Weeks To a DATE';
-- Output ==> 2020-03-18 10:37:57
-- Subtract Months to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL -2 MONTH) AS 'Subtract Months To a DATE';
-- Output ==> 2020-02-01 10:37:57
-- Subtract Quarters to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL -2 QUARTER) AS 'Subtract Quarters To a DATE';
-- Output ==> 2019-10-01 10:37:57
-- Subtract Years to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL -2 YEAR) AS 'Subtract Years To a DATE';
-- Output ==> 2018-04-01 10:37:57
-- Subtract Hours to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL -2 HOUR) AS 'Subtract Hours To a DATE';
-- Output ==> 2020-04-01 08:37:57
-- Subtract Minutes to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL -15 MINUTE) AS 'Subtract Minutes To a DATE';
-- Output ==> 2020-04-01 10:22:57
-- Subtract Seconds to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL -1500 SECOND) AS 'Subtract Seconds To a DATE';
-- Output ==> 2020-04-01 10:12:57
-- Subtract MicroSeconds to the Specific Date
SELECT ADDDATE(@DateVAl, INTERVAL -150000 MICROSECOND) AS 'Subtract MicroSeconds To a DATE';
-- Output ==> 2020-04-01 10:37:56.850000
-- --------- ADD Unit of Time To a Specific Date ----------
-- DECLARE a DATETIME Variable
SET @DateVAl = CAST('2020-04-01 10:37:57.100000' AS DATETIME);
-- ADD Days to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL 30 DAY) AS 'ADD Days To a DATE';
-- Output ==> 2020-05-01 10:37:57
-- ADD Weeks to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL 2 WEEK) AS 'ADD Weeks To a DATE';
-- Output ==> 2020-04-15 10:37:57
-- ADD Months to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL 2 MONTH) AS 'ADD Months To a DATE';
-- Output ==> 2020-06-01 10:37:57
-- ADD Quarters to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL 2 QUARTER) AS 'ADD Quarters To a DATE';
-- Output ==> 2020-10-01 10:37:57
-- ADD Years to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL 2 YEAR) AS 'ADD Years To a DATE';
-- Output ==> 2022-04-01 10:37:57
-- ADD Hours to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL 2 HOUR) AS 'ADD Hours To a DATE';
-- Output ==> 2020-04-01 12:37:57
-- ADD Minutes to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL 15 MINUTE) AS 'ADD Minutes To a DATE';
-- Output ==> 2020-04-01 10:52:57
-- ADD Seconds to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL 1500 SECOND) AS 'ADD Seconds To a DATE';
-- Output ==> 2020-04-01 11:02:57
-- ADD MicroSeconds to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL 150000 MICROSECOND) AS 'ADD MicroSeconds To a DATE';
-- Output ==> 2020-04-01 10:37:57.150000
In this example, you will learn how to subtract years, months, weeks, days, hours, minutes, seconds, microseconds, etc. from a DateTime using DATE_ADD()
in MySQL.
-- --------- Subtract Unit of Time To a Specific Date ----------
-- DECLARE a DATETIME Variable
SET @DateVAl = CAST('2020-04-01 10:37:57.100000' AS DATETIME);
-- Subtract Days to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL -30 DAY) AS 'Subtract Days To a DATE';
-- Output ==> 2020-03-02 10:37:57
-- Subtract Weeks to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL -2 WEEK) AS 'Subtract Weeks To a DATE';
-- Output ==> 2020-03-18 10:37:57
-- Subtract Months to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL -2 MONTH) AS 'Subtract Months To a DATE';
-- Output ==> 2020-02-01 10:37:57
-- Subtract Quarters to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL -2 QUARTER) AS 'Subtract Quarters To a DATE';
-- Output ==> 2019-10-01 10:37:57
-- Subtract Years to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL -2 YEAR) AS 'Subtract Years To a DATE';
-- Output ==> 2018-04-01 10:37:57
-- Subtract Hours to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL -2 HOUR) AS 'Subtract Hours To a DATE';
-- Output ==> 2020-04-01 08:37:57
-- Subtract Minutes to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL -15 MINUTE) AS 'Subtract Minutes To a DATE';
-- Output ==> 2020-04-01 10:22:57
-- Subtract Seconds to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL -1500 SECOND) AS 'Subtract Seconds To a DATE';
-- Output ==> 2020-04-01 10:12:57
-- Subtract MicroSeconds to the Specific Date
SELECT DATE_ADD(@DateVAl, INTERVAL -150000 MICROSECOND) AS 'Subtract MicroSeconds To a DATE';
-- Output ==> 2020-04-01 10:37:56.850000
In this example, you will learn how to subtract years, months, weeks, days, hours, minutes, seconds, microseconds, etc. from a DateTime using DATE_SUB()
in MySQL.
-- --------- Subtract Unit of Time from a Specific Date ----------
-- DECLARE a DATETIME Variable
SET @DateVAl = CAST('2020-04-01 10:37:57.100000' AS DATETIME);
-- Subtract Days from the Specific Date
SELECT DATE_SUB(@DateVAl, INTERVAL 30 DAY) AS 'Subtract Days from a DATE';
-- Output ==> 2020-03-02 10:37:57
-- Subtract Weeks from the Specific Date
SELECT DATE_SUB(@DateVAl, INTERVAL 2 WEEK) AS 'Subtract Weeks from a DATE';
-- Output ==> 2020-03-18 10:37:57
-- Subtract Months from the Specific Date
SELECT DATE_SUB(@DateVAl, INTERVAL 2 MONTH) AS 'Subtract Months from a DATE';
-- Output ==> 2020-02-01 10:37:57
-- Subtract Quarters from the Specific Date
SELECT DATE_SUB(@DateVAl, INTERVAL 2 QUARTER) AS 'Subtract Quarters from a DATE';
-- Output ==> 2019-10-01 10:37:57
-- Subtract Years from the Specific Date
SELECT DATE_SUB(@DateVAl, INTERVAL 2 YEAR) AS 'Subtract Years from a DATE';
-- Output ==> 2018-04-01 10:37:57
-- Subtract Hours from the Specific Date
SELECT DATE_SUB(@DateVAl, INTERVAL 2 HOUR) AS 'Subtract Hours from a DATE';
-- Output ==> 2020-04-01 08:37:57
-- Subtract Minutes from the Specific Date
SELECT DATE_SUB(@DateVAl, INTERVAL 15 MINUTE) AS 'Subtract Minutes from a DATE';
-- Output ==> 2020-04-01 10:22:57
-- Subtract Seconds from the Specific Date
SELECT DATE_SUB(@DateVAl, INTERVAL 1500 SECOND) AS 'Subtract Seconds from a DATE';
-- Output ==> 2020-04-01 10:12:57
-- Subtract MicroSeconds from the Specific Date
SELECT DATE_SUB(@DateVAl, INTERVAL 150000 MICROSECOND) AS 'Subtract MicroSeconds from a DATE';
-- Output ==> 2020-04-01 10:37:56.850000
In this example, you will learn how to add years, months, weeks, days, hours, minutes, seconds, microseconds, etc. to a DateTime without using any Built-in function in MySQL. For adding a unit of time to date, we used the Plus(+) operator.
-- --------- ADD Unit of Time To a Specific Date ----------
-- DECLARE a DATETIME Variable
SET @DateVAl = CAST('2020-04-01 10:37:57.100000' AS DATETIME);
-- ADD Days to the Specific Date
SELECT @DateVAl + INTERVAL 30 DAY AS 'ADD Days To a DATE';
-- Output ==> 2020-05-01 10:37:57
-- ADD Weeks to the Specific Date
SELECT @DateVAl + INTERVAL 2 WEEK AS 'ADD Weeks To a DATE';
-- Output ==> 2020-04-15 10:37:57
-- ADD Months to the Specific Date
SELECT @DateVAl + INTERVAL 2 MONTH AS 'ADD Months To a DATE';
-- Output ==> 2020-06-01 10:37:57
-- ADD Quarters to the Specific Date
SELECT @DateVAl + INTERVAL 2 QUARTER AS 'ADD Quarters To a DATE';
-- Output ==> 2020-10-01 10:37:57
-- ADD Years to the Specific Date
SELECT @DateVAl + INTERVAL 2 YEAR AS 'ADD Years To a DATE';
-- Output ==> 2022-04-01 10:37:57
-- ADD Hours to the Specific Date
SELECT @DateVAl + INTERVAL 2 HOUR AS 'ADD Hours To a DATE';
-- Output ==> 2020-04-01 12:37:57
-- ADD Minutes to the Specific Date
SELECT @DateVAl + INTERVAL 15 MINUTE AS 'ADD Minutes To a DATE';
-- Output ==> 2020-04-01 10:52:57
-- ADD Seconds to the Specific Date
SELECT @DateVAl + INTERVAL 1500 SECOND AS 'ADD Seconds To a DATE';
-- Output ==> 2020-04-01 11:02:57
-- ADD MicroSeconds to the Specific Date
SELECT @DateVAl + INTERVAL 150000 MICROSECOND AS 'ADD MicroSeconds To a DATE';
-- Output ==> 2020-04-01 10:37:57.150000
In this example, you will learn how to Subtract years, months, weeks, days, hours, minutes, seconds, microseconds, etc. to a DateTime without using any Built-in function in MySQL. For subtracting the unit of time to date, we used the Minus(-) operator.
-- --------- Subtract Unit of Time From a Specific Date ----------
-- DECLARE a DATETIME Variable
SET @DateVAl = CAST('2020-04-01 10:37:57.100000' AS DATETIME);
-- Subtract Days From the Specific Date
SELECT @DateVAl - INTERVAL 30 DAY AS 'Subtract Days From a DATE';
-- Output ==> 2020-03-02 10:37:57
-- Subtract Weeks From the Specific Date
SELECT @DateVAl - INTERVAL 2 WEEK AS 'Subtract Weeks From a DATE';
-- Output ==> 2020-03-18 10:37:57
-- Subtract Months From the Specific Date
SELECT @DateVAl - INTERVAL 2 MONTH AS 'Subtract Months From a DATE';
-- Output ==> 2020-02-01 10:37:57
-- Subtract Quarters From the Specific Date
SELECT @DateVAl - INTERVAL 2 QUARTER AS 'Subtract Quarters From a DATE';
-- Output ==> 2019-10-01 10:37:57
-- Subtract Years From the Specific Date
SELECT @DateVAl - INTERVAL 2 YEAR AS 'Subtract Years From a DATE';
-- Output ==> 2018-04-01 10:37:57
-- Subtract Hours From the Specific Date
SELECT @DateVAl - INTERVAL 2 HOUR AS 'Subtract Hours From a DATE';
-- Output ==> 2020-04-01 08:37:57
-- Subtract Minutes From the Specific Date
SELECT @DateVAl - INTERVAL 15 MINUTE AS 'Subtract Minutes From a DATE';
-- Output ==> 2020-04-01 10:22:57
-- Subtract Seconds From the Specific Date
SELECT @DateVAl - INTERVAL 1500 SECOND AS 'Subtract Seconds From a DATE';
-- Output ==> 2020-04-01 10:12:57
-- Subtract MicroSeconds From the Specific Date
SELECT @DateVAl - INTERVAL 150000 MICROSECOND AS 'Subtract MicroSeconds From a DATE';
-- Output ==> 2020-04-01 10:37:56.850000
Comments