?
Solved

Varchar to Numeric Search

Posted on 2006-07-07
10
Medium Priority
?
574 Views
Last Modified: 2012-05-05
I have a table that contains a InvoiceField varchar field
some of these record field has a value of 0000123456789  and some have a value of 00123456789
I need to be able to returen all records where varchar field is 0000123456789 , 00123456789 or 123456789

EXAMPLE:
SELECT * FROM MyTable
WHERE
DO YOUR STUFF  = 123456789

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
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 12

Assisted Solution

by:Einstine98
Einstine98 earned 400 total points
ID: 17061854
SELECT * FROM TABLE
WHERE convert (decimal, Myfield) = 123456789
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 17061923
When I try the code above I get a Error converting data type varchar to numeric  
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 17061991
I check the table and it had a record where the InvoiceField was blank which caused the error.  Can the above query be midified not to return a error if the field should have non numeric data.  But instead just to ignore the record.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17061996
SELECT * FROM MyTable
WHERE RIGHT(YourColumn, 9) = '123456789'
0
 
LVL 25

Expert Comment

by:jrb1
ID: 17062000
Are the values always numeric?  If so, maybe:

SELECT * FROM MyTable
WHERE cast(yourfield as float)  = 123456789

or to trap for conversion errors  (if this works....it's OK, but not the best of SQL Server's functions)

SELECT * FROM MyTable
WHERE isnumeric(yourfield)
and cast(yourfield as float)  = 123456789
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 800 total points
ID: 17062045
You can also take advantage of the fact that T-SQL uses short-circuit logic and use something like this:
SELECT * FROM MyTable
Where ISNUMERIC(Col1) = 1
           And CAST(Col1 as bigint) = 123456789
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 17062258
>> SELECT * FROM MyTable
WHERE RIGHT(YourColumn, 9) = '123456789' <<

Technically | theoretically this might return incorrect results because of values such as '3123456789', '51123456789', etc..

0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 17062343
SELECT * FROM MyTable
Where ISNUMERIC(Col1) = 1
           And CAST(Col1 as bigint) = 123456789

or

SELECT * FROM MyTable
Where ISNUMERIC(Myfield) = 1
AND convert (decimal, Myfield) = 123456789


Seems to work fine.  This table has several million records the  InvoiceField  is a IX_Index Will the  ISNUMERIC and CAST or CONVERT have verry much effect on the speed of the search.
0
 
LVL 25

Assisted Solution

by:jrb1
jrb1 earned 400 total points
ID: 17062372
Yes it will.  You could create a computed column:

ALTER TABLE YourTable
ADD YourComputedColumn AS CAST(Col1 as bigint)

Then index it:

CREATE INDEX ComputedColumnIndex
ON YourTable(YourComputedColumn)
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 400 total points
ID: 17062522
Yes, a *huge* impact -- SQL will simply *not* use an index for the lookup.

Honestly, you'd be better off using a method similar to the one below, as dopey as it looks, assuming there aren't leading spaces in the InvoiceField that would throw off a comparison, since SQL should then be able to use the index:

SELECT ...
FROM MyTable
INNER JOIN (
    SELECT '0000000000123456789' AS inv
    UNION ALL
    SELECT '000000000123456789'
    UNION ALL
    SELECT '00000000123456789'
    UNION ALL
    SELECT '0000000123456789'
    UNION ALL
    SELECT '000000123456789'
    UNION ALL
    SELECT '00000123456789'
    UNION ALL
    SELECT '0000123456789'
    UNION ALL
    SELECT '000123456789'
    UNION ALL
    SELECT '00123456789'
    UNION ALL
    SELECT '0123456789'
    UNION ALL
    SELECT '123456789'
) AS searchFor ON searchFor.inv = MyTable.invoiceField
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

800 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