如何根据时间限制获取最后一天和今天的数据

时间:2017-10-13 07:50:20

标签: sql sql-server tsql

我这里有样本数据:

ID  Val            dt
1   Mohan   2017-10-13 13:02:49.493
2   Manasa  2017-10-12 20:02:49.493
3   maren   2017-10-13 18:02:49.493

当我今天下午2点发表声明时,它应该在数据之后的最后一天下午6点给出结果集,今天的数据到目前为止,当我今天晚上7点运行声明时,应该给出今天的结果。下午6点后的数据。

如果我今天下午2点运行它,结果将是这样的

ID  Val            dt
1   Mohan   2017-10-13 13:02:49.493
2   Manasa  2017-10-12 20:02:49.493

如果我今天晚上7点跑步:

ID  Val            dt
3   maren   2017-10-13 18:02:49.493

基本的事情,如果我在下午6点之前运行它应该在下午6点之前提供最后一天的数据,如果我在下午6点之后运行它应该在今天下午6点之后提供数据。我尝试了 DATE DIFF条件,但我无法证明结果的合理性 - 有人可以建议吗?

表脚本

Declare @tab table 
(ID INT,Val Varchar(10),dt datetime)

Insert into @tab (ID,val,dt)
values (1,'Mohan','2017-10-13 13:02:49.493'),
(2,'Manasa','2017-10-12 20:02:49.493'),
(3,'maren','2017-10-13 18:02:49.493') 

5 个答案:

答案 0 :(得分:1)

你可以通过创建模拟你的日“窗口”的两个日期来实现这一点,即昨天下午6点和今天下午6点,根据当前时间是在当前时间下午6点之前还是之后调整昨天/今天的概念。然后,您只需选择dt介于这两个日期之间的数据(或使用< =和>或您需要包含或排除相关行的任何内容),例如....

DECLARE @t table (ID int,Val varchar(30),dt datetime2(3))
INSERT @t(ID,Val,dt)
VALUES(1,'Mohan' ,'2017-10-13T19:02:49.493'),
      (2,'Manasa','2017-10-12T20:02:49.493'),
      (3,'maren' ,'2017-10-13T07:02:49.493');

DECLARE @now datetime2 = dateadd(HOUR,12,sysdatetime());

SELECT  *,
        @now,
        x.Today6pm,
        x.Yesterday6pm
FROM @t AS t
CROSS APPLY (VALUES(DATEADD(day, DATEDIFF(day,'19000101',cast(@now AS date))-(CASE WHEN cast(@now AS time) < timefromparts(18,00,00,0,0) THEN 1 ELSE 0 END), CAST(timefromparts(18,00,00,0,0) AS DATETIME2(7))),
                    DATEADD(day, DATEDIFF(day,'19000101',cast(@now AS date))+(CASE WHEN cast(@now AS time) > timefromparts(18,00,00,0,0) THEN 1 ELSE 0 END), CAST(timefromparts(18,00,00,0,0) AS DATETIME2(7))))) x(Yesterday6pm,Today6pm)
WHERE dt BETWEEN x.Yesterday6pm AND x.Today6pm

CROSS APPLY这里是为了简化代码,否则你可以将这些函数直接放在WHERE谓词中。

变量@now仅用于测试以确保调整工作,而不是等到今晚6:01 pm。显然,在您的代码中,只需将@now替换为sysdatetime()

注意:如果您的日期是日期时间,那么一起添加日期和时间“有效”,但它不适用于datetime2。上述方法适用于datetime和datetime2,因此更具弹性。

答案 1 :(得分:1)

DECLARE @T TABLE
(
    ID  INT,
    Val VARCHAR(50)  ,        
    dt  DATETIME
)
INSERT INTO @T
VALUES
(1,   'Mohan  ', '2017-10-13 13:02:49.493'),
(2,   'Manasa ', '2017-10-12 20:02:49.493'),
(3,   'maren  ', '2017-10-13 18:02:49.493')

DECLARE @CURRENT datetime  = '13 oct 2017 18:00'
SELECT 
*
FROM @T
WHERE 
    (
        CAST(@CURRENT  as time) BETWEEN '00:00' AND '18:00' AND
        dt  BETWEEN  DATEADD(day,-1,DATEADD(hh,18,CAST(CAST(@CURRENT as date) as datetime))) AND DATEADD(hh,18,CAST(CAST(@CURRENT as date) as datetime))
    ) OR 
    (
        CAST(@CURRENT  as time) NOT BETWEEN '00:00' AND '18:00' AND
        dt >=DATEADD(hh,18,CAST(CAST(@CURRENT as date) as datetime))
    )

答案 2 :(得分:1)

您可以尝试使用此脚本。

SELECT * FROM MyTable WHERE 
( (CAST(GETDATE() AS TIME) < '18:00') 
    AND dt < DATEADD(HOUR,18, CAST(CAST(GETDATE() AS DATE) AS DATETIME) ) )
OR 
(( CAST(GETDATE() AS TIME) >= '18:00' ) 
    AND dt >= DATEADD(HOUR,18, CAST(CAST(GETDATE() AS DATE) AS DATETIME) ) )

答案 3 :(得分:0)

试试这个:

DECLARE @Now DATETIME, @FromDate DATETIME
SET @Now = GETDATE()
SET @FromDate = DATEADD( HOUR, 18, DATEADD( DAY, DATEDIFF( DAY, 0, @Now )
    - ( CASE WHEN DATEPART( HOUR, @Now ) < 18 THEN 1 ELSE 0 END ), 0 ))

SELECT @Now, @FromDate

SELECT *
FROM @tab
WHERE @FromDate < dt AND dt <= @Now

结果:

Now                     FromDate
----------------------- -----------------------
2017-10-13 19:36:40.963 2017-10-13 18:00:00.000

ID          Val        dt
----------- ---------- -----------------------
3           maren      2017-10-13 18:02:49.493

说明:

@FromDate的计算方法如下:

  • ( CASE WHEN DATEPART( HOUR, @Now ) < 18 THEN 1 ELSE 0 END ) - 如果小时小于18(下午6点),则返回1
  • DATEADD( HOUR, 18, DATEADD( DAY, DATEDIFF( DAY, 0, @Now ) - ... , 0 )) - 返回“6PM日期”,即昨天或今天,具体取决于上面CASE表达式的结果

答案 4 :(得分:-3)

你可以试试这个:

SELECT * FROM [yourTable] WHERE
(dt between CONCAT(CAST(GETDATE() AS DATE), ' ', '18:00:00.000') AND 
   CONCAT(CAST(GETDATE() AS DATE), ' ', '23:59:59.999')
   AND HOUR(GETDATE()) > 18)
OR
(dt between CONCAT(CAST(dt AS DATE) - INTERVAL 1 DAY, ' ', '18:00:00.000')
       AND GETDATE()
   AND HOUR(GETDATE()) < 18)