Skip to Content
OpenETL
Reference

SQL expressions and types

Look up SQL operators, time zones, data types and casts.

Edit on GitHub

Use this reference when you write a SQL transform.

Expressions and operators

Operator or syntaxMeaning
+, -, *, /, %Arithmetic: see Arithmetic. A timestamp plus or minus an INTERVAL is a timestamp.
-x, +xNegation, and the value itself
=, <>, !=, <, <=, >, >=Comparison: see Comparison
AND, OR, NOTLogic
IS NULL, IS NOT NULL, IS TRUE, IS NOT TRUE, IS FALSE, IS NOT FALSE, IS UNKNOWN, IS NOT UNKNOWNTests that are never null
IS DISTINCT FROM, IS NOT DISTINCT FROMComparison 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 TOPattern 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 … ENDConditional value
CAST(x AS TYPE), x::TYPEConversion. 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' DAYLiterals of those types: see Literals
ts AT TIME ZONE zoneA 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

LiteralValue
42, -7An int64. A whole number that int64 cannot hold is refused.
1.5, .5, 1e3A 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, NULLBooleans, 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' DAYA 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, \b and \f: newline, tab, carriage return, backspace and form feed.
  • \\ and \': a backslash and a quote. '' is a quote too.
  • \x41 and \101: a character by its code, in hexadecimal with one or two digits, or in octal with one to three. The code is at most 7F.
  • \u00e9 and \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.

OperandsResult type
Two integersThe wider integer type: int16 + int32 is int32. A whole-number literal is int64, so int16_column + 1 is int64.
Two floatsThe wider float type
An integer or a decimal, and a floatThe float's type. float32 holds int64 and decimal values with less precision. A literal such as 1.5 is float64.
An integer and a decimalA 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 / 2 is 3, and -7 / 2 is -3.
  • % gives the remainder with the sign of its left side: -7 % 2 is -1, and 7 % -2 is 1.
  • 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, -inf or NaN, and its remainder by zero is NaN.
  • Integer arithmetic that overflows its type fails its row: int32 + int32 is int32, and stays in its range. Arithmetic on constants that overflows is refused when the statement is planned.
Dates and timesResult type
date + INTERVAL, INTERVAL + date, date - INTERVALdate32
date + integer, integer + date, date - integerdate32, where the integer counts days
date - dateint64, the number of days
date + time, time + date, date - timetimestamp[ns]
timestamp + INTERVAL, INTERVAL + timestamp, timestamp - INTERVALThe timestamp's type
time + INTERVAL, INTERVAL + time, time - INTERVALtime64[us]
time + time, time - time, INTERVAL - timemonth_day_nano_interval
INTERVAL + INTERVAL, INTERVAL - INTERVAL, -INTERVALmonth_day_nano_interval
timestamp - timestamp, timestamp - date, date - timestampA 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 sideThe other sideCompared as
A numberA numberThe type that Arithmetic gives the two
A stringA number, a date, a time, a timestamp or binaryThe 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 dateA timestampTimestamps, with the date at midnight
A dateAn integerDates, where the integer counts days from 1970-01-01
A timestampA timestamp of another unit, or with or without a time zoneThe same instant. A timestamp without a time zone is in UTC.
A JSON valueA number, a string, binary, a date, a time or a timestampThe other side's type. See JSON.
BinaryA UUIDBinary
A listA listLists, 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 equals x. Otherwise, it is null if x or 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 equals x. Otherwise, it is null if x or an element is null, and true if not. An empty list gives true.
  • A null list gives null in both. NOT (x = ANY (list)) is x <> ALL (list).
  • ANY and ALL take these operators only. Other comparisons, such as x > ANY (list) or x = 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, greatest and least ignore nulls. array_sort puts nulls first.

Patterns

  • LIKE and ILIKE match the whole string. % matches any characters and _ matches one character. ILIKE ignores case.
  • A backslash before a character matches that character: 'a\%' matches a%, and 'a\\' matches a\.
  • ESCAPE 'c' makes c the escape character, in place of the backslash, which is then an ordinary character: s LIKE '100!%' ESCAPE '!' matches 100%. ESCAPE '' gives a pattern no escape character.
  • With an ESCAPE other than '\', the pattern is written as a literal. A pattern that ends with its escape character is refused, and so is an ESCAPE of more than one character.
  • SIMILAR TO matches a regular expression, as ~ does: the pattern can match anywhere in the string.
  • ~, ~*, !~, !~* and the regular expression functions take the syntax of Rust's regex crate. A pattern matches anywhere in the string, unless ^ and $ anchor it. \d, \w and \s match 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] and list[-5:2] are list[1:2].
  • list[:b] starts at the first element, and list[a:] ends at the last.
  • A range that holds no element gives an empty list, such as list[3:2], or list[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 isThe result is
A timestamp without a time zoneThe instant at which a clock in zone shows ts, as a timestamp with a time zone
A timestamp with a time zoneThe 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 orders
  • zone is 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' is 2024-03-31 01:30:00+00, and TIMESTAMP '2024-10-27 02:30:00' is 2024-10-27 01:30:00+00.
  • A null ts or zone gives null.
  • AT TIME ZONE binds 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.

WhatMessage
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 dateAT 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 columnAT 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 NULLAT TIME ZONE takes a timestamp, and NULL has no type: write CAST(NULL AS TIMESTAMP) or CAST(NULL AS TIMESTAMPTZ)
A time of dayAT 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 INTERVALa 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 givesThe 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 PThe same type
int8, uint8int16
uint16int32
uint32, uint64int64
float16float32
A large or view string or binary typeutf8 or binary
A timestamp in seconds, milliseconds or microsecondstimestamp[us]
A timestamp in nanosecondstimestamp[ns]
A timestamp with a time zoneThe same instant, with time zone UTC
A listA list of the converted element type
A JSON column, or a value from ->, #> or json_getJSON, written as the document's own text
A UUID column, passed throughA 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 can

Casts

CAST, TRY_CAST and :: take these SQL type names, and no others:

SQL typeType
BOOLEAN, BOOLbool
TINYINTint8, written as int16
SMALLINT, INT2int16
INT, INTEGER, INT4int32
BIGINT, INT8int64
TINYINT UNSIGNED, SMALLINT UNSIGNED, INT UNSIGNED, INTEGER UNSIGNED, BIGINT UNSIGNEDUnsigned integers, written as wider signed ones
REAL, FLOAT4, FLOAT(P) with P from 1 to 24float32
DOUBLE PRECISION, DOUBLE, FLOAT8, FLOAT, FLOAT(P) with P from 25 to 53float64
NUMERIC(P, S), DECIMAL(P, S), DEC(P, S)decimal(P, S)
NUMERIC(P), DECIMAL(P), DEC(P)decimal(P, 0)
NUMERIC, DECIMAL, DECdecimal(38, 10)
VARCHAR, TEXT, CHARACTER VARYING, CHAR VARYINGutf8
VARCHAR(N), CHARACTER VARYING(N), CHAR VARYING(N)utf8, cut to its first N characters
BYTEAbinary
DATEdate32
TIME, TIME WITHOUT TIME ZONEtime64[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 ZONEtimestamp[ns]
TIMESTAMP(6), TIMESTAMP(6) WITHOUT TIME ZONE, TIMESTAMPTZ(6), TIMESTAMP(6) WITH TIME ZONEtimestamp[us]
TIMESTAMP(9), TIMESTAMP(9) WITHOUT TIME ZONE, TIMESTAMPTZ(9), TIMESTAMP(9) WITH TIME ZONEtimestamp[ns]
INTERVALmonth_day_nano_interval
  • FLOAT and FLOAT(P) follow Postgres: FLOAT is DOUBLE PRECISION, and so is FLOAT(P) from 25 bits. FLOAT(P) up to 24 bits is REAL.
  • NUMERIC without a precision is decimal(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)) is 3. A value that the decimal's precision cannot hold fails its row.
  • A cast to VARCHAR(N) cuts the string to its first N characters, as a cast in Postgres does: CAST('héllo' AS VARCHAR(2)) is 'hé'. N is from 1 to 10485760.
  • Other type names are refused, each with the name to write instead. Among them are CHAR and CHARACTER, with or without a length (write VARCHAR(N) to cut a string, or rpad(s, n) to pad it), FLOAT(P) with P above 53, a timestamp's precision other than 6 or 9, TIME with a precision or a time zone, INTERVAL with fields, integer types with a width, and list types.
  • JSON and UUID are 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 DOUBLE that REAL cannot hold becomes inf or -inf.

On this page