0

我有一个查询显示创建数据库的时间,实际上是提前 4 小时。在我的输出中,我希望将时间收回 4 小时以显示正确的时间。日期列被创建为一个不为空的数值 (17,9)。例如 20130627.180846113


然后我将其转换为两个单独的列,显示例如日期:2013-06-28 例如时间:17:11:43

但是我不确定在我的查询中在哪里考虑 -4 小时。任何人都可以就正确的时间显示查询可能会是什么样子提出建议吗?请注意,我使用的是 SQL Server Management Studio 2008。

现有查询:

  WITH CTE AS
  (
  SELECT  CONVERT(DATETIME,LEFT([Date],8)+' '+
                         SUBSTRING([Date],10,2)+':'+
                         SUBSTRING([Date],12,2)+':'+
                         SUBSTRING([Date],14,2)+'.'+
                         RIGHT([Date],3)) [Date],
        ItemId,
        MsgID,
        Parms,
        [OBJECT]
 FROM (  SELECT  ItemId,MsgID,
                CONVERT(VARCHAR(18),[Date]) [Date],
                Parms,
                [OBJECT]
        FROM JnlList
        WHERE UserCode = 'Automation') A
 )
 SELECT  ItemId, MsgID,
    CONVERT(DATE,[Date]) [Date],
    CONVERT(VARCHAR(10),[Date],108) AS [Time],
    CASE WHEN MsgID = '1' THEN SUBSTRING(Parms,1,6) +' Was removed from:'
    WHEN MsgID = '2' THEN 'This Move List has been cleared of all its media'
    ELSE '' -- Optional Else
    END AS [RemovalPath],
    [OBJECT] [Move List:]
 FROM CTE
 WHERE [Date] >= CONVERT(DATE,DATEADD(DAY,-4,GETDATE()))
4

1 回答 1

2

您可以使用 DATEADD() 来调整时间:

DATEADD(hour,-4,timefield)

在您的查询中(假设查询的其余部分很好):

 SELECT  ItemId, MsgID,
    CONVERT(DATE,[Date]) [Date],
    CONVERT(VARCHAR(10),DATEADD(hour,-4,CAST([Date] AS TIME)),108) AS [Time],
    CASE WHEN MsgID = '1' THEN SUBSTRING(Parms,1,6) +' Was removed from:'
    WHEN MsgID = '2' THEN 'This Move List has been cleared of all its media'
    ELSE '' -- Optional Else
    END AS [RemovalPath],
    [OBJECT] [Move List:]
 FROM CTE
 WHERE [Date] >= CONVERT(DATE,DATEADD(DAY,-4,GETDATE()))
于 2013-07-02T15:25:56.343 回答