如何将列名放在sql server CAST数据类型中?

时间:2010-08-06 08:56:51

标签: sql-server decimal casting

今天,我已经开始编写一个查询,如下所示:


SELECT IP1.InstId,CAST(IP2.Quote AS DECIMAL(10,2)) AS 'CurrentPrice',IP1.Quote,(IP2.Quote-IP1.Quote) AS 'Change' 
FROM InstrumentPrices IP1
INNER JOIN InstrumentPrices IP2
ON IP1.InstId=IP2.InstId
INNER JOIN Instruments I
ON I.Id=IP2.InstId
INNER JOIN Games G
ON G.Id=IP1.GameId
AND G.CurrentPeriod-2=IP1.Period
AND G.CurrentPeriod-1=IP2.Period

以上查询返回了我的预期结果。然后我更改了IP2.Quote列的输出,使得它应该根据Instruments表中的另一列NDP返回小数点。更改的查询如下所示:


SELECT IP1.InstId,CAST(IP2.Quote AS DECIMAL(10,I.NDP)) AS 'CurrentPrice',IP1.Quote,(IP2.Quote-IP1.Quote) AS 'Change' 
FROM InstrumentPrices IP1
INNER JOIN InstrumentPrices IP2
ON IP1.InstId=IP2.InstId
INNER JOIN Instruments I
ON I.Id=IP2.InstId
INNER JOIN Games G
ON G.Id=IP1.GameId
AND G.CurrentPeriod-2=IP1.Period
AND G.CurrentPeriod-1=IP2.Period

第二个查询返回一个语法返回,如下所示:


Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'I'.

我对SQLServer返回的错误一无所知。

请帮助!!!

谢谢,Mahesh

1 个答案:

答案 0 :(得分:1)

您似乎试图根据列中的值指定强制转型中的小数位,您只是不能这样做它是无效的TSQL。