QuestionQ281

VerifiedRestricting and Sorting Data

The EMPLOYEES table has these columns:

  • EMPLOYEE_ID NUMBER(4)
  • LAST_NAME VARCHAR2(25)
  • JOB_ID VARCHAR2(10)

You need to search the JOB_ID column for strings that contain SA_. Which SQL statement should you use?

Explanation

In Oracle SQL, LIKE treats '' as a wildcard matching any single character, so to match a literal underscore in JOB_ID you must precede it with an escape character and declare that character with the ESCAPE clause, e.g. LIKE '%SA_' ESCAPE ''. Oracle string and character literals are always delimited with single quotes, not double quotes, so ESCAPE "" is invalid syntax, and a plain LIKE '%SA' would let the underscore match any character rather than a literal underscore, while using '=' would require an exact match instead of a pattern search.

Community Discussion

No comments yet. Be the first to start the discussion!