Converting Varchar to Numberic or int

When I run this query I receive the following error:
Error converting data type varchar to numeric.

The Query:
select [HefferTypeCode], [HefferCode], CAST([HefferCode] AS NUMERIC)
FROM dbo.tblHeffers
WHERE [HefferTypeCode] IN ( 'A' , 'BC' ) AND CAST(HefferCode AS NUMERIC) < 1000

The HefferCode runs from 001 to 2019 and if I remove the test after the AND it runs.
wdbatesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JestersGrindCommented:
Do you get any records when you run this?

SELECT *
FROM dbo.tblHeffers
WHERE ISNUMERIC(HefferCode) = 0

If you do, that is why it's failing.

Greg

0
wdbatesAuthor Commented:
Yes, but when I place in the test WHERE [HefferTypeCode] IN ( 'A' , 'BC' ) I return no records.  I did more checking and I do have a record with a HefferCode of 0 in the A and BC group.  I also found a record not in the A and BC group that is alpha.  So how do I fix my task?
0
JestersGrindCommented:
I don't think the 0 in the HefferCode column is causing an issue.  Although, if that is incorrect, you should update it to a correct value.  The other record is probably the cause.  Even though it is not in group A or BC, SQL is still trying to evaluate it and can't.  To fix it, you need to update that record to a numeric value.

Greg

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wdbatesAuthor Commented:
Thank you for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.