Solved

Varchar to Numeric Search

Posted on 2006-07-07
10
573 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 100 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
Industry Leaders: 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 200 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 100 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 100 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

689 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