Solved

Convert VarChar to Numeric in WHERE Clause

Posted on 2013-06-10
9
1,530 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now