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

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
Asked:
holemania
1 Solution
 
nutschCommented:
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' 

Open in new window


Thomas
0
 
VipulKadiaCommented:
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
 
holemaniaAuthor 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.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now