SQL expressions and types
Look up SQL operators, time zones, data types and casts.
Use this reference when you write a SQL transform.
Expressions and operators
| Operator or syntax | Meaning |
|---|---|
+, -, *, /, % | Arithmetic: see Arithmetic. A timestamp plus or minus an INTERVAL is a timestamp. |
-x, +x | Negation, and the value itself |
=, <>, !=, <, <=, >, >= | Comparison: see Comparison |
AND, OR, NOT | Logic |
IS NULL, IS NOT NULL, IS TRUE, IS NOT TRUE, IS FALSE, IS NOT FALSE, IS UNKNOWN, IS NOT UNKNOWN | Tests that are never null |
IS DISTINCT FROM, IS NOT DISTINCT FROM | Comparison that treats two nulls as equal |
IN (…), NOT IN (…) | Membership in a list of values. A list of values only: a subquery is refused. |
x = ANY (list), x = SOME (list), x <> ALL (list) | Whether a list has x, and whether it does not: see Comparison. A list only: a subquery is refused. |
BETWEEN … AND …, NOT BETWEEN … AND … | A range, both ends included |
LIKE, NOT LIKE, ILIKE, NOT ILIKE, SIMILAR TO, NOT SIMILAR TO | Pattern matching: see Patterns. LIKE and ILIKE take ESCAPE. |
~, ~*, !~, !~* | Regular expression matching. * ignores case, and ! negates. See Patterns. |
|| | String concatenation. If one side is text, the other side is converted to text. A list and a value, or two lists, give a list. |
CASE WHEN … THEN … ELSE … END, CASE x WHEN … END | Conditional value |
CAST(x AS TYPE), x::TYPE | Conversion. A value that cannot be converted fails its row. See Casts. |
TRY_CAST(x AS TYPE) | Conversion that gives null for a value that cannot be converted |
ARRAY[a, b, …], [a, b, …] | A list of the values |
list[i] | A list's element, counting from 1 |
list[a:b], list[a:], list[:b] | The elements from a to b, both included, counting from 1. See Lists. |
s['field'] | A struct's field |
EXTRACT(PART FROM ts) | date_part('PART', ts) |
POSITION(sub IN s) | strpos(s, sub) |
SUBSTRING(s FROM start FOR length) | substr(s, start, length) |
TRIM([BOTH | LEADING | TRAILING] chars FROM s) | btrim, ltrim or rtrim |
OVERLAY(s PLACING r FROM start [FOR length]) | overlay |
DATE '2026-01-01', TIME '12:30:00', TIMESTAMP '2026-01-01 00:00:00', TIMESTAMPTZ '2026-01-01 00:00:00+00', INTERVAL '1 day', INTERVAL '2' DAY | Literals of those types: see Literals |
ts AT TIME ZONE zone | A timestamp in a time zone: see Time zones |
->, ->>, #>, #>>, ? | JSON paths: see JSON |
Any other operator or syntax is refused when the statement is planned, at the place where it is written. Expressions lists examples and their messages. crates/sql/types.txt gives the type of each operator over each pair of types, each cast and each function, and a test fails when the engine gives another.
Literals
| Literal | Value |
|---|---|
42, -7 | An int64. A whole number that int64 cannot hold is refused. |
1.5, .5, 1e3 | A float64: a number with a point or an exponent |
'text' | A string, with '' for a quote. A backslash is an ordinary character: 'a\nb' has four characters. |
E'text' | A string in which a backslash starts an escape, as in Postgres. See the list below. |
$$text$$, $tag$text$tag$ | A string without escapes, which can hold ' and $$ |
TRUE, FALSE, NULL | Booleans, and null |
DATE '2026-01-01' | A date32 |
TIME '12:30:00', TIME '12:30:00.5' | A time64[ns] |
TIMESTAMP '2026-01-01 00:00:00', TIMESTAMP WITHOUT TIME ZONE '…' | A timestamp[ns] |
TIMESTAMPTZ '2026-01-01 00:00:00+00', TIMESTAMP WITH TIME ZONE '…' | A timestamp[ns]: the time in UTC at the instant the text gives |
INTERVAL '1 day 2 hours' | A month_day_nano_interval, with its units inside the quotes |
INTERVAL '2' DAY | A month_day_nano_interval of one unit, after the quotes: YEAR, MONTH, DAY, HOUR, MINUTE or SECOND |
The escapes in E'…' are these:
\n,\t,\r,\band\f: newline, tab, carriage return, backspace and form feed.\\and\': a backslash and a quote.''is a quote too.\x41and\101: a character by its code, in hexadecimal with one or two digits, or in octal with one to three. The code is at most7F.\u00e9and\U0001F600: a character by its Unicode code point, in four or eight hexadecimal digits.- A backslash before any other character is that character.
A string with an escape that is not valid, such as E'\xFF' or E'\0', is a syntax error.
An INTERVAL with its unit after the quotes takes a whole number, which can be negative, such as INTERVAL '-2' HOUR. Only SECOND takes a fraction: INTERVAL '1.5' SECOND. Write INTERVAL '1 day 12 hours' for a value of more than one unit. INTERVAL '1' YEAR TO MONTH, and a unit with a precision such as SECOND(3), are refused.
Arithmetic
+, -, *, / and % take numbers, and the dates and times below. A string or a boolean is refused: CAST it first.
| Operands | Result type |
|---|---|
| Two integers | The wider integer type: int16 + int32 is int32. A whole-number literal is int64, so int16_column + 1 is int64. |
| Two floats | The wider float type |
| An integer or a decimal, and a float | The float's type. float32 holds int64 and decimal values with less precision. A literal such as 1.5 is float64. |
| An integer and a decimal | A decimal. The integer counts as decimal(5, 0) if it is int16, decimal(10, 0) if it is int32, and decimal(20, 0) if it is int64. |
decimal(P1, S1) + or - decimal(P2, S2) | decimal(max(P1 - S1, P2 - S2) + max(S1, S2) + 1, max(S1, S2)) |
decimal(P1, S1) * decimal(P2, S2) | decimal(P1 + P2 + 1, S1 + S2) |
decimal(P1, S1) / decimal(P2, S2) | decimal(P1 + S2 + 4, S1 + 4) |
decimal(P1, S1) % decimal(P2, S2) | decimal(min(P1 - S1, P2 - S2) + max(S1, S2), max(S1, S2)) |
A decimal's precision is at most 38. A result whose precision is more than 38 has a precision of 38 and the same scale.
/of two integers is an integer, truncated toward zero:7 / 2is3, and-7 / 2is-3.%gives the remainder with the sign of its left side:-7 % 2is-1, and7 % -2is1.- An integer or a decimal divided by zero fails its row, and so does its remainder by zero.
- A float divided by zero is
inf,-inforNaN, and its remainder by zero isNaN. - Integer arithmetic that overflows its type fails its row:
int32 + int32isint32, and stays in its range. Arithmetic on constants that overflows is refused when the statement is planned.
| Dates and times | Result type |
|---|---|
date + INTERVAL, INTERVAL + date, date - INTERVAL | date32 |
date + integer, integer + date, date - integer | date32, where the integer counts days |
date - date | int64, the number of days |
date + time, time + date, date - time | timestamp[ns] |
timestamp + INTERVAL, INTERVAL + timestamp, timestamp - INTERVAL | The timestamp's type |
time + INTERVAL, INTERVAL + time, time - INTERVAL | time64[us] |
time + time, time - time, INTERVAL - time | month_day_nano_interval |
INTERVAL + INTERVAL, INTERVAL - INTERVAL, -INTERVAL | month_day_nano_interval |
timestamp - timestamp, timestamp - date, date - timestamp | A duration, which an output cannot hold |
Other combinations, such as INTERVAL * 2 or a timestamp plus an integer, are refused.
Comparison
A comparison gives a boolean, or null if either side is null. IS DISTINCT FROM and IS NOT DISTINCT FROM treat two nulls as equal, and are never null. WHERE keeps a row only where its condition is true.
Values of two types compare as one type:
| One side | The other side | Compared as |
|---|---|---|
| A number | A number | The type that Arithmetic gives the two |
| A string | A number, a date, a time, a timestamp or binary | The other side's type. A string that does not convert fails its row, and a literal that does not convert is refused when the statement is planned: '7' = 7 is true. |
| A date | A timestamp | Timestamps, with the date at midnight |
| A date | An integer | Dates, where the integer counts days from 1970-01-01 |
| A timestamp | A timestamp of another unit, or with or without a time zone | The same instant. A timestamp without a time zone is in UTC. |
| A JSON value | A number, a string, binary, a date, a time or a timestamp | The other side's type. See JSON. |
| Binary | A UUID | Binary |
| A list | A list | Lists, element by element, with their elements compared as this table says |
Other pairs, such as a boolean and a number, are refused. CAST one side to the other's type.
x = ANY (list)is true if an element of the list equalsx. Otherwise, it is null ifxor an element is null, and false if not. An empty list gives false.x = SOME (list)is the same.x <> ALL (list)is false if an element of the list equalsx. Otherwise, it is null ifxor an element is null, and true if not. An empty list gives true.- A null list gives null in both.
NOT (x = ANY (list))isx <> ALL (list). ANYandALLtake these operators only. Other comparisons, such asx > ANY (list)orx = ALL (list), are refused.- Strings compare by their bytes in UTF-8, which is the order of their Unicode code points. Upper case comes before lower case, and accented letters come after
z:'B' < 'a'and'é' > 'z'are true. No collation applies. - Case and trailing spaces count:
'a' = 'A'and'a ' = 'a'are false. min,max,greatestandleastignore nulls.array_sortputs nulls first.
Patterns
LIKEandILIKEmatch the whole string.%matches any characters and_matches one character.ILIKEignores case.- A backslash before a character matches that character:
'a\%'matchesa%, and'a\\'matchesa\. ESCAPE 'c'makescthe escape character, in place of the backslash, which is then an ordinary character:s LIKE '100!%' ESCAPE '!'matches100%.ESCAPE ''gives a pattern no escape character.- With an
ESCAPEother than'\', the pattern is written as a literal. A pattern that ends with its escape character is refused, and so is anESCAPEof more than one character. SIMILAR TOmatches a regular expression, as~does: the pattern can match anywhere in the string.~,~*,!~,!~*and the regular expression functions take the syntax of Rust'sregexcrate. A pattern matches anywhere in the string, unless^and$anchor it.\d,\wand\smatch Unicode digits, word characters and spaces.- Backreferences, such as
\1, and lookaround, such as(?=…), are not part of that syntax. A pattern written as a literal that is not valid is refused when the statement is planned. A pattern from a column that is not valid fails its row.
Lists
list[a:b] gives the elements from position a to position b, both included, counting from 1, as in Postgres:
- A bound before the first element is the first element. A bound after the last element is the last element:
list[0:2]andlist[-5:2]arelist[1:2]. list[:b]starts at the first element, andlist[a:]ends at the last.- A range that holds no element gives an empty list, such as
list[3:2], orlist[6:9]of a list of five elements. - A null list or a null bound gives null.
- A slice with a step, such as
list[1:5:2], is refused.
array_slice(list, a, b) differs: a negative bound counts from the end of the list.
Time zones
ts AT TIME ZONE zone, and the function timezone(zone, ts), convert between a timestamp with a time zone and the wall-clock time in a zone:
ts is | The result is |
|---|---|
| A timestamp without a time zone | The instant at which a clock in zone shows ts, as a timestamp with a time zone |
| A timestamp with a time zone | The time that a clock in zone shows at the instant ts, as a timestamp without a time zone |
A timestamp with a time zone and a timestamp without one are different types of output column. A destination writes each as its own column type, as it does for CAST(ts AS TIMESTAMP).
INSERT INTO orders_local
SELECT id,
created_at AT TIME ZONE 'America/New_York' AS created_local,
CAST(created_at AT TIME ZONE 'Asia/Kolkata' AS DATE) AS india_day,
local_time AT TIME ZONE store_zone AS happened_at
FROM orderszoneis the IANA name of a time zone, such as'Europe/Berlin', in any case, or'UTC'. It can come from a column, so it can differ by row.- An offset such as
'+05:30'or'UTC-3'is not a zone. Write a zone's name, such as'Asia/Kolkata'. - An abbreviation such as
'CET'or'PST'is not a zone. Write a zone's name, such as'Europe/Berlin'. - A zone written as a literal, or as an expression of literals such as
'Europe/' || 'Berlin', is checked at plan time. If it is not a zone, the statement is refused. - A zone from a column is checked for each row. If it is not a zone, the row fails.
- Daylight saving can skip a wall-clock time or repeat it. A skipped time is read with the offset before the change, and a repeated time with the offset after it. Each gives the later of the two instants. In Berlin,
TIMESTAMP '2024-03-31 02:30:00'is2024-03-31 01:30:00+00, andTIMESTAMP '2024-10-27 02:30:00'is2024-10-27 01:30:00+00. - A null
tsorzonegives null. AT TIME ZONEbinds more tightly than arithmetic:ts AT TIME ZONE 'UTC' + INTERVAL '1 hour'adds the hour to the result.
These forms are refused at plan time. A zone from a column that is an offset, an abbreviation or an unknown name is not refused: each row with such a zone fails instead.
| What | Message |
|---|---|
| A literal offset as the zone | '+05:30' is an offset, not a time zone's name: write an IANA name, such as 'Asia/Kolkata' |
| A literal abbreviation as the zone | 'CET' is an abbreviation, not a time zone's name: write an IANA name, such as 'Europe/Berlin', or 'UTC' |
| A literal name that is not a time zone | 'Nowhere/Land' isn't a time zone transforms know: write an IANA name such as 'Europe/Berlin', or 'UTC' |
| A date | AT TIME ZONE takes a timestamp, not a date: CAST the date to TIMESTAMP for midnight in the zone, or to TIMESTAMPTZ for midnight UTC |
| A string, or a text column | AT TIME ZONE takes a timestamp, not text: write TIMESTAMP '…' for a wall-clock time or TIMESTAMPTZ '…' for an instant, or CAST the text to one |
An untyped NULL | AT TIME ZONE takes a timestamp, and NULL has no type: write CAST(NULL AS TIMESTAMP) or CAST(NULL AS TIMESTAMPTZ) |
| A time of day | AT TIME ZONE takes a timestamp, and a time of day has no date to place it in a zone: add it to a date first |
A zone that is not text, such as an INTERVAL | a time zone is text, such as 'Europe/Berlin', not Interval(MonthDayNano) |
Types
A statement reads each column with the logical type that the stream's schema gives it, such as int64, utf8, decimal(12, 2) or timestamp[us, tz=UTC]. Statements see every column as nullable.
An output's columns have the engine's logical types. A value of another type is converted to one that holds it:
| A statement gives | The output column has |
|---|---|
bool, int16, int32, int64, float32, float64, utf8, binary, date32, time64[us], month_day_nano_interval, decimal(P, S) with P from 1 to 38 and S from 0 to P | The same type |
int8, uint8 | int16 |
uint16 | int32 |
uint32, uint64 | int64 |
float16 | float32 |
| A large or view string or binary type | utf8 or binary |
| A timestamp in seconds, milliseconds or microseconds | timestamp[us] |
| A timestamp in nanoseconds | timestamp[ns] |
| A timestamp with a time zone | The same instant, with time zone UTC |
| A list | A list of the converted element type |
A JSON column, or a value from ->, #> or json_get | JSON, written as the document's own text |
| A UUID column, passed through | A UUID |
Any other type is refused: a struct, a map, a duration, a time in a unit other than microseconds, and an untyped NULL. The message names the type and asks for a cast:
column s is Struct("a": Int64), which an output can't hold: CAST it to a type it canCasts
CAST, TRY_CAST and :: take these SQL type names, and no others:
| SQL type | Type |
|---|---|
BOOLEAN, BOOL | bool |
TINYINT | int8, written as int16 |
SMALLINT, INT2 | int16 |
INT, INTEGER, INT4 | int32 |
BIGINT, INT8 | int64 |
TINYINT UNSIGNED, SMALLINT UNSIGNED, INT UNSIGNED, INTEGER UNSIGNED, BIGINT UNSIGNED | Unsigned integers, written as wider signed ones |
REAL, FLOAT4, FLOAT(P) with P from 1 to 24 | float32 |
DOUBLE PRECISION, DOUBLE, FLOAT8, FLOAT, FLOAT(P) with P from 25 to 53 | float64 |
NUMERIC(P, S), DECIMAL(P, S), DEC(P, S) | decimal(P, S) |
NUMERIC(P), DECIMAL(P), DEC(P) | decimal(P, 0) |
NUMERIC, DECIMAL, DEC | decimal(38, 10) |
VARCHAR, TEXT, CHARACTER VARYING, CHAR VARYING | utf8 |
VARCHAR(N), CHARACTER VARYING(N), CHAR VARYING(N) | utf8, cut to its first N characters |
BYTEA | binary |
DATE | date32 |
TIME, TIME WITHOUT TIME ZONE | time64[ns], which an output cannot hold: use it in an expression that gives another type, such as date + time |
TIMESTAMP, TIMESTAMP WITHOUT TIME ZONE, TIMESTAMPTZ, TIMESTAMP WITH TIME ZONE | timestamp[ns] |
TIMESTAMP(6), TIMESTAMP(6) WITHOUT TIME ZONE, TIMESTAMPTZ(6), TIMESTAMP(6) WITH TIME ZONE | timestamp[us] |
TIMESTAMP(9), TIMESTAMP(9) WITHOUT TIME ZONE, TIMESTAMPTZ(9), TIMESTAMP(9) WITH TIME ZONE | timestamp[ns] |
INTERVAL | month_day_nano_interval |
FLOATandFLOAT(P)follow Postgres:FLOATisDOUBLE PRECISION, and so isFLOAT(P)from 25 bits.FLOAT(P)up to 24 bits isREAL.NUMERICwithout a precision isdecimal(38, 10). A cast to it rounds a value to 10 decimal places, and a value with more than 28 digits before the point fails its row.- A cast to a decimal rounds a value to the decimal's scale, half away from zero:
CAST(CAST('2.5' AS NUMERIC(2, 1)) AS NUMERIC(1))is3. A value that the decimal's precision cannot hold fails its row. - A cast to
VARCHAR(N)cuts the string to its firstNcharacters, as a cast in Postgres does:CAST('héllo' AS VARCHAR(2))is'hé'.Nis from 1 to 10485760. - Other type names are refused, each with the name to write instead. Among them are
CHARandCHARACTER, with or without a length (writeVARCHAR(N)to cut a string, orrpad(s, n)to pad it),FLOAT(P)withPabove 53, a timestamp's precision other than 6 or 9,TIMEwith a precision or a time zone,INTERVALwith fields, integer types with a width, and list types. JSONandUUIDare not SQL types that casts take, and a UUID cannot be cast to a string. A cast of a JSON value reads it as the type it names: see JSON.- A
DOUBLEthatREALcannot hold becomesinfor-inf.