Results 1 to 7 of 7

Thread: LIKE keyword

  1. #1
    Join Date
    Aug 2008
    Posts
    464

    Unanswered: LIKE keyword

    Hi guys

    Going to ask quite a basic question I suppose.

    How do I use the 'LIKE' keyword to select multiple entities?

    Obviously the full name of the entity is not known.

    I tried:

    Code:
    SELECT * FROM TABLE WHERE DATETIME>TRUNC(SYSDATE)-1 AND CELL LIKE 'A%' AND CELL LIKE 'B%'
    but this didn't workl

    Regards
    Shajju
    Last edited by shajju; 03-19-13 at 09:53.

  2. #2
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    SELECT * FROM TABLE WHERE CELL LIKE 'A%' or CELL LIKE 'B%'
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

  3. #3
    Join Date
    Aug 2008
    Posts
    464
    Thanks but if you look at the query I posted closely, there's also

    Code:
    WHERE DATETIME > TRUNC(SYSDATE)-1

  4. #4
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    It's not specific to LIKE predicate to use parentheses to clarify the precedence in complex AND/OR combinations,
    like...

    Code:
     WHERE datetime > TRUNC(SYSDATE) - 1
       AND
      (    cell LIKE 'A%'
       OR  cell LIKE 'B%'
      )

  5. #5
    Join Date
    Jul 2006
    Posts
    49
    sajju,
    Your original query did not work because you used the AND operator. You told Oracle to only list rows that started with the letters A and B at the same time. A row can only start with one letter, right, so there's no way a row can start with two different letters at the same time. The solution is to change the operator to OR. Changing the operator to OR solves that problem but it creates a new problem: if you mix AND and OR operators then you must consider the rules of precedence. For these reasons tonkuma's solution is correct.

  6. #6
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    Another way might be...
    Code:
     WHERE datetime > TRUNC(SYSDATE) - 1
       AND RPAD(cell , 1) IN ('A' , 'B')

  7. #7
    Join Date
    Aug 2008
    Posts
    464

    Thanks

    Appreciated guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •