sql searching

Discussion in 'MySQL' started by Jamie18, Nov 9, 2007.

  1. #1
    i need to search through a database for whole words...

    this search gives me almost exactly what i need..
    SELECT *
    FROM contacts
    WHERE last_name LIKE '#search_phrase#'
      OR last_name LIKE '%[^a-zA-z]#search_phrase#'
      OR last_name LIKE '#search_phrase#[^a-zA-z]%'
      OR last_name LIKE '%[^a-zA-z]#search_phrase#[^a-zA-z]%'
    Code (markup):
    So as you can see i'm looking for whole words within a field that contains multiple words......
    the field could contain the exact search phrase, end with it, start with it or it could be somewhere in the middle..

    just by looking at this query i assume there must be a better way..
    all i'm really looking for is that the search phrase is surrounded by whitespace or start or end characters

    any ideas?
     
    Jamie18, Nov 9, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    For regular expression, instead of 'LIKE', use 'RLIKE' or 'REGEXP'. The regex for 'word boundary' is '\b'. So it should be:
    SELECT * FROM contacts WHERE last_name RLIKE '%\b#search_phrase#\b%';
     
    phper, Nov 11, 2007 IP
  3. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for the suggestion but unfortunately it didn't help since i'm using mssql server... which apparently doesn't support regular expressions at all
     
    Jamie18, Nov 13, 2007 IP
  4. ketan9

    ketan9 Active Member

    Messages:
    548
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #4
    ketan9, Nov 14, 2007 IP
  5. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Ketan9 - the OP has mentioned that he is using MSSQL Server, and so
    mysql text search documentation may not be of much help to him.
     
    Kuldeep1952, Nov 14, 2007 IP
  6. ketan9

    ketan9 Active Member

    Messages:
    548
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #6
    i misread mssql to be mysql.. sorry about that!!!
     
    ketan9, Nov 15, 2007 IP