0

我有一个相当长的 SQL 查询,它将数字日期数据类型转换为日期时间值,并根据我的要求进一步指定搜索。但是,我收到了一些我似乎无法解决的错误,我想知道是否有人可以修改我的查询以使其无错误运行。

这是我的查询:

SELECT [Object] AS [Dataset],
CAST(DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))) AS DATE) 'Date',
  LEFT(CAST(DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))) AS TIME),8) 'Time',
 MsgId,
 Parms,
CASE WHEN MsgID = '61' THEN SUBSTRING(Parms,35,6)
    ELSE '' --Optional ELSE
END  AS [Dataset],
FROM ( SELECT  ItemId,
CONVERT(VARCHAR(18),[Date]) [Date],
[Object],
MsgID,
Parms,
FROM JnlDataSection
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 SUBSTRING(Parms,35,6) = 'X05219'
ORDER BY [DATE] DESC;

这些是错误:

Msg 156, Level 15, State 1, Line 17
Incorrect syntax near the keyword 'FROM'.

Msg 156, Level 15, State 1, Line 22

Please Note: I'm using SQL Server Management Studio 2008
Incorrect syntax near the keyword 'FROM'.
4

3 回答 3

2

您在 FROM 之前有一个额外的逗号:

END  AS [Dataset],

应该只是

END  AS [Dataset]
于 2013-07-15T16:04:25.540 回答
1

去掉上面 lilne 上的 [Dataset] 之后的逗号。还

Parms,
FROM JnlDataSection

Parms 后面不应该有逗号

于 2013-07-15T16:04:03.047 回答
0

尝试删除 from 子句之前的“,”。新查询应如下所示...

SELECT [Object] AS [Dataset],
  CAST(DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
  SUBSTRING([Date],10,2)+':'+
  SUBSTRING([Date],12,2)+':'+
  SUBSTRING([Date],14,2)+'.'+
  SUBSTRING([Date],15,3))) AS DATE) 'Date',
  LEFT(CAST(DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
 SUBSTRING([Date],10,2)+':'+
 SUBSTRING([Date],12,2)+':'+
 SUBSTRING([Date],14,2)+'.'+
 SUBSTRING([Date],15,3))) AS TIME),8) 'Time',
 MsgId,
 Parms,
 CASE WHEN MsgID = '61' THEN SUBSTRING(Parms,35,6)
 ELSE '' --Optional ELSE
 END  AS [Dataset]
FROM ( SELECT  ItemId,
CONVERT(VARCHAR(18),[Date]) [Date],
[Object],
MsgID,
Parms
FROM JnlDataSection
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 SUBSTRING(Parms,35,6) = 'X05219'
ORDER BY [DATE] DESC;
于 2013-07-15T16:04:50.430 回答