Literals: Alternative to single quote to start/end a string

As all know I can use single quotes to start/end a string in SQL and PL/SQL.

SELECT 'my string' AS a
  FROM dual;

A
---------
my string

When we have single quotes in a string we need an escape character. In Oracle this is another single quote. That looks like this.

SELECT 'my string with two single quotes for one '' ' AS a
  FROM dual;

A
-------------------------------------------
my string with two single quotes for one '

Complex strings can be difficult to handle this way. But Oracle provides another option.

SELECT q'[my string]' AS a
  FROM dual;

A
---------
my string

This way you can have as many single quotes in the string as you wish.
There are other options besides [] – {}, ().

SELECT q'{my 1' 2'' 3''' 4'''' string}' AS a
  FROM dual;

A
---------------------------
my 1' 2'' 3''' 4'''' string

I hope this helps.

Oracle Documentation 12c – Literals

Advertisements

My first blog – the why

Hi there,

I have decided to start my own blog. There are serveral reasons:

  • I want a place where I save my findings.
    In the years I have found out that I tend to forget all the little neat things found in the process of creating Oracle PL/SQL and SQL code for my customers.
  • I  want to minimize the time searching in Oracle not finding the  answer I am looking for.
  • Hope to help others with similar problems

So here it is. My external brain, my sticky notes that would be lost and forgotten.

I hope it will help you and you help me to create a place with insides to Oracle PL/SQL and SQL programming.

Cheers,
Jan