查询从两天前1:01 AM到昨天凌晨1点的所有记录

时间:2016-04-05 13:55:45

标签: sql sql-server

我目前正在尝试将查询编写为SSIS包的一部分,部分要求是查询将返回带有时间戳的所有行(当前位于yyyy-mm-dd hh:mm:ss: mmm格式)发生在1:01 AM 2天前到1 AM 1天前。我可以写这个日期,但我坚持如何纳入小时范围。当前查询:

SELECT *
FROM
(SELECT dateadd(S, logintime, '1970-01-01') as "conversion"
,DATEPART(hh, dateadd(HOUR, logintime, '1970-01-01')) as "HourCol"
,DATEPART(hh, dateadd(MINUTE, logintime, '1970-01-01')) as "MinuteCol"
, * FROM my.logtable
WHERE row_date between dateadd(day, -3, getdate()) and DATEADD(day, -1, GETDATE())) as subselect
where conversion between --and from here I'm lost.
order by conversion

1 个答案:

答案 0 :(得分:1)

SELECT getdate() today,
       CAST(getdate() as DATE) today_0000,       
       DATEADD(day, -1, CAST(getdate() as DATE)) yesterday0000,
       DATEADD(day, -2, CAST(getdate() as DATE)) daybeforyesterday0000,
       DATEADD(mi, 60, CAST(DATEADD(day, -1, CAST(getdate() as DATE)) as datetime)) yesterday0100,
       DATEADD(mi, 61, CAST(DATEADD(day, -2, CAST(getdate() as DATE)) as datetime)) daybeforyesterday0101

最后两个为您提供您正在寻找的范围。只要你比较另一个日期时间就可以了。

enter image description here