繁体   English   中英

如何比较SQL Server中4个日期时间列之间的日期时间范围

[英]How to compare datetime range between 4 datetime columns in SQL Server

我的表way_point有4个日期时间列。 四列是

scheduled_a, checkin_time, no_later_than, no_earlier_than

现在我想查看scheduled_acheckin_time值是否介于no_later_thanno_earlier_than列的范围之间。

这是我写的代码,但是我收到了这个错误:

在预期条件的上下文中指定的非布尔类型的表达式,在'和'附近。

码:

select 
    customer_id, position 
from 
    way_points 
where 
    position = 2 
    and [scheduled_a] and [checkin_time] between [no_later_than] and [no_earlier_Than]

Position在这里是另一个列,我必须在其中使用条件。 但大部分时间我都被困在比较日期时间列。

知道我哪里错了吗? 谢谢。

你快到了。 您需要将其拆分为两个不同的条件。 不能说no_later_than或no_earlier_than是否更大。 在第一个参数between需要较小的一个。 例如,如果no_later_than = '2016-02-22'no_earlier_than = '2016-02-23'no_later_than是较小的参数。

select customer_id,position 
from way_points where position=2 
and [scheduled_a] between no_later_than and [no_earlier_than]
and [checkin_time] between no_later_than and [no_earlier_than]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM