views:

24

answers:

1

Hi,

Today, I have started writing a query which is as given below:


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

Above query returned me with the expected results. Then I changed the output of IP2.Quote column in such a way that it should return the decimal points based on another column NDP in the Instruments table. The changed query is as given below:


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

The second query returned a syntactical return which is as given below:


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

I have no clue on the error returned by SQLServer.

Please help!!!

Thanks, Mahesh

+1  A: 

You seem to be trying to specify the scale of the decimal in the cast based on a value from a column, you just can't do this it is not valid TSQL.

Ben Robinson
Thanks for your reply. I have ended doing the rounding in .NET code.
Mahesh