0

我在 SQL Server Management Studio 2008 上运行了这个 SQL 查询:

  SELECT  ItemId,
    DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
                    SUBSTRING([Date],10,2)+':'+
                    SUBSTRING([Date],12,2)+':'+
                    SUBSTRING([Date],14,2)+'.'+
                    SUBSTRING([Date],15,3))) [Date],
    [Object],
    SourceSite,
    DestSite
   FROM (  SELECT  ItemId,
            CONVERT(VARCHAR(18),[Date]) [Date],
            [Object],
            SourceSite,
            DestSite,
                        UserCode
    FROM JnlMediumMove) A
  WHERE UserCode = 'Automation'
  ORDER BY [Date] DESC;

此查询显示每个日期,但是我只对当前日期前 1 天的日期感兴趣。因此,可以发送包含前一天活动的日常报告。

4

2 回答 2

0

如果您添加到 where 子句:

and datediff(day, [date], getUTCDate()) = 1

需要注意的是 UTC 命令的时区是 GMT。因此,根据您可能需要约会的时间范围,添加小时以获得适当的时区。

因为您的专栏不是实际日期,请尝试执行以下操作:

    and dateDiff(day, CONVERT(DATETIME,
                    LEFT([Date],8)+' '+
                    SUBSTRING([Date],10,2)+':'+
                    SUBSTRING([Date],12,2)+':'+
                    SUBSTRING([Date],14,2)+'.'+
                    SUBSTRING([Date],15,3)
                    ), getUtcDate()) = 1

And isDate( CONVERT(DATETIME,
                    LEFT([Date],8)+' '+
                    SUBSTRING([Date],10,2)+':'+
                    SUBSTRING([Date],12,2)+':'+
                    SUBSTRING([Date],14,2)+'.'+
                    SUBSTRING([Date],15,3)
                    )
           ) = 1

整个查询将是:

SELECT  ItemId,
    DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
                    SUBSTRING([Date],10,2)+':'+
                    SUBSTRING([Date],12,2)+':'+
                    SUBSTRING([Date],14,2)+'.'+
                    SUBSTRING([Date],15,3))) [Date],
    [Object],
    SourceSite,
    DestSite
   FROM (  SELECT  ItemId,
            CONVERT(VARCHAR(18),[Date]) [Date],
            [Object],
            SourceSite,
            DestSite,
                        UserCode
    FROM JnlMediumMove) A
  WHERE UserCode = 'Automation'
  AND dateDiff(day, CONVERT(DATETIME,
                    LEFT([Date],8)+' '+
                    SUBSTRING([Date],10,2)+':'+
                    SUBSTRING([Date],12,2)+':'+
                    SUBSTRING([Date],14,2)+'.'+
                    SUBSTRING([Date],15,3)
                    ), getUtcDate()) = 1

And isDate( CONVERT(DATETIME,
                    LEFT([Date],8)+' '+
                    SUBSTRING([Date],10,2)+':'+
                    SUBSTRING([Date],12,2)+':'+
                    SUBSTRING([Date],14,2)+'.'+
                    SUBSTRING([Date],15,3)
                    )
           ) = 1

        ORDER BY [Date] DESC;   
于 2013-06-24T17:52:54.140 回答
0

尝试这个:

SELECT  ItemId,
DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
                SUBSTRING([Date],10,2)+':'+
                SUBSTRING([Date],12,2)+':'+
                SUBSTRING([Date],14,2)+'.'+
                SUBSTRING([Date],15,3))) [Date],
[Object],
SourceSite,
DestSite
FROM (  SELECT  ItemId,
        CONVERT(VARCHAR(18),[Date]) [Date],
        [Object],
        SourceSite,
        DestSite,
                    UserCode
  FROM JnlMediumMove 
  WHERE CAST(substring(convert(varchar(50), [Date]), 0, 5) + '-' +
  substring(convert(varchar(50), [Date]), 5, 2) + '-' +
  substring(convert(varchar(50), [Date]), 7, 2) AS DATETIME) = 
  CONVERT(date, DATEADD(day, -1, getdate()))) A --Converting to date again to remove the time part
WHERE UserCode = 'Automation'
ORDER BY [Date] DESC;
于 2013-06-24T17:54:46.897 回答