我在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;
此查询显示每个日期,但我只对当前日期前一天的日期感兴趣。因此,可以发送包含前一天活动的日常报告。
答案 0 :(得分: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;
答案 1 :(得分: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;