Given this sample data set:
Date_Time
10/1/2011 12:05:00 AM
10/6/2011 2:25:00 AM
10/3/2011 5:59:00 AM
10/19/2011 6:01:00 PM
11/5/2011 8:30:00 PM
11/30/2011 11:59:00 PM
Is there a date time function in SQL server (2005 or 2008) that can count the number of records in these two time ranges? Below is the result from above data set.
Please let me know if a website that shows this as well.
Midnight-6:00am 6:00pm – Midnight.
3 3
Note, you want the second range to go up to 11:59pm, not midnight… Use
DATEPART()to get just the hour portion of the dates and use the aggregateSUM()to add 1 to the total for each range when it is met. So what you need are hours between 0 and 5 (00:00 – 05:59) and 18 and 23 (18:00 – 23:59)Update
The above counts up to 05:59:59am. If this really should count up to 06:00:00, you need to also check for the individual time parts for 6:00. This might be slow.
Alternatively, it might be faster to use