- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Update the date and time values while inserting them in MySQL
Here, we will see an example wherein we are inserting datetime and updating them while using INSERT query.
Let us first create a table −
mysql> create table DemoTable816 (DueDate datetime); Query OK, 0 rows affected (0.45 sec)
Insert some records in the table using insert command. Here is the query to add (minutes / hours / days / months / years) to date when performing INSERT −
mysql> insert into DemoTable816 values(date_add(now(),interval 3 minute)); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable816 values(date_add('2018-01-21 00:00:00',interval 3 Hour)); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable816 values(date_add('2016-11-11 12:40:00',interval 3 Day)); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable816 values(date_add('2018-12-01 11:00:00',interval 3 Month)); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable816 values(date_add('2011-03-21 10:00:00',interval 3 Year)); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable816;
This will produce the following output -
+---------------------+ | DueDate | +---------------------+ | 2019-08-03 13:08:43 | | 2018-01-21 03:00:00 | | 2016-11-14 12:40:00 | | 2019-03-01 11:00:00 | | 2014-03-21 10:00:00 | +---------------------+ 5 rows in set (0.00 sec)
- Related Articles
- In MySQL, how can I insert date and time automatically while inserting NULL values to the other columns?
- Set particular value of column without using update and while inserting values in MySQL
- Format date while inserting records in MySQL
- How can we insert current date and time automatically on inserting values in other columns in MySQL?
- Can we skip a column name while inserting values in MySQL?
- Can we use INTERVAL keyword while inserting date records in a MySQL table?
- MySQL queries to update date records with NULL values
- How to update the date format in MySQL?
- Update MySQL date and increment by one Year?
- When running UPDATE … datetime = NOW(); will all rows updated have the same date/ time in mysql?
- Update record on a specific date matching the current date in MySQL
- How to convert positive value to negative while inserting in MySQL?
- Display an error while inserting duplicate records in a MySQL table
- Get distinct values and count them in MySQL
- How to fix the incorrect datetime value while inserting in a MySQL table?

Advertisements