Louise
asked on
error converting varchar to float - how to bypass bad data
Hi,
Im performing some calcuations within my select query.
Every now n then one of the values isnt entered correctly and is a varchar - this then returns an error from the stored proc and crashes my report.
heres the part of the query thats the problem:
SELECT
dbo.tbPurchaseItem.mPrice * dbo.tbPurchaseItem.iQty - dbo.tbPurchaseItem.mPrice * dbo.tbPurchaseItem.iQty * (convert(float,dbo.tbPurch aseItem.vC ustom1) /100) AS [cost inc discount],
dbo.tbPurchaseItem.vCustom 1 was entered as 7,5 and crashed the report.
There are now checks to enure this wont happen again, however for older orders this may happen again..
Any ideas how to put a default value in or somehow change the varchar to float,
Thanks
Im performing some calcuations within my select query.
Every now n then one of the values isnt entered correctly and is a varchar - this then returns an error from the stored proc and crashes my report.
heres the part of the query thats the problem:
SELECT
dbo.tbPurchaseItem.mPrice * dbo.tbPurchaseItem.iQty - dbo.tbPurchaseItem.mPrice * dbo.tbPurchaseItem.iQty * (convert(float,dbo.tbPurch
dbo.tbPurchaseItem.vCustom
There are now checks to enure this wont happen again, however for older orders this may happen again..
Any ideas how to put a default value in or somehow change the varchar to float,
Thanks
ASKER
Thanks Joel, I tried
case dbo.tbPurchaseItem.vCustom 1
when isnumeric(dbo.tbPurchaseIt em.vCustom 1) then 'num'
else 'not num'
end,
to test your response, however I still get
Syntax error converting the varchar value '7,5' to a column of data type int.
any ideas?
case dbo.tbPurchaseItem.vCustom
when isnumeric(dbo.tbPurchaseIt
else 'not num'
end,
to test your response, however I still get
Syntax error converting the varchar value '7,5' to a column of data type int.
any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks gohord
Using isnumeric it converst everything to 0 as the field is a text field
its just that when its not a number converting to float crashes
so I suppose I either need to change , to .
or change the value to 0 if its not a value that can be converted??
Using isnumeric it converst everything to 0 as the field is a text field
its just that when its not a number converting to float crashes
so I suppose I either need to change , to .
or change the value to 0 if its not a value that can be converted??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes thats what Ive done, there was just 1 record.
I wanted to know if/how I could do this in case it somehow happens again (as an extra precaution)
cheers
I wanted to know if/how I could do this in case it somehow happens again (as an extra precaution)
cheers
Hi. I think the first suggestion is a good solution...the issue is in the syntax for the case statement. You want something like this:
case isnumeric(vCustom1) when 1 then 'num' else 'not num' end
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Nice addition CoolBurn28... I"ll be incorporating your addition in my use of that function from now on.
Nice function. What happens when you have a value like 7.5.2 in your field?
I would stick with isnumeric in this case as you want a number after and not a string...but that is just me. The 'num' | 'not num' was just an example, imagine code like this:
I would stick with isnumeric in this case as you want a number after and not a string...but that is just me. The 'num' | 'not num' was just an example, imagine code like this:
convert(float,
case isnumeric(vCustom1)
when 1 then vCustom1
else 0
end
) as fltCustom1
ASKER
Thanks for your help.
The comments were useful
The comments were useful
You could either run an update query and set the ones that aren't numeric to zero or you could do a SELECT CASE statement right in your current query to get around data that isn't a number.