Solved

Convert VarChar to Numeric in WHERE Clause

Posted on 2013-06-10
9
1,672 Views
Last Modified: 2013-06-11
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
0
Comment
Question by:PhilChapmanJr
9 Comments
 
LVL 4

Accepted Solution

by:
BAKADY earned 500 total points
ID: 39236027
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
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39236095
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
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39236318
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 4

Expert Comment

by:BAKADY
ID: 39236325
what about if the field AgencyDefinField is '4765765.00' ????
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39236355
Would you pass that on that request via an integer? (wouldn't advise it)

SELECT     *
FROM         Accounts
WHERE     AgencyDefinField =  '4765765.00'
0
 
LVL 4

Expert Comment

by:BAKADY
ID: 39237018
Is the author no more interested in our answers????
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39237029
2013-06-11 at 06:31:30
2013-06-11 at 18:39:50
duration = 12:08:20
such enthusiasm :)
0
 
LVL 2

Author Closing Comment

by:PhilChapmanJr
ID: 39237446
I found the problem, someone had removed the number and left the the field flank.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39237462
that would do it :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
insert query with value having 's 2 57
Help with SQL - TOP 10 by date and by group 13 40
Please help for the below sql query. 1 27
SQL Server Configuration Manager WMI Error 11 16
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question