How to Escape Characters in Oracle PL/SQL Queries

Oracle databases reserve some special characters with specific meaning and purpose within Oracle environment. These reserved characters include _ (underscore) wild card character which used to match exactly one character, % (percentage) which used to match zero or more occurrences of any characters and ‘ (apostrophe or quotation mark) which used to mark the value supplied. These special characters will not be interpreted literally when building SQL query in Oracle, and may caused error in results returned especially when performing string search with LIKE keyword. To use these characters so that Oracle can interpret them literally as a part of string value instead of preset mean, escape character has to be assigned.

Oracle allows the assignment of special escape characters to the reserved characters in Oracle can be escaped to normal characters that is interpreted literally, by using ESCAPE keyword.

For example, to select the name of guests with _ (underscore) in it, use the following statement:

SELECT guest_name FROM guest_table WHERE name LIKE ‘%\_%’ ESCAPE ‘\’;

Without specifying the \ (backslash) as escape clause, the query will return all guest names, making the unwanted results problem.

The above syntax will not work on ‘ (quote). To escape this quotation mark and to display the quote literally in string, insert another quote (total 2 quotes) for every quote that want to be displayed. For example:

SELECT ‘This will display line with quote’’s word.’ FROM temp_table;
SELECT ‘This will display ””double quoted”” word.’ FROM temp_table;

will return the following respectively:

This will display line with quote’s word.
This will display ”double quoted” word.

Share and contribute or get technical support and help at My Digital Life Forums.



3 Responses to “How to Escape Characters in Oracle PL/SQL Queries”

  1. Escape Character in MySql - Discussweb IT Community - Web Development, Software Programming, SEO, Quality Assurance, 3D, Web Hosting and more...
    July 13th, 2007 19:27
    1

    [...] about Escape Characters. Escape Characters Oracle Wildcards Wildcard Characters Escape Character How to Escape Characters in Oracle PL/SQL Queries

  2. sam33
    September 17th, 2008 20:44
    2

    salut j’ai le mm pb ! si je fait une recherche sur le ‘_’ seul sa marche mais si v je chercher des mots avec le ‘_’
    exmple:
    SELECT * from Test WHERE nom LIKE ‘1%\_%test’ ESCAPE ‘\’;
    sa me renvoie :
    1_test
    10_test ??
    or que normalement sa devait rendre que:
    1_test ??
    je comprend pas

  3. Charles Asbornsen
    September 26th, 2008 00:28
    3

    Thank you very much - I’m a SQL Server gent with an Oracle source and I was going nuts! :)

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.
Custom Search

New Articles

Incoming Search Terms for the Article

oracle escape character - oracle ESCAPE - PL SQL escape character - oracle sql escape character - pl/sql escape character - escape character in sql - oracle escape string - escape & in oracle - HOW TO ESCAPE & IN ORACLE SQL - sql escape character - oracle escape ' - oracle escape apostrophe - plSQL escape character - sql escape - escape character in Oracle sql - oracle sql escape & - pl sql escape - oracle escape & character - escape sql oracle - escape sql - oracle escape & - oracle sql & character - oracle string escape characters - oracle escape characters - oracle sql escape char - plsql escape - Plsql escape & - sql escape character oracle - escaping "'" in oracle - how to escape SQL - oracle escape char - oracle sql escape characters - oracle string escape character - oracle sql ESCAPE - pl/sql escape quote - plsql string special characters - special characters in oracle - ESCAPE CHARACTERS IN ORACLE - escape character oracle - oracle sql special characters - wild character search in oracle - escape oracle - escape characters sql - oracle sql ESCAPE SEQUENCE - oracle like escape - oracle escape keyword - escape '&' in Oracle - oracle & escape character - oracle sql & escape - oracle like escape underscore -