Appearance
C-style Escape Sequences in String Constants
A string constant that has an E
or e
directly before the opening single quote may contain escape sequences. For example, you can type:
e'any string'
E'any string'
Within a string that starts with E
or e
, type a backslash character (\
) to begin a C-style escape sequence. What follows can be one of a limited set of alphanumeric characters, an octal hex, or a unicode sequence.
The following example uses several backslash-character sequences. The result is a string constant that contains multiple tab characters and new lines:
premdb=# select E'\t'||E'\t'||E'\t'||'We are such stuff'||E'\n'||'As dreams are made on; and our little life'||E'\n'||'Is rounded with a sleep'
as verse from sys.const;
verse
--------------------------------------------
We are such stuff +
As dreams are made on; and our little life+
Is rounded with a sleep.
(1 row)
Note: The +
characters in this example are just artifacts of how ybsql
denotes newlines. They are not literal text inserted into the generated SQL string.
The specific escape sequences you can use are shown in the following table.
Backslash Escape Sequence | Interpretation |
---|---|
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Tab |
\o , \oo , \ooo | Octal byte value (o = 0 to 7) |
\xh , \xhh | Hexadecimal byte value (h = 0 to 9, A to F) |
\uxxx , \Uxxxxxxxx | 16-bit or 32-bit hexadecimal Unicode character value (x = 0 to 9, A to F) |
Note: This list of escape sequences overlaps with but is not the same as the escape sequences used with ybtools
. For example, \us
may be used as the field delimiter in ybload
, but it is an invalid character sequence in a SQL string.
Any other character following a backslash is taken literally. For example, you can embed a single quote in a string by using either double single quotes or a backslash. An embedded backslash must also be preceded with a backslash. To create the literal string \1’s
, you can use either of the following sequences:
premdb=# select E'\\1''s' as with_two_single_quotes, E'\\1\'s' as with_backslash_single_quote from sys.const;
with_two_single_quotes | with_backslash_single_quote
------------------------+-----------------------------
\1's | \1's
(1 row)
Parent topic:SQL String Constants