[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Excel MS Query - Issue with Cast

Posted on 2011-10-20
Medium Priority
893 Views
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
Question by:holemania

LVL 39

Expert Comment

ID: 37002358
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

LVL 8

Accepted Solution

ID: 37005330
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 Closing Comment

ID: 37012894
Thanks didn't even catch that.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
###### Suggested Courses
Course of the Month19 days, 3 hours left to enroll