return row where 'x' CONTAINS 'y'

Discussion in 'MySQL' started by PEACE_ESKIMO, Mar 3, 2009.

  1. #1
    i have my data for zip codes stored as 99999-9999.

    there is a search by zip code on my site.

    i figure that most users will be using 99999 format.

    how can i return rows where zip contains 99999?
     
    PEACE_ESKIMO, Mar 3, 2009 IP
  2. nyxano

    nyxano Peon

    Messages:
    417
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What type of database are you using? Access, Oracle, SQL Server, MySQL, etc. The query would vary slightly depending on the database.

    For MySQL, the query would be SELECT * FROM table WHERE ZipCode LIKE '%99999%'

    The percent sign acts as sort of a wildcard so the above would search any where in the ZipCode field for 99999. If you wanted to only search the beginning of the field, then it would be LIKE '99999%'
     
    nyxano, Mar 3, 2009 IP
  3. qazu

    qazu Well-Known Member

    Messages:
    1,834
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Sounds like you store zip codes in two formats: a 5 digit format and a 4 digit format and you want to return rows where the 5 digit format is used.

    Zip codes would be in integer so SELECT * FROM table WHERE ZipCode > 9999

    Note: 9999 is a number and not a placeholder as in your example so the result would have rows where zip code is 10000 and higher
     
    qazu, Mar 4, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    I would do it a little differently. Use:

    SELECT * FROM table WHERE LEFT(ZipCode,5) = '99999'

    You can place an index on the first 5 characters of the ZipCode field and you wont see any performance hit you would find with a Where '%%' query.

    CREATE INDEX five_zip ON table (ZipCode (5));
     
    jestep, Mar 4, 2009 IP
  5. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i think this sort of search would work for you.. although it might not be the fastest

    i believe i tried this before in sql server
    SELECT *
    FROM table
    WHERE zip like '%searchstring%'

    and the problem is that will only return rows where searchstring has values to the left and the right of it i.e. if you're searching 9999 it will return 89999-9999 but not 99998-8888 because there is nothing to the left of the leftmost 9

    this version will check to see if the search string is anywhere within the zip field

    SELECT *
    FROM Table
    WHERE zip LIKE 'searchstring'
    OR zip LIKE '%searchstring'
    OR zip LIKE 'searchstring%'
    OR zip LIKE '%searchstring%'
     
    Jamie18, Mar 5, 2009 IP
  6. peterCx

    peterCx Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    substr(zipcode,1,5)
     
    peterCx, Mar 10, 2009 IP