[英]How to compare datetime range between 4 datetime columns in SQL Server
I have 4 datetime columns in my table way_point
. 我的表
way_point
有4个日期时间列。 The four columns are 四列是
scheduled_a, checkin_time, no_later_than, no_earlier_than
Now I want to see if values in scheduled_a
, checkin_time
fall between the range of no_later_than
and no_earlier_than
columns. 现在我想查看
scheduled_a
, checkin_time
值是否介于no_later_than
和no_earlier_than
列的范围之间。
This is the code that I have written but I get this error: 这是我写的代码,但是我收到了这个错误:
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
在预期条件的上下文中指定的非布尔类型的表达式,在'和'附近。
Code: 码:
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
is here another column which I must use as a condition in where. Position
在这里是另一个列,我必须在其中使用条件。 But mostly I am stuck at comparing the datetime columns. 但大部分时间我都被困在比较日期时间列。
Any idea where I might be wrong please? 知道我哪里错了吗? Thank you.
谢谢。
You were almost there. 你快到了。 You need to split it into two different conditions.
您需要将其拆分为两个不同的条件。 Can't say whether no_later_than or no_earlier_than is bigger.
不能说no_later_than或no_earlier_than是否更大。 After
between
the first argument needs to be the smaller one. 在第一个参数
between
需要较小的一个。 For example if no_later_than = '2016-02-22'
and no_earlier_than = '2016-02-23'
then no_later_than
is the smaller argument. 例如,如果
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.