Solved

Convert VarChar to Numeric in WHERE Clause

Posted on 2013-06-10
9
1,281 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
 
LVL 4

Expert Comment

by:BAKADY
ID: 39236325
what about if the field AgencyDefinField is '4765765.00' ????
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

19 Experts available now in Live!

Get 1:1 Help Now