Introduction
There are five categories of constants in CockroachDB:- String literals: these define string values but their actual data type will
be inferred from context, for example,
'hello'. - Numeric literals: these define numeric values but their actual data
type will be inferred from context, for example,
-12.3. - Bit array literals: these define bit array values with data type
BIT, for example,B'1001001'. - Byte array literals: these define byte array values with data type
BYTES, for example,b'hello'. - Interpreted literals: these define arbitrary values with an explicit
type, for example,
INTERVAL '3 days'. - Named constants: these have predefined values with a predefined
type, for example,
TRUEorNULL.
String literals
CockroachDB supports the following formats for string literals:- Standard SQL string literals.
- String literals with C escape sequences.
- Dollar-quoted string literals
| Expression | Data type of the string literal |
|---|---|
length('hello') | STRING |
now() + '3 day' | INTERVAL |
INSERT INTO tb(date_col) VALUES ('2013-01-02') | DATE |
STRING otherwise.
Check our blog for
more information about the typing of string literals.
Standard SQL string literals
SQL string literals are formed by an arbitrary sequence of characters enclosed between single quotes ('), for example, 'hello world'.
To include a single quote in the string, use a double single quote.
For example:
'hello' ' world!'
doesn’t work. This is mandated by the SQL standard.
String literals with character escapes
CockroachDB also supports string literals containing escape sequences like in the programming language C. These are constructed by prefixing the string literal with the lettere, for example,
e'hello\nworld!'.
The following escape sequences are supported:
| Escape Sequence | Interpretation |
|---|---|
\a | ASCII code 7 (BEL) |
\b | backspace (ASCII 8) |
\t | tab (ASCII 9) |
\n | newline (ASCII 10) |
\v | vertical tab (ASCII 11) |
\f | form feed (ASCII 12) |
\r | carriage return (ASCII 13) |
\xHH | hexadecimal byte value |
\ooo | octal byte value |
\uXXXX | 16-bit hexadecimal Unicode character value |
\UXXXXXXXX | 32-bit hexadecimal Unicode character value |
e'x61\141\u0061' escape string represents the
hexadecimal byte, octal byte, and 16-bit hexadecimal Unicode character
values equivalent to the 'aaa' string literal.
Dollar-quoted string literals
To make it easier to write certain types of string constants in SQL code, CockroachDB supports dollar-quoted string literals. This is particularly useful for strings that need to contain lots of single quotes (') or backslashes (\).
At a high level, the dollar-quoting behavior works similarly to “heredocs” as used in UNIX shells and some programming languages.
Dollar-quoted strings have the form: $ + (optional) tag + $ + arbitrary text + $ + (optional) tag + $
For example:
Numeric literals
Numeric literals can have the following forms:| Syntax | Possible data types |
|---|---|
| Contains a decimal separator | FLOAT, DECIMAL |
| Contains an exponent | FLOAT, DECIMAL |
| Contains a value outside of the range -2^63…(2^63)-1 | FLOAT, DECIMAL |
| Otherwise | INT, DECIMAL, FLOAT |
Bit array literals
Bit array literals consist of theB prefix followed by a string of
binary digits (bits) enclosed in single quotes.
For example: B'1001010101'
Bit array literals are acceptable both when values of types
or (BIT VARYING) are
expected.
The number of bits is arbitrary. An empty bit array is denoted B'';
the number of bits need not be a multiple of 8, and bit arrays can
contain more than 64 bits.
Byte array literals
CockroachDB supports two formats for byte array literals:Byte array literals with character escapes
This uses the same syntax as string literals containing character escapes, using ab prefix instead of e. Any character escapes are interpreted like they
would be for string literals.
For example: b'hello,\x32world'
The two differences between byte array literals and string literals
with character escapes are as follows:
- Byte array literals always have data type
BYTES, whereas the data type of a string literal depends on context. - Byte array literals may contain invalid UTF-8 byte sequences, whereas string literals must always contain valid UTF-8 sequences.
Hexadecimal-encoded byte array literals
This is a CockroachDB-specific extension to express byte array literals: the delimiterx' followed by an arbitrary sequence of
hexadecimal digits, followed by a closing '.
For example, all the following formats are equivalent to b'cat':
x'636174'X'636174'
Interpreted literals
A constant of any data type can be formed using either of the following formats:'string'::type and CAST('string' AS type) is also recognized as an
interpreted literal. These are special cases of
.
For more information about the allowable format of interpreted
literals, refer to the “Syntax” section of the respective data types:
, , , ,
.
Named constants
CockroachDB recognizes the following SQL named constants:TRUEandFALSE, the two possible values of data typeBOOL.NULL, the special SQL symbol that indicates “no value present”.
NULL is a valid constant for any type: its actual data
type during expression evaluation is determined based on context.

