p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   SQL Language (http://p2p.wrox.com/forumdisplay.php?f=100)
-   -   LIKE %<search text>% (http://p2p.wrox.com/showthread.php?t=14142)

U.N.C.L.E. June 13th, 2004 03:23 AM

LIKE %<search text>%
 
I am performing a search of records by phone number...

SELECT * FROM tblClients
WHERE Phone LIKE '%" & Request("Phone") & "%'

The phone field is somewhat jumbled because everyone has different ways of entering their phone number into the db.

e.g.
1-800-555-1212
(800)555-1212
8005551212
18005551212

So when I enter a number to search, for example 555-1212, the query would return 2 rows. I'd like it to return all 4 rows.

Is there a method of 'stripping' the non-numeric chars from the column data for the purpose of the query comparison?

I'm thinking there may be some type of function I can use to modify the data, analogous to a regular expression: [^0-9]

owain June 13th, 2004 12:38 PM

The easiest way of doing this would be to store the number in the database with no non-numeric characters in it. You could of course also store the user entered phone number as well. Then when someone wanted to search for the number they would enter a formatted number, you could strip out the non-numeric characters in your front end and then perform a LIKE search based on this version of the number.

Regards
Owain Williams

U.N.C.L.E. June 13th, 2004 12:55 PM

That was my first thought also, but there are several areas where a phone number is entered, whereas there is only one search page. So, in my eyes, the method with the least number of code modifications is the easiest.

Can someone post an example of a custom routine? As in:

SELECT * FROM tblClients
WHERE STRIP(Phone) LIKE '%" & Request("Phone") & "%'

STRIP() would be a function that strips non-numeric characters from the column.

owain June 13th, 2004 06:32 PM

If you are using SQL Server (I don't know about Oracle) then you could use the REPLACE() function. Here is the example I tried:
Code:

SELECT *
  FROM tblClients
 WHERE REPLACE(REPLACE(REPLACE([Phone], '-', ''), '(', ''), ')', '')
       LIKE '%5551212%'

       This will replace hyphens, open brackets and close brackets symbols with empty strings. You can remove more symbols by nesting more REPLACE() functions. This could also easily be added to a UDF if required.

If you need to remove all non numeric characters then you will need to loop through the string testing each character. This can be done in a WHILE loop using the SUBSTRING() function. I tried to create a UDF that would do this but I could not get it to work, unfortunately I do not have a great deal of experience with SQL Server so I am probably just making a silly mistake, the code I tried is below, maybe someone with more experience would be kind enough to take a look at it and fix the problems:
Code:

CREATE FUNCTION PhoneStrip(@expression varchar(20))
RETURNS varchar(20)
AS
BEGIN
    DECLARE @index int
    DECLARE @return varchar(20)

    @index = 1

    WHILE @index < LEN(@expression)
    BEGIN
        IF SUBSTRING(@expression, @index, 1) BETWEEN '0' AND '9'
            @return = @return + SUBSTRING(@expression, @index, 1)
        @index = @index + 1
    END
    RETURN @return
END

In the mean time the REPLACE() function should suffice. I hope this helps.

Regards
Owain Williams

U.N.C.L.E. June 13th, 2004 09:23 PM

Actually, it is very helpful and your efforts are appreciated! Too bad one can't simply use regular expressions in some way...

FYI: I am using SQL Server 2000.


owain June 14th, 2004 03:59 AM

Wait for SQL Server Yukon, then you will be able to use the full power of .Net in your Stored Procedures and User Defined Functions! I know this doesn't help you now, but it is very cool (although the purist's will probably disagree).

Regards
Owain Williams

Mickel February 28th, 2006 01:57 PM

Here's a corrected version of the PhoneStrip function:

--Function will strip away any non-numeric characters in a free text
--phone number field

CREATE FUNCTION PhoneStrip (@expression varchar(20))
RETURNS varchar(20)
AS
BEGIN
    DECLARE @index int
    DECLARE @return varchar(20)

   Set @index = 1
   Set @return=''

    WHILE @index < LEN(@expression)+1
    BEGIN
        IF SUBSTRING(@expression, @index, 1) BETWEEN '0' AND '9'
          Set @return = @return + SUBSTRING(@expression, @index, 1)
        Set @index = @index + 1
    END
    RETURN @return
END



jbenson001 March 4th, 2006 11:58 PM

You can use regular expressions in a LIKE clause. Look up LIKE in Books On Line. Then choose Pattern Matching in Search Conditions.

Jim


Jeff Mason March 5th, 2006 11:02 AM

Quote:

quote:Originally posted by jbenson001
 You can use regular expressions in a LIKE clause.

With all due respect, the pattern matching operator in the LIKE clause as defined in the SQL standard hardly qualifies as a true regular expression capability.

They are a very restricted subset of something that looks sort of like a regular expression...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com

jbenson001 March 5th, 2006 03:33 PM

it still uses something like regular expression syntax, never said it was a full blown regular expression alternative. It will certainly do that the poster wants.



All times are GMT -4. The time now is 08:07 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.