Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert VarChar to Numeric in WHERE Clause

Posted on 2013-06-10
9
Medium Priority
?
2,750 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 4

Accepted Solution

by:
BAKADY earned 2000 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 93

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 49

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

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

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 49

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 49

Expert Comment

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

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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