“CAST”附近的语法错误,预期为“AS”

时间:2016-02-18 19:20:28

标签: sql-server tsql

我有这个错误:

  

“CAST”附近的语法不正确,预期为“AS”。

SELECT IdAnnoucement, AnnoucementNAme, AnnoucmentDetails, DateOn, DateOff, 
active, appID FROM tblAnnoucemnt WHERE DateOn >= 
CAST(convert(nvarchar,CAST(GETDATE() as date))+'00:00:01') as datetime)) AND 
DateOff <= CAST(convert(nvarchar,CAST(GETDATE() AS date))+'23:59:01') as
datetime)) ORDER BY IdAnnoucement DESC

2 个答案:

答案 0 :(得分:3)

你不仅要注意括号,还要注意' 00:00...'的空间。这是为了准备正确转换的日期所必需的。

SELECT idannoucement, 
       annoucementname, 
       annoucmentdetails, 
       dateon, 
       dateoff, 
       active, 
       appid 
FROM   tblannoucemnt 
WHERE  dateon >= CAST(CONVERT(NVARCHAR, CAST(getdate() AS DATE)) + ' 00:00:01' AS DATETIME)
AND 
dateoff <= CAST(CONVERT(NVARCHAR, CAST(getdate() AS DATE)) + ' 23:59:01' AS DATETIME) ORDER BY idannoucement DESC

答案 1 :(得分:2)

检查括号

SELECT  IdAnnoucement ,
        AnnoucementNAme ,
        AnnoucmentDetails ,
        DateOn ,
        DateOff ,
        active ,
        appID
FROM    tblAnnoucemnt
WHERE   DateOn >= CAST(CONVERT(NVARCHAR, CAST(GETDATE() AS DATE)) + '00:00:01' AS DATETIME)
        AND DateOff <= CAST(CONVERT(NVARCHAR, CAST(GETDATE() AS DATE))
        + '23:59:01' AS DATETIME)
ORDER BY IdAnnoucement DESC