簡體   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