Home > Back-end >  How to get records after a certain time using SQL datetime field (not by whole hour)
How to get records after a certain time using SQL datetime field (not by whole hour)

Time:01-20

I am on SQL Server 2017. If I have a datetime field (systime), how do I get just records created later than a certain time, ignoring the date altogether? An example of the field would be: 2015-05-20 18:23:49.000

I want to find any records at or after 5.30 PM or another scenario, no later than 7.25 PM. How would I be able to get either result?

The below query would give me the result based on the whole hour.

select *
from myTable
where datepart(hh, sysdate) > 17 

Here are more date time examples

2015-08-30 21:34:52.000
2015-09-30 20:20:34.000
2015-11-03 19:53:34.000
2015-12-08 22:49:58.000
2016-02-01 18:47:50.000
2016-02-05 18:41:19.000
2016-02-05 18:41:20.000

CodePudding user response:

You can check if time part is after 17:

Select * from myTable where convert(char(8),myDatetimeField,114) > '17:30:00';

Select * from myTable where convert(char(8), myDatetimeField, 114) <= '19:25:00';
  • Related