SQL | Timestamp in where clause to select the date range
Select date range from the timestamp column with where clause can be done by using the BETWEEN operator.
BETWEEN Operator :
The BETWEEN operator selects values within a given range.
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 created between '2020-05-05 11:30:00' and '2020-05-6 18:30:00'
SELECT *
FROM Employee
WHERE CREATED_DATE
BETWEEN '2020-05-05 11:30:00' AND '2020-05-06 18:30:00'
The above query generates the following output
+----+----------+----------+----------------------+ | 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 | | 5 | Komal | 8500.00 | 2020-05-05 11:35:25 | +----+----------+----------+----------------------+