Literals

All database systems make provisions for embedding literal values in SQL statements. Text and numeric literals are usually quite simple, but there are some nuances of which you should be aware. Date and time literals tend to be more complex.

Text Literals

The ISO SQL standard for text literals is to enclose them in single quotes:

'This is a text literal'

Use two adjacent single quotes when you need to embed a single quote in a string:

'Isn''t SQL fun?'

SQL will treat the two adjacent single quotes as a single quote within the literal:

Isn't SQL fun?

Oracle Database 10g and higher allow you to specify alternative quoting delimiters, which are always two characters and always include leading and trailing single quotes. Introduce delimiters by prefacing them with a Q or a q. For example, to use '[ and ]' as delimiters, specify:

Q'[This isn't as bad as it looks]'
q'[This isn't as bad as it looks]'

The (, [, and { characters are special cases in that their corresponding closing delimiters must be ), ], and }, respectively. Otherwise, use the same character to close the string that you use to open it:

Q'|This string is delimited by vertical bars|'

You can’t use space, tab, or return characters to delimit a string in this manner.

PostgreSQL allows you to specify alternative quoting delimiters using a dollar-sign syntax, producing a dollar-quoted string constant. For example:

$tag$This is a dollar-quoted string constant$tag$

Replace tag with any desired sequence of characters. Your quoting delimiter ...

Get SQL Pocket Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.