STRING stores a string of Unicode characters.
STRING is not a data type supported by PostgreSQL. For PostgreSQL compatibility, CockroachDB supports additional aliases and STRING-related types.Aliases
CockroachDB supports the following alias forSTRING:
TEXT
Related types
For PostgreSQL compatibility, CockroachDB supports the followingSTRING-related types and their aliases:
VARCHAR(and aliasCHARACTER VARYING)CHAR(and aliasCHARACTER)NAME
STRING.
CockroachDB also supports the single-byte "char" special character type. As in PostgreSQL, this special type is intended for internal use in , and has a storage size of 1 byte. CockroachDB truncates all values of type "char" to a single character.
Length
To limit the length of a string column, useSTRING(n), where n is the maximum number of Unicode code points (normally thought of as “characters”) allowed. This applies to all related types as well (e.g., to limit the length of a VARCHAR type, use VARCHAR(n)). To reduce performance issues caused by storing very large string values in indexes, Cockroach Labs recommends setting length limits on string-typed columns.
We strongly recommend adding size limits to all , which includes columns in .
Values exceeding 1 MiB can lead to and cause significant performance degradation or even .
To add a size limit using :
STRING value or a STRING-related-type value:
-
If the value is cast with a length limit (e.g.,
CAST('hello world' AS STRING(5))), CockroachDB truncates to the limit. This applies toSTRING(n)and all related types. -
If the value exceeds the column’s length limit, CockroachDB returns an error. This applies to
STRING(n)and all related types. -
For
STRING(n)andVARCHAR(n)/CHARACTER VARYING(n)types, if the value is under the column’s length limit, CockroachDB does not add space padding to the end of the value. -
For
CHAR(n)/CHARACTER(n)types, if the value is under the column’s length limit, CockroachDB adds space padding from the end of the value to the length limit. Type | Length --------------------------------------------------|------------------------------CHARACTER,CHARACTER(n),CHAR,CHAR(n)| Fixed-lengthCHARACTER VARYING(n),VARCHAR(n),STRING(n)| Variable-length, with a limitTEXT,VARCHAR,CHARACTER VARYING,STRING| Variable-length, with no limit"char"(special type) | 1 byte
Syntax
A value of typeSTRING can be expressed using a variety of formats.
See for more details.
When printing out a STRING value in the , the shell uses the simple
SQL string literal format if the value doesn’t contain special character,
or the escaped format otherwise.
Collations
STRING values accept , which lets you sort strings according to language- and country-specific rules.
Size
The size of aSTRING value is variable, but it’s recommended to keep values under 64 kilobytes to ensure performance. Above that threshold, and other considerations may cause significant performance degradation.
Examples
Supported casting and conversion
STRING values can be to any of the following data types:
| Type | Details |
|---|---|
ARRAY | Requires supported string format, e.g., '{1,2,3}'.Note that string literals can be implicitly cast to any supported ARRAY data type except , , , , and the Box2D, GEOGRAPHY, and GEOMETRY. |
BIT | Requires supported string format, e.g., '101001' or 'xAB'. |
BOOL | Requires supported string format, e.g., 'true'. |
BYTES | For more details, . |
CITEXT | Preserves the original letter case, but value comparisons are treated case-insensitively. Refer to . |
DATE | Requires supported string format, e.g., '2016-01-25'. |
DECIMAL | Requires supported string format, e.g., '1.1'. |
FLOAT | Requires supported string format, e.g., '1.1'. |
INET | Requires supported string format, e.g, '192.168.0.1'. |
INT | Requires supported string format, e.g., '10'. |
INTERVAL | Requires supported string format, e.g., '1h2m3s4ms5us6ns'. |
JSONPATH | Requires a valid expression string, e.g., '$' or '$.players[*] ? (@.stats.ppg > 30)'. |
TIME | Requires supported string format, e.g., '01:22:12' (microsecond precision). |
TIMESTAMP | Requires supported string format, e.g., '2016-01-25 10:10:10.555555'. |
TSQUERY | Requires supported string format, e.g., 'Requires & supported & TSQUERY & string & format'.Note that casting a string to a TSQUERY will not normalize the tokens into lexemes. To do so, use to_tsquery(), plainto_tsquery(), or phraseto_tsquery(). |
TSVECTOR | Requires supported string format, e.g., 'Requires supported TSVECTOR string format.'.Note that casting a string to a TSVECTOR will not normalize the tokens into lexemes. To do so, use to_tsvector(). |
UUID | Requires supported string format, e.g., '63616665-6630-3064-6465-616462656562'. |
STRING vs. BYTES
While both STRING and BYTES can appear to have similar behavior in many situations, one should understand their nuance before casting one into the other.
STRING treats all of its data as characters, or more specifically, Unicode code points. BYTES treats all of its data as a byte string. This difference in implementation can lead to dramatically different behavior. For example, let’s take a complex Unicode character such as ☃ (the snowman emoji):
Translate literals to STRING vs. BYTES
A literal entered through a SQL client will be translated into a different value based on the type:
BYTESgives a special meaning to the pair\xat the beginning, and translates the rest by substituting pairs of hexadecimal digits to a single byte. For example,\xffis equivalent to a single byte with the value of 255. For more information, see .STRINGdoes not give a special meaning to\x, so all characters are treated as distinct Unicode code points. For example,\xffis treated as aSTRINGwith length 4 (\,x,f, andf).
Cast hexadecimal digits to BIT
You can cast a STRING value of hexadecimal digits prefixed by x or X to a BIT value.
For example:
Concatenate STRING values with values of other types
STRING values can be concatenated with any non-ARRAY, non-NULL type, resulting in a STRING value.
For example:
STRING value with a results in a NULL value.
For example:
Convert STRING to TIMESTAMP
You can use the to parse strings in TIMESTAMP format.
Convert STRING to TSVECTOR
You can use the to parse strings in format. This will normalize the tokens into lexemes, and will add an integer position to each lexeme.
Convert STRING to TSQUERY
You can use the to parse strings in format. This will normalize the tokens into lexemes.
When using to_tsquery(), the string input must be formatted as a , with operators separating tokens.

