Convert VarChar to Numeric in WHERE Clause

I have the following query that has been working for years

AgencyDefinField is a VarChar

SELECT     *
FROM         Accounts
WHERE     (CONVERT(numeric, AgencyDefinField) = 4765765)

Beginning Today I started getting the following message

Microsoft ODBC SQL server driver [SQL Server ] Error converting data type varchar to numeric

I have two Questions:
1.  What could have cause the problem to only now appear
2.  How to fix it

Any help would be appreciated.

Thanks,
Phil
LVL 2
Phil ChapmanAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
BAKADYConnect With a Mentor Commented:
maybe works this, or you have values like '4765765.00' then not.
SELECT     *
FROM         Accounts
WHERE     rtrim(ltrim(AgencyDefinField)) = '4765765'

Open in new window

the problem are the values in AgencyDefinField, maybe NULL or empty strings.

you can try ISNUMERIC-function too
SELECT     *
FROM         Accounts
WHERE     CASE WHEN ISNUMERIC(AgencyDefinField) THEN CAST(AgencyDefinField as NUMERIC) ELSE 0 END = 4765765

Open in new window

t
0
 
Patrick MatthewsCommented:
Almost always, this type of error is caused by having a [n][var]char value that is not convertible to a numeric.

BAKADY's second SQL statement above handles that scenario by forcing a return of zero.

You can utilize that approach to find out which record(s) are causing you difficulty:

SELECT     *
FROM         Accounts
WHERE     ISNUMERIC(AgencyDefinField) = 0

Open in new window

0
 
PortletPaulfreelancerCommented:
It's better from a performance perspective to filter the opposite way i.e. amend the criteria to suit that data. This way indexes can be used by the query.

e.g.

SELECT     *
FROM         Accounts
WHERE     AgencyDefinField = convert(varchar,4765765) -- or nvarchar

if the field is only supposed to carry strings of digits then there may be data you need to correct, however I'd still suggest filtering by conversion of criteria to suit the field's data type.
0
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.

 
BAKADYCommented:
what about if the field AgencyDefinField is '4765765.00' ????
0
 
PortletPaulfreelancerCommented:
Would you pass that on that request via an integer? (wouldn't advise it)

SELECT     *
FROM         Accounts
WHERE     AgencyDefinField =  '4765765.00'
0
 
BAKADYCommented:
Is the author no more interested in our answers????
0
 
PortletPaulfreelancerCommented:
2013-06-11 at 06:31:30
2013-06-11 at 18:39:50
duration = 12:08:20
such enthusiasm :)
0
 
Phil ChapmanAuthor Commented:
I found the problem, someone had removed the number and left the the field flank.
0
 
PortletPaulfreelancerCommented:
that would do it :)
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.

All Courses

From novice to tech pro — start learning today.