SQL SERVER : Select Query - Not So Simple Query

Discussion in 'Databases' started by neil12345, Nov 24, 2010.

  1. #1
    I have a "Career" table in SQL SERVER which has foll. data:

    P_Id FirstName
    1 maid
    2 maids singapore
    3 Fashion Designer
    4 Kari
    5 Designers
    6 Fashion

    Now we want to Search the FirstName with foll. scenario / examples

    Example 1 : Search word : "maids"
    Should display records 1,2

    Example 2 : Search word : "singapore"
    Should display record number : 2

    Example 3 : Search word : "Designer"
    Should display record number : 3, 5

    Example 4 : Search word : "Fashion"
    Should display record number : 3, 6

    Example 5 : Search word : "housemaids"
    Should display records 1,2
     
    neil12345, Nov 24, 2010 IP
  2. MattCA

    MattCA Peon

    Messages:
    14
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Examples 1 and 5 can't be done without altering search word, others can be achieved by
     
    MattCA, Nov 25, 2010 IP
    Mohie likes this.
  3. Mohie

    Mohie Peon

    Messages:
    122
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    as MattCA said!

    +1 MAttCA
     
    Mohie, Nov 25, 2010 IP
  4. neil12345

    neil12345 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    IS there any other possibility by breaking the words into characters or some intelligent way ? I see many of the site does that ?
     
    neil12345, Nov 25, 2010 IP
  5. Mohie

    Mohie Peon

    Messages:
    122
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    words in the search area ? or words saved in the db ?
     
    Mohie, Nov 25, 2010 IP
  6. neil12345

    neil12345 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Words in the search criteria
     
    neil12345, Nov 25, 2010 IP
  7. MattCA

    MattCA Peon

    Messages:
    14
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can always trim last 1-2 characters from a word to get more results, e.g.:
    search = 'games' -1 = 'game'
    search 'played' -1 = 'playe' (matches player, players etc)
    search 'played' -2 = 'play'

    You could do it in PHP with substr() function.
     
    MattCA, Nov 26, 2010 IP
  8. neil12345

    neil12345 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I thought its possible using SQL SERVER - T SQL Queries
     
    neil12345, Nov 26, 2010 IP
  9. Mohie

    Mohie Peon

    Messages:
    122
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    but that does not mean it will always return desired solutions , even it may return trash solutions,
    for example:
    a user search for joe,
    if we trim last 2 chars from user search field ,
    that mean we will be searching in the database for like 'j%' ,
    results will return for example:
    joe
    jonathan
    jihad
    jenny
    jessy

    lots of trash results
     
    Mohie, Nov 27, 2010 IP
  10. firman01

    firman01 Well-Known Member

    Messages:
    155
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    165
    #10
    look up: mysql fulltext search, sphinx, solr/lucene or use mysql regex to put word boundary.

    
    SELECT P_Id, FirstName FROM employees WHERE FirstName LIKE REGEXP '[[:<:]]Don[[:>:]]';
    
    Code (markup):
    Don Carlito <-- match
    Donovan Joe <-- not match
    Dono Kasi Indro <-- not match

    cmiiw
     
    Last edited: Jan 11, 2011
    firman01, Jan 11, 2011 IP
  11. mickster

    mickster Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    If you want to manipulate the string with SQL, you can use a function like this:
     
    mickster, Jan 13, 2011 IP