Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Strings

The string type represents any valid UTF-8 string.

For backward compatibility with SQL, syntactic aliases for type string are defined as follows:

  • CHARACTER VARYING
  • CHARACTER
  • TEXT
  • VARCHAR

A string is formed by enclosing the string’s unicode characters in quotation marks whereby the following escape sequences allowed:

SequenceUnicode Character
\"quotation mark U+0022
\'apostrophe U+0008
\\reverse solidus U+005C
\/solidus U+002F
\bbackspace U+0008
\fform feed U+000C
\nline feed U+000A
\rcarriage return U+000D
\ttab U+0009
\uXXXXU+XXXX

The backslash character (\) and the control characters (U+0000 through U+001F) must be escaped.

In SQL expressions, the quotation mark is a single quote character (') and in pipe expressions, the quotation mark may be either single quote or double quote (").

In single-quote strings, the single-quote character must be escaped and in double-quote strings, the double-quote character must be escaped.

Raw String

Raw strings or r-strings are expressed as the character r followed by a single- or double-quoted string, where any backslash characters are treated literally and not as an escape sequence. For example, r'foo\bar' is equivalent to 'foo\\bar'.

Unlike other string syntaxes, raw strings may have embedded newlines, e.g., a literal newline inside of the string rather than the character sequence \n as in

r"foo
bar"

Formatted Strings

Formatted strings or f-strings are expressed as the character f followed by a single- or double-quoted string and may contain embedded expressions denoted within curly braces { }.

Examples


Various strings

# spq
values 'hello, world', len('foo'), "SuperDB", "\"quoted\"", 'foo'+'bar'
# input

# expected output
"hello, world"
3
"SuperDB"
"\"quoted\""
"foobar"

String literal vs field identifier in a SQL SELECT statement

# spq
SELECT 'x' as s, "x" as x
# input
{x:1}
{x:2}
# expected output
{s:"x",x:1}
{s:"x",x:2}

Formatted strings

# spq
values f'{x} + {y} is {x+y}'
# input
{x:1,y:3}
{x:2,y:4}
# expected output
"1 + 3 is 4"
"2 + 4 is 6"

Raw strings

# spq
values r'foo\nbar\t'
# input

# expected output
"foo\\nbar\\t"

Raw strings with embedded newline

# spq
values r'foo
bar'
# input

# expected output
"foo\nbar"