Computer >> Computer tutorials >  >> Programming >> MySQL

MySQL query to set current date in the datetime field for all the column values


Let us first create a table −

mysql> create table DemoTable821(AdmissionDate datetime);
Query OK, 0 rows affected (1.24 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable821 values('2019-01-21');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable821 values('2018-11-02');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable821 values('2016-12-31');
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable821 values('2015-03-19');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable821;

This will produce the following output −

+---------------------+
| AdmissionDate       |
+---------------------+
| 2019-01-21 00:00:00 |
| 2018-11-02 00:00:00 |
| 2016-12-31 00:00:00 |
| 2015-03-19 00:00:00 |
+---------------------+
4 rows in set (0.00 sec)

Here is the query to set current date in the DateTime field −

mysql> update DemoTable821 set AdmissionDate=CURDATE();
Query OK, 4 rows affected (0.74 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Let us check table records once again −

mysql> select *from DemoTable821;

This will produce the following output −

+---------------------+
| AdmissionDate       |
+---------------------+
| 2019-08-03 00:00:00 |
| 2019-08-03 00:00:00 |
| 2019-08-03 00:00:00 |
| 2019-08-03 00:00:00 |
+---------------------+
4 rows in set (0.00 sec)