Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi guys can you help me on this one, i need to check if the given time range has a time between 10PM and 6AM,

for example:
Given Time: 7PM - 4AM


I want to output at the time between 10PM - 6AM on that given time range.

What I have tried:

SQL
Case
		When CAST(c.Start_Time as Time) <= CAST('10:00 PM' as Time) and Cast(d.End_Time as Time) <= CAST('06:00 AM' as Time) Then 1 
		Else 0
	end as [HoursWorked w/ ND],
Posted
Updated 28-Jun-16 18:25pm

Use the datepart function of SQL. See DATEPART (Transact-SQL)[^]

As example

SQL
Select case when datepart(hh, start_time) > 10 or datepart(hh, starttime) < 5 then 'Out of hours' else ' work hours' end as x from XXX


This assumes you are using a datetime field. The area to be careful with is the end date because the exact time of 6am will not be included but 5:59:59 will be.

If you really need 6:00:00 you will need to extent the query in a similar manner to include 6:00:00 but excluded anything beyond 6:00:00
 
Share this answer
 
I am not exactly sure but you can try this
SQL
SELECT * FROM TABLE
WHERE DATE BETWEEN '06/29/2016 10:00:00' and '06/29/2016 06:00:00'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900