如果一个时间(格式:yymmdd小时:分钟:秒)在一个时间范围内如何在SQL中找到?

时间:2014-03-04 17:14:22

标签: sql sybase

有人可以帮我解决一个SQL查询,该查询将告知特定时间是否在给定的时间范围内。

start_time = 20140304 11:00:00
end_time   = 20140303 08:00:00

如果我给20140303 07:00:00,它应该说它是否在上述时间范围内。对于当前场景,它应该传递并打印“是”

更新 这些值是varchar格式

1 个答案:

答案 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_timeend_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