The T-SQL DatePart will do the trick:
To get all records from 10:00 - 12:59 :
SELECT * FROM TableA WHERE DATEPART(hh, [OrderTimeStamp]) >= 10 AND DATEPART(hh, [OrderTimeStamp]) < 13
Or if you want to get all records from 10:00 - 13:00 (seconds / milliseconds are omitted):
SELECT * FROM TableA WHERE DATEPART(hh, [OrderTimeStamp]) >= 10 AND DATEPART(hh, [OrderTimeStamp]) < 13 OR (DATEPART(hh, [OrderTimeStamp]) = 13 AND DATEPART(mi, [OrderTimeStamp]) = 0)
Keep in mind that 24h values ββare returned from the DatePart function when used with hh as the format.
See here for more details:
http://msdn.microsoft.com/en-us/library/ms174420.aspx
UPDATE
Since ouy works with SQL 2008, you can use the TIME data type and make your query a lot easier (and right too):
SELECT * FROM TableA WHERE CONVERT(TIME(7), [OrderTimeStamp ]) >= '10:00:00.0000000' AND CONVERT(TIME(7), [OrderTimeStamp ]) <= '13:00:00.0000000'
See here for more details:
http://msdn.microsoft.com/en-us/library/bb677243.aspx
source share