Insert current date in datetime format MySQL?


To insert the current date (not time), then you can use in-built function CURDATE() from MySQL. The syntax is as follows −

INSERT INTO yourTableName values(curdate());

Or if you want to add date and time both then you can use the in-built function NOW() from MySQL. The syntax is as follows −

INSERT INTO yourTableName values(now());

To understand both the syntax, let us first create a table. The query to create a table is as follows −

mysql> create table NowAndCurdateDemo
   −> (
   −> YourDueDate datetime
   −> );
Query OK, 0 rows affected (1.75 sec)

Implement both the functions to insert the current date and date-time the table. The query to insert date is as follows −

mysql> insert into NowAndCurdateDemo values(curdate());
Query OK, 1 row affected (0.28 sec)

mysql> insert into NowAndCurdateDemo values(now());
Query OK, 1 row affected (0.14 sec)

Check whether the data is inserted or not in the table using the following query −

mysql> select *from NowAndCurdateDemo;

The following is the output −

+---------------------+
| YourDueDate         |
+---------------------+
| 2018-12-05 00:00:00 |
| 2018-12-05 21:24:10 |
+---------------------+
2 rows in set (0.06 sec)

Updated on: 30-Jul-2019

794 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements