Calling NOW() function to fetch current date records in MySQL?


Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ShippingDate datetime
   -> );
Query OK, 0 rows affected (1.16 sec)

Insert some records in the table using insert command. Consider current date “2019-06-28” −

mysql> insert into DemoTable(ShippingDate) values('2019-01-31');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(ShippingDate) values('2019-06-06');
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable(ShippingDate) values('2019-06-28');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable(ShippingDate) values('2019-07-01');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+---------------------+
| Id | ShippingDate        |
+----+---------------------+
|  1 | 2019-01-31 00:00:00 |
|  2 | 2019-06-06 00:00:00 |
|  3 | 2019-06-28 00:00:00 |
|  4 | 2019-07-01 00:00:00 |
+----+---------------------+
4 rows in set (0.00 sec)

Here is the query to call NOW() function in MySQL and fetch the current date record from the table −

mysql> select *from DemoTable where Date(ShippingDate)=Date(now());

This will produce the following output −

+----+---------------------+
| Id | ShippingDate        |
+----+---------------------+
|  3 | 2019-06-28 00:00:00 |
+----+---------------------+
1 row in set (0.00 sec)
raja
Published on 12-Jul-2019 11:31:31
Advertisements