Sql – How to get records after a certain time using SQL datetime field

datetimesqlsql server

If I have a datetime field, how do I get just records created later than a certain time, ignoring the date altogether?

It's a logging table, it tells when people are connecting and doing something in our application. I want to find out how often people are on later than 5pm.

(Sorry – it is SQL Server. But this could be useful for other people for other databases)

Best Answer

For SQL Server:

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

See http://msdn.microsoft.com/en-us/library/aa258265(SQL.80).aspx.