我怎么知道给定的时间范围是否在晚上10点到下午6点之间? [英] How will I know if the given time range has between time of 10PM and 6PM?

查看:220
本文介绍了我怎么知道给定的时间范围是否在晚上10点到下午6点之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,你可以帮我解决这个问题,我需要检查一下给定的时间范围是否在晚上10点到早上6点之间,



例如:

给定时间:晚上7点 - 凌晨4点





我想在晚上10点到早上6点之间输出给定时间范围。



我尝试过:



 案例 
何时 CAST(c.Start_Time as 时间)< = CAST(' 10:00 PM' as 时间 Cast(d.End_Time as 时间)< = CAST(' 06:00 AM' as 时间然后 1
否则 0
end as [HoursWorked w / ND],

解决方案

使用SQL的datepart函数。请参阅 DATEPART(Transact-SQL) [ ^ ]



例如< br $> b $ b

 选择  case   datepart(hh,start_time)>  10   datepart(hh,starttime)<  5  然后 ' 非工作时间  else  ' 工作时间 结束  as  x 来自 XXX 





这假设您使用的是日期时间字段。要注意的区域是结束日期,因为早上6点的确切时间不包括在内,但是5:59:59将是。



如果你真的需要6 :00:00您将需要以类似的方式扩展查询以包括6:00:00但排除超出6:00:00的任何内容


我不完全确定但您可以试试这个

  SELECT  *  FROM  < span class =code-keyword> TABLE  
WHERE DATE BETWEEN ' 06/29/2016 10:00:00' ' 06/29/2016 06: 00:00'


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:

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],

解决方案

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

As example

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


I am not exactly sure but you can try this

SELECT * FROM TABLE
WHERE DATE BETWEEN '06/29/2016 10:00:00' and '06/29/2016 06:00:00'


这篇关于我怎么知道给定的时间范围是否在晚上10点到下午6点之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆