有人可以帮我解决一个SQL查询,该查询将告知特定时间是否在给定的时间范围内。
start_time = 20140304 11:00:00
end_time = 20140303 08:00:00
如果我给20140303 07:00:00
,它应该说它是否在上述时间范围内。对于当前场景,它应该传递并打印“是”
更新 这些值是varchar格式
答案 0 :(得分:2)
这是一个where
子句:
where (start_time < end_time and @VALUE between start_time and end_time) or
(start_time > end_time and @VALUE between end_time and start_time)
我认为您想要匹配,无论start_time
是end_time
之前还是之后。
编辑:
如果您想在select
中使用此功能,请使用case
:
select (case when (start_time < end_time and @VALUE between start_time and end_time) or
(start_time > end_time and @VALUE between end_time and start_time)
then 1 else 0
end) as Flag