
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How MySQL performs date arithmetic with addition and subtraction operators?
MySQL can perform date arithmetic with addition and subtraction operators by adding together INTERVAL keyword with a unit of time, date or datetime.
Example1
Adding 2 days to a particular date.
mysql> Select '2017-05-20' + INTERVAL 2 day; +-------------------------------+ | '2017-05-20' + INTERVAL 2 day | +-------------------------------+ | 2017-05-22 | +-------------------------------+ 1 row in set (0.00 sec)
Example2
Subtracting 2 days from a particular date.
mysql> Select '2017-05-20' - INTERVAL 2 day; +-------------------------------+ | '2017-05-20' - INTERVAL 2 day | +-------------------------------+ | 2017-05-18 | +-------------------------------+ 1 row in set (0.00 sec)
Example 3
Adding 2 hours in time.
mysql> Select '2017-05-20 05:04:35' + INTERVAL 3 hour; +-----------------------------------------+ | '2017-05-20 05:04:35' + INTERVAL 3 hour | +-----------------------------------------+ | 2017-05-20 08:04:35 | +-----------------------------------------+ 1 row in set (0.00 sec)
Example 4
Adding one month to a particular date
mysql> Select '2017-05-20 05:04:35' + INTERVAL 1 month; +------------------------------------------+ | '2017-05-20 05:04:35' + INTERVAL 1 month | +------------------------------------------+ | 2017-06-20 05:04:35 | +------------------------------------------+ 1 row in set (0.00 sec)
In this way with the help of INTERVAL keyword, we can perform date arithmetic.
- Related Articles
- How will addition, subtraction, multiplication, and division operator work with date represented as MySQL string?
- How will addition, subtraction, multiplication and division operator work with date values stored in MySQL table?
- Explain addition and subtraction of fractions with example.
- Explain Closure property of addition and subtraction with example.
- Explain the addition and subtraction of integers with examples.
- Explain addition and subtraction of algebraic equations.
- Signals and Systems: Addition and Subtraction of Signals
- Python Arithmetic Operators
- Java arithmetic operators
- Perl Arithmetic Operators
- How can I use the arithmetic operators (+,-,*,/) with unit values of INTERVAL keyword in MySQL?
- Given an example of C pointer addition and subtraction
- Give properties of addition and subtraction for whole numbers.
- Why in MySQL, we cannot use arithmetic operators like ‘=’, ‘
- Complete the addition-subtraction box.(a)(b)"

Advertisements