Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 13th, 2004, 03:23 AM
Friend of Wrox
Points: 422, Level: 7
Points: 422, Level: 7 Points: 422, Level: 7 Points: 422, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2003
Location: La Jolla, CA, USA.
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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]
Reply With Quote
  #2 (permalink)  
Old June 13th, 2004, 12:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #3 (permalink)  
Old June 13th, 2004, 12:55 PM
Friend of Wrox
Points: 422, Level: 7
Points: 422, Level: 7 Points: 422, Level: 7 Points: 422, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2003
Location: La Jolla, CA, USA.
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #4 (permalink)  
Old June 13th, 2004, 06:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old June 13th, 2004, 09:23 PM
Friend of Wrox
Points: 422, Level: 7
Points: 422, Level: 7 Points: 422, Level: 7 Points: 422, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2003
Location: La Jolla, CA, USA.
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Reply With Quote
  #6 (permalink)  
Old June 14th, 2004, 03:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #7 (permalink)  
Old February 28th, 2006, 01:57 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Reply With Quote
  #8 (permalink)  
Old March 4th, 2006, 11:58 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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

Jim

Reply With Quote
  #9 (permalink)  
Old March 5th, 2006, 11:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #10 (permalink)  
Old March 5th, 2006, 03:33 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 8: <asp:image> inside <a> & ext.CSS (pg. 274) epc BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 July 12th, 2008 04:37 AM
How do I wrap a <p> around text that contains <i>? dhsmd XSLT 6 March 5th, 2008 04:52 AM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
<marquee><b>About CHAT App. in PHP4</b></marquee> Ramkrishna PHP How-To 1 September 11th, 2004 07:01 AM
<STRONG> vs <B> and <EM> vs <I> anshul HTML Code Clinic 12 September 1st, 2004 05:22 PM



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


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