Sql server 如何在sqlsever CAST数据类型中放置列名?

Sql server 如何在sqlsever CAST数据类型中放置列名?,sql-server,decimal,casting,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=

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


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

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'.
上面的查询返回了我预期的结果。然后,我更改了IP2.Quote列的输出,它应该基于Instruments表中的另一列NDP返回小数点。更改后的查询如下所示:


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

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'.
第二个查询返回一个语法返回,如下所示:


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

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

您似乎试图根据列中的值指定强制转换中的小数比例,但您无法这样做,因为TSQL无效。

您似乎试图根据列中的值指定强制转换中的小数比例,您不能这样做,因为它不是有效的TSQL。

您实际上是在告诉它改变每行的列数据类型。我怀疑这是可能的。您可能需要选择一种覆盖所有大小写的数据类型,并将数字四舍五入为i.ndp数字,或者如果只是为了显示,请将结果再次转换为字符串并返回字符列。实际上,您是在告诉它每行改变列的数据类型。我怀疑这是可能的。您可能需要选择一种涵盖所有情况的数据类型,并将数字舍入为i.ndp数字,或者如果只是为了显示,请将结果再次转换为字符串并返回字符列。谢谢您的回复。我已经结束了对.NET代码的舍入。谢谢你的回复。我已经结束了.NET代码中的舍入操作。