• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1057

# Excel MS Query - Issue with Cast

I'm trying to query some items and ran into an issue.  I have the following:

SELECT ID, CAST((COST/QTY) AS DECIMAL(6,2) AS TOTAL,
X.DATE,
FROM   ITEM
INNER JOIN
(SELECT PART_ID, CAST((COST/QTY)) AS DECIMAL(6,2) AS TOTAL, DATE
FROM ITEM_TRANS WHERE TYPE = 'I' AND CLASS='R')  X ON X.TOTAL = CAST((COST/QTY) AS DECIMAL(6,2))
AND X.PART_ID = X.ID
WHERE  TYPE = 'O' AND CLASS = 'I'
AND ID = 'ABC888'

Incorrect column expression: 'CAST(COST/QTY) AS DECIMAL(6,2))'.

If I can't use cast, what can I use to convert my calculation so that it is 2 decimal places?
0
holemania
1 Solution

Commented:
use round(,2), as in
``````SELECT ID, round(COST/QTY,2) AS TOTAL,
X.DATE
FROM   ITEM
INNER JOIN
(SELECT PART_ID, CAST((COST/QTY)) AS DECIMAL(6,2) AS TOTAL, DATE
FROM ITEM_TRANS WHERE TYPE = 'I' AND CLASS='R')  X ON X.TOTAL = CAST((COST/QTY) AS DECIMAL(6,2))
AND X.PART_ID = X.ID
WHERE  TYPE = 'O' AND CLASS = 'I'
AND ID = 'ABC888'
``````

Thomas
0

Commented:
There were missing ')' at two points. I have corrected it. Try following :
SELECT ID, CAST((COST/QTY) AS DECIMAL(6,2)) AS TOTAL,
X.DATE,
FROM   ITEM
INNER JOIN
(SELECT PART_ID, CAST((COST/QTY) AS DECIMAL(6,2)) AS TOTAL, DATE
FROM ITEM_TRANS WHERE TYPE = 'I' AND CLASS='R')  X ON X.TOTAL = CAST((COST/QTY) AS DECIMAL(6,2))
AND X.PART_ID = X.ID
WHERE  TYPE = 'O' AND CLASS = 'I'
AND ID = 'ABC888'

0

Author Commented:
Thanks didn't even catch that.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.