【发布时间】:2018-04-13 05:38:03
【问题描述】:
我有如下数据,
尝试在查询下方运行但返回 0 行, 下面的查询应返回如上所示突出显示的行数据。
谁能解释一下,我错过了什么?
select * from Flt_OperativeFlight_SchedulePeriods
where
(
(cast('2018-04-05' as date) between cast(ScheduleStartDate as date) and cast(ScheduleEndDate as date) )
or
(cast('2018-04-11' as date) between cast(ScheduleStartDate as date) and cast(ScheduleEndDate as date) )
)
and CarrierCode='SQ' and FlightNumber='0004'
【问题讨论】:
-
您的
2018-04-05比突出显示的行中的范围早 1 天,2018-04-11比突出显示的行晚 1 天。因此,没有一个语句是正确的,因此整个 where 语句对于突出显示的行返回 false。为了更好地解释,第一个BETWEEN转换为:ScheduleStartDate <= 2018-04-05 <= ScheduleEndDate这是不正确的。第二个BETWEEN声明也是如此。
标签: sql sql-server tsql date compare