在指定的时间范围内预订

时间:2016-07-19 09:28:47

标签: sql sql-server sql-server-2008

这是我的表格的一部分,其中包含

Post_time              Post_date     
------------------------------------
06:13:15.0000000       2015-07-15    
22:13:15.0000000       2015-07-15
03:13:15.0000000       2015-07-16
..........             ..........
..........             ..........

我需要在第二天的22:00到05:00之间获得所有信息的时间戳。所以这意味着发布日期改变了,这是另一天。而且我不仅要求所有表格都在这个日期。

我试过了:

SELECT Post_time FROM dbo.My_Table WHERE
Post_time between dateadd(HH,22,cast(cast(Post_date as date) as datetime))    and
 dateadd(HH,29,cast(cast(Post_date as date) as datetime))

但我得到了这个:

The data types time and date are incompatible in the greater than or equal to operator.

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

SELECT Post_time FROM dbo.My_Table WHERE
Post_date+Post_time between dateadd(HH,22,cast(cast(Post_date as date) as datetime))    and
 dateadd(HH,29,cast(cast(Post_date as date) as datetime))

或者施放Post_Time

SELECT Post_time FROM dbo.My_Table WHERE
Post_date+cast(Post_time as datetime) between dateadd(HH,22,cast(cast(Post_date as date) as datetime))    and
 dateadd(HH,29,cast(cast(Post_date as date) as datetime))

实际上,如果您只是检查时间,则不需要Post_date

   SELECT Post_time 
     FROM dbo.My_Table 
    WHERE
    CAST('2001-01-01' as datetime) + CAST(Post_time AS datetime) 
    between cast('2001-01-01 22:00' AS datetime) AND
    between cast('2001-01-02 05:00' AS datetime)