Compare timestamp with date only

Last updated 1 year, 2 months ago | 364 views 75     5

Tags:- SQL MySQL

SQL | Compare timestamp with date only

Timestamp holds Date and Time both with 19 characters. To filter table record with the only date parameter can be achieved by SQL DATE() function.

Consider the Employee table having the following records −

+----+----------+----------+----------------------+
| ID | NAME     | SALARY   | CREATED_DATE         |
+----+----------+----------+----------------------+
|  1 | Kamal    |  3000.00 |  2020-05-05 18:35:11 |
|  2 | Roshan   |  3000.00 |  2020-05-06 09:50:01 |
|  3 | Shyam    |  2000.00 |  2020-05-05 10:30:20 |
|  4 | Sagar    |  6500.00 |  2020-05-07 09:40:10 |
|  5 | Komal    |  8500.00 |  2020-05-05 11:35:25 |
+----+----------+----------+----------------------+

The following SQL query will select all the record having CREATED_DATE is "2020-05-05" 

SELECT * 
FROM Employee 
WHERE DATE(CREATED_DATE) = "2020-05-05"

The above query generates the following output:-

+----+----------+----------+----------------------+
| ID | NAME     | SALARY   | CREATED_DATE         |
+----+----------+----------+----------------------+
|  1 | Kamal    |  3000.00 |  2020-05-05 18:35:11 |
|  3 | Shyam    |  2000.00 |  2020-05-05 10:30:20 |
|  5 | Komal    |  8500.00 |  2020-05-05 11:35:25 |
+----+----------+----------+----------------------+