Solved

Varchar to Numeric Search

Posted on 2006-07-07
10
564 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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:ScottPletcher
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:ScottPletcher
ScottPletcher 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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

16 Experts available now in Live!

Get 1:1 Help Now