我有一个相当长的 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'.