October 4, 2006 at 12:32 pm
In my sql query, in which I am reading through 9000 records and pulling out SUM (tableX.Quantity) , they seem to parse through fine until a certian point where it breaks saying that type varchar cannot be converted to type real.
Lets say you have something like this
DECLARE @TEMP AS REAL
SET @TEMP = (Select Sum(tableX.Quantity) ..... .... .... )
the SUM(tableX.Quantity) is coming out as real variables for all, but probably breaks when it finds bad data ...
either that or it is coming across negative values.
Can someone suggest a solution for this ?
Thanks
October 4, 2006 at 1:05 pm
What is the datatype of tableX.Quantity? If it is a numeric datatype, a negative number will not cause a cast error. Bad data, in this case, may be bad design. Give us the datatypes of the columns involved and post your code and DDL if possible.
October 4, 2006 at 1:25 pm
October 4, 2006 at 2:10 pm
Just for the record ISNUMERIC is not reliable either
* Noel
October 5, 2006 at 8:01 am
Do you want @Temp to capture the sum of all the quantity? Then your query had a problem.
DECLARE @TEMP AS REAL
SELECT @Temp = SUM(CASE WHEN ISNUMERIC(tableX.Quantity) = 0
THEN 0
ELSE tableX.Quantity
END )
FROM Table
October 5, 2006 at 3:13 pm
Thanks folks..it sure helped !!!!!
October 5, 2006 at 5:44 pm
Oh... no, no, no, no... ISNUMERIC does not adequately test for IS A NUMBER or IS ALL DIGITS...
Try this...
SELECT ISNUMERIC('$1,000')
SELECT CAST('$1,000' AS MONEY)
SELECT CAST('$1,000' AS INT)
GO
SELECT ISNUMERIC('0d05')
SELECT CAST('0d05' AS INT)
GO
Notice that both uses of ISNUMERIC return a 1 which most people take to mean as "IS ALL DIGITS" or even ISNUMERIC... you've got to be really careful with ISNUMERIC... for example, notice that the conversion to MONEY above works but the conversion to INT with the same value does not work?
And, there are more than two dozen characters and patterns of characters that ISNUMERIC will return a "1" for that are not numeric digits.
If you want to test for "IS ALL DIGITS", you must use something like the following...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply