Solved

Varchar to Numeric Search

Posted on 2006-07-07
10
567 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
  • 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
 
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

863 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

18 Experts available now in Live!

Get 1:1 Help Now