Insert current date to the database in MySQL?


To insert current date to the database, you can use NOW(). Following is the syntax −

INSERT INTO yourTableName(yourDateColumnName) VALUES(NOW());

If your column has datatype date then NOW() function inserts only current date, not time and MySQL will give a warning. To remove the warning, you can use CURDATE().

Let us first create a table −

mysql> create table insertcurrentdate
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> currentDate date
   -> );
Query OK, 0 rows affected (1.09 sec)

Following is the query to insert some records in the table using insert command. We have used both NOW() and CURDATE() to display current date −

mysql> insert into insertcurrentdate(currentDate) values(NOW());
Query OK, 1 row affected, 1 warning (0.24 sec)

mysql> insert into insertcurrentdate(currentDate) values(CURDATE());
Query OK, 1 row affected (0.18 sec)

Following is the query to display all records from the table using select statement −

mysql> select * from insertcurrentdate;

This will produce the following output −

+----+-------------+
| Id | currentDate |
+----+-------------+
| 1  | 2019-04-05  |
| 2  | 2019-04-05  |
+----+-------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements