This page explains BigQuery expressions, including functions and operators.
Function call rules
The following rules apply to all functions unless explicitly indicated otherwise in the function description:
- For functions that accept numeric types, if one operand is a floating point operand and the other operand is another numeric type, both operands are converted to FLOAT64 before the function is evaluated.
- If an operand is
NULL, the result isNULL, with the exception of the IS operator. - For functions that are time zone sensitive (as indicated in the function description), the default time zone, UTC, is used if a time zone is not specified.
SAFE. prefix
Syntax:
SAFE.function_name()
Description
If you begin a function with the SAFE. prefix, it will return NULL instead
of an error. The SAFE. prefix only prevents errors from the prefixed function
itself: it does not prevent errors that occur while evaluating argument
expressions. The SAFE. prefix only prevents errors that occur because of the
value of the function inputs, such as "value out of range" errors; other
errors, such as internal or system errors, may still occur. If the function
does not return an error, SAFE. has no effect on the output.
Operators, such as + and =, do not support the SAFE.
prefix. To prevent errors from a division
operation, use SAFE_DIVIDE. Some operators,
such as IN, ARRAY, and UNNEST, resemble functions, but do not support the
SAFE. prefix. The CAST and EXTRACT functions also do not support the
SAFE. prefix. To prevent errors from casting, use
SAFE_CAST.
Example
In the following example, the first use of the SUBSTR function would normally
return an error, because the function does not support length arguments with
negative values. However, the SAFE. prefix causes the function to return
NULL instead. The second use of the SUBSTR function provides the expected
output: the SAFE. prefix has no effect.
SELECT SAFE.SUBSTR('foo', 0, -2) AS safe_output UNION ALL
SELECT SAFE.SUBSTR('bar', 0, 2) AS safe_output;
+-------------+
| safe_output |
+-------------+
| NULL |
| ba |
+-------------+
Supported functions
BigQuery supports the use of the SAFE. prefix with most scalar
functions that can raise errors, including STRING functions,
math functions, DATE functions,
DATETIME functions, and
TIMESTAMP functions. BigQuery does not support
the use of the SAFE. prefix with aggregate,
analytic, or user-defined functions.
Calling persistent user-defined functions (UDFs)
After creating a persistent UDF, you can call it as you would any other function, prepended with the name of the dataset in which it is defined as a prefix.
Syntax
[`project_name`].dataset_name.function_name([parameter_value[, ...]])
To call a UDF in a project other than the project that you are using to run
the query, project_name is required.
Examples
The following example creates a UDF named multiply_by_three and calls it
from the same project.
CREATE FUNCTION my_dataset.multiply_by_three(x INT64) AS (x * 3);
SELECT my_dataset.multiply_by_three(5) AS result; -- returns 15
The following example calls a persistent UDF from a different project.
CREATE `other_project`.other_dataset.other_function(x INT64, y INT64)
AS (x * y * 2);
SELECT `other_project`.other_dataset.other_function(3, 4); --returns 24
Conversion rules
Conversion includes, but is not limited to, casting, coercion, and supertyping.
- Casting is explicit conversion and uses the
CAST()function. - Coercion is implicit conversion, which BigQuery performs automatically under the conditions described below.
- A supertype is a common type to which two or more expressions can be coerced.
There are also conversions that have their own function names, such as
PARSE_DATE(). To learn more about these functions, see
Conversion functions
Comparison of casting and coercion
The following table summarizes all possible cast and coercion possibilities for BigQuery data types. The Coerce to column applies to all expressions of a given data type, (for example, a column), but literals and parameters can also be coerced. See literal coercion and parameter coercion for details.
| From type | Cast to | Coerce to |
|---|---|---|
| INT64 | BOOL INT64 NUMERIC BIGNUMERIC FLOAT64 STRING |
NUMERIC BIGNUMERIC FLOAT64 |
| NUMERIC | INT64 NUMERIC BIGNUMERIC FLOAT64 STRING |
BIGNUMERIC FLOAT64 |
| BIGNUMERIC | INT64 NUMERIC BIGNUMERIC FLOAT64 STRING |
FLOAT64 |
| FLOAT64 | INT64 NUMERIC BIGNUMERIC FLOAT64 STRING |
|
| BOOL | BOOL INT64 STRING |
|
| STRING | BOOL INT64 NUMERIC BIGNUMERIC FLOAT64 STRING BYTES DATE DATETIME TIME TIMESTAMP |
|
| BYTES | STRING BYTES |
|
| DATE | STRING DATE DATETIME TIMESTAMP |
DATETIME |
| DATETIME | STRING DATE DATETIME TIME TIMESTAMP |
|
| TIME | STRING TIME |
|
| TIMESTAMP | STRING DATE DATETIME TIME TIMESTAMP |
|
| ARRAY | ARRAY | |
| STRUCT | STRUCT |
Casting
Most data types can be cast from one type to another with the CAST function.
When using CAST, a query can fail if BigQuery is unable to perform
the cast. If you want to protect your queries from these types of errors, you
can use SAFE_CAST. To learn more about the rules for CAST, SAFE_CAST and
other casting functions, see
Conversion functions.
Coercion
BigQuery coerces the result type of an argument expression to another
type if needed to match function signatures. For example, if function func()
is defined to take a single argument of type FLOAT64
and an expression is used as an argument that has a result type of
INT64, then the result of the expression will be
coerced to FLOAT64 type before func() is computed.
Literal coercion
BigQuery supports the following literal coercions:
| Input data type | Result data type | Notes |
|---|---|---|
| STRING literal | DATE DATETIME TIME TIMESTAMP |
Literal coercion is needed when the actual literal type is different from the
type expected by the function in question. For
example, if function func() takes a DATE argument,
then the expression func("2014-09-27") is valid because the
string literal "2014-09-27" is coerced to
DATE.
Literal conversion is evaluated at analysis time, and gives an error if the input literal cannot be converted successfully to the target type.
Parameter coercion
BigQuery supports the following parameter coercions:
| Input data type | Result data type |
|---|---|
| STRING parameter |
If the parameter value cannot be coerced successfully to the target type, an error is provided.
Supertypes
A supertype is a common type to which two or more expressions can be coerced.
Supertypes are used with set operations such as UNION ALL and expressions such
as CASE that expect multiple arguments with matching types. Each type has one
or more supertypes, including itself, which defines its set of supertypes.
| Input type | Supertypes |
|---|---|
| BOOL | BOOL |
| INT64 |
INT64 FLOAT64 NUMERIC BIGNUMERIC |
| FLOAT64 |
FLOAT64 |
| NUMERIC |
NUMERIC BIGNUMERIC FLOAT64 |
| DECIMAL |
DECIMAL BIGDECIMAL FLOAT64 |
| BIGNUMERIC |
BIGNUMERIC FLOAT64 |
| BIGDECIMAL |
BIGDECIMAL FLOAT64 |
| STRING | STRING |
| DATE | DATE |
| TIME | TIME |
| DATETIME | DATETIME |
| TIMESTAMP | TIMESTAMP |
| BYTES | BYTES |
| STRUCT | STRUCT with the same field position types. |
| ARRAY | ARRAY with the same element types. |
| GEOGRAPHY | GEOGRAPHY |
If you want to find the supertype for a set of input types, first determine the intersection of the set of supertypes for each input type. If that set is empty then the input types have no common supertype. If that set is non-empty, then the common supertype is generally the most specific type in that set. Generally, the most specific type is the type with the most restrictive domain.
Examples
| Input types | Common supertype | Returns | Notes |
|---|---|---|---|
|
INT64 FLOAT64 |
FLOAT64 | FLOAT64 | If you apply supertyping to INT64 and FLOAT64, supertyping succeeds because they they share a supertype, FLOAT64. |
|
INT64 BOOL |
None | Error | If you apply supertyping to INT64 and BOOL, supertyping fails because they do not share a common supertype. |
Exact and inexact types
Numeric types can be exact or inexact. For supertyping, if all of the input types are exact types, then the resulting supertype can only be an exact type.
The following table contains a list of exact and inexact numeric data types.
| Exact types | Inexact types |
|---|---|
|
INT64 NUMERIC BIGNUMERIC |
FLOAT64 |
Examples
| Input types | Common supertype | Returns | Notes |
|---|---|---|---|
|
INT64 FLOAT64 |
FLOAT64 | FLOAT64 | If supertyping is applied to INT64 and DOUBLE, supertyping succeeds because there are exact and inexact numeric types being supertyped. |
Types specificity
Each type has a domain of values that it supports. A type with a
narrow domain is more specific than a type with a wider domain. Exact types
are more specific than inexact types because inexact types have a wider range
of domain values that are supported than exact types. For example,
INT64 is more specific than FLOAT64.
Supertypes and literals
Supertype rules for literals are more permissive than for normal expressions, and are consistent with implicit coercion rules. The following algorithm is used when the input set of types includes types related to literals:
- If there exists non-literals in the set, find the set of common supertypes of the non-literals.
- If there is at least one possible supertype, find the most specific type to which the remaining literal types can be implicitly coerced and return that supertype. Otherwise, there is no supertype.
- If the set only contains types related to literals, compute the supertype of the literal types.
- If all input types are related to
NULLliterals, then the resulting supertype isINT64. - If no common supertype is found, an error is produced.
Examples
| Input types | Common supertype | Returns |
|---|---|---|
|
INT64 literal UINT64 expression |
UINT64 | UINT64 |
|
TIMESTAMP expression STRING literal |
TIMESTAMP | TIMESTAMP |
|
NULL literal NULL literal |
INT64 | INT64 |
|
BOOL literal TIMESTAMP literal |
None | Error |
Aggregate functions
An aggregate function is a function that summarizes the rows of a group into a
single value. COUNT, MIN and MAX are examples of aggregate functions.
SELECT COUNT(*) as total_count, COUNT(fruit) as non_null_count,
MIN(fruit) as min, MAX(fruit) as max
FROM (SELECT NULL as fruit UNION ALL
SELECT "apple" as fruit UNION ALL
SELECT "pear" as fruit UNION ALL
SELECT "orange" as fruit)
+-------------+----------------+-------+------+
| total_count | non_null_count | min | max |
+-------------+----------------+-------+------+
| 4 | 3 | apple | pear |
+-------------+----------------+-------+------+
When used in conjunction with a GROUP BY clause, the groups summarized
typically have at least one row. When the associated SELECT has no GROUP BY
clause or when certain aggregate function modifiers filter rows from the group
to be summarized it is possible that the aggregate function needs to summarize
an empty group. In this case, the COUNT and COUNTIF functions return 0,
while all other aggregate functions return NULL.
The following sections describe the aggregate functions that BigQuery supports.
ANY_VALUE
ANY_VALUE(expression) [OVER (...)]
Description
Returns expression for some row chosen from the group. Which row is chosen is
nondeterministic, not random. Returns NULL when the input produces no
rows. Returns NULL when expression is NULL for all rows in the group.
ANY_VALUE behaves as if RESPECT NULLS is specified;
Rows for which expression is NULL are considered and may be selected.
Supported Argument Types
Any
Optional Clause
OVER: Specifies a window. See
Analytic Functions.
Returned Data Types
Matches the input data type.
Examples
SELECT ANY_VALUE(fruit) as any_value
FROM UNNEST(["apple", "banana", "pear"]) as fruit;
+-----------+
| any_value |
+-----------+
| apple |
+-----------+
SELECT
fruit,
ANY_VALUE(fruit) OVER (ORDER BY LENGTH(fruit) ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS any_value
FROM UNNEST(["apple", "banana", "pear"]) as fruit;
+--------+-----------+
| fruit | any_value |
+--------+-----------+
| pear | pear |
| apple | pear |
| banana | apple |
+--------+-----------+
ARRAY_AGG
ARRAY_AGG([DISTINCT] expression [{IGNORE|RESPECT} NULLS]
[ORDER BY key [{ASC|DESC}] [, ... ]] [LIMIT n])
[OVER (...)]
Description
Returns an ARRAY of expression values.
Supported Argument Types
All data types except ARRAY.
Optional Clauses
The clauses are applied in the following order:
OVER: Specifies a window. See Analytic Functions. This clause is currently incompatible with all other clauses withinARRAY_AGG().DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.IGNORE NULLSorRESPECT NULLS: IfIGNORE NULLSis specified, theNULLvalues are excluded from the result. IfRESPECT NULLSis specified, theNULLvalues are included in the result. If neither is specified, theNULLvalues are included in the result. An error is raised if an array in the final query result contains aNULLelement.ORDER BY: Specifies the order of the values.- For each sort key, the default sort direction is
ASC. - NULLs: In the context of the
ORDER BYclause, NULLs are the minimum possible value; that is, NULLs appear first inASCsorts and last inDESCsorts. - Floating point data types: see Floating Point Semantics on ordering and grouping.
- If
DISTINCTis also specified, then the sort key must be the same asexpression. - If
ORDER BYis not specified, the order of the elements in the output array is non-deterministic, which means you might receive a different result each time you use this function.
- For each sort key, the default sort direction is
LIMIT: Specifies the maximum number ofexpressioninputs in the result. The limitnmust be a constant INT64.
Returned Data Types
ARRAY
If there are zero input rows, this function returns NULL.
Examples
SELECT FORMAT("%T", ARRAY_AGG(x)) AS array_agg
FROM UNNEST([2, 1, -2, 3, -2, 1, 2]) AS x;
+-------------------------+
| array_agg |
+-------------------------+
| [2, 1, -2, 3, -2, 1, 2] |
+-------------------------+
SELECT FORMAT("%T", ARRAY_AGG(DISTINCT x)) AS array_agg
FROM UNNEST([2, 1, -2, 3, -2, 1, 2]) AS x;
+---------------+
| array_agg |
+---------------+
| [2, 1, -2, 3] |
+---------------+
SELECT FORMAT("%T", ARRAY_AGG(x IGNORE NULLS)) AS array_agg
FROM UNNEST([NULL, 1, -2, 3, -2, 1, NULL]) AS x;
+-------------------+
| array_agg |
+-------------------+
| [1, -2, 3, -2, 1] |
+-------------------+
SELECT FORMAT("%T", ARRAY_AGG(x ORDER BY ABS(x))) AS array_agg
FROM UNNEST([2, 1, -2, 3, -2, 1, 2]) AS x;
+-------------------------+
| array_agg |
+-------------------------+
| [1, 1, 2, -2, -2, 2, 3] |
+-------------------------+
SELECT FORMAT("%T", ARRAY_AGG(x LIMIT 5)) AS array_agg
FROM UNNEST([2, 1, -2, 3, -2, 1, 2]) AS x;
+-------------------+
| array_agg |
+-------------------+
| [2, 1, -2, 3, -2] |
+-------------------+
SELECT FORMAT("%T", ARRAY_AGG(DISTINCT x IGNORE NULLS ORDER BY x LIMIT 2)) AS array_agg
FROM UNNEST([NULL, 1, -2, 3, -2, 1, NULL]) AS x;
+-----------+
| array_agg |
+-----------+
| [-2, 1] |
+-----------+
SELECT
x,
FORMAT("%T", ARRAY_AGG(x) OVER (ORDER BY ABS(x))) AS array_agg
FROM UNNEST([2, 1, -2, 3, -2, 1, 2]) AS x;
+----+-------------------------+
| x | array_agg |
+----+-------------------------+
| 1 | [1, 1] |
| 1 | [1, 1] |
| 2 | [1, 1, 2, -2, -2, 2] |
| -2 | [1, 1, 2, -2, -2, 2] |
| -2 | [1, 1, 2, -2, -2, 2] |
| 2 | [1, 1, 2, -2, -2, 2] |
| 3 | [1, 1, 2, -2, -2, 2, 3] |
+----+-------------------------+
ARRAY_CONCAT_AGG
ARRAY_CONCAT_AGG(expression [ORDER BY key [{ASC|DESC}] [, ... ]] [LIMIT n])
Description
Concatenates elements from expression of type
ARRAY, returning a single
ARRAY as a result. This function ignores NULL input
arrays, but respects the NULL elements in non-NULL input arrays
(an error is raised, however, if an array in the final query result contains a NULL element).
Supported Argument Types
ARRAY
Optional Clauses
The clauses are applied in the following order:
ORDER BY: Specifies the order of the values.- For each sort key, the default sort direction is
ASC. - Array ordering is not supported, and thus the sort key cannot be the
same as
expression. - NULLs: In the context of the
ORDER BYclause, NULLs are the minimum possible value; that is, NULLs appear first inASCsorts and last inDESCsorts. - Floating point data types: see Floating Point Semantics on ordering and grouping.
- If
ORDER BYis not specified, the order of the elements in the output array is non-deterministic, which means you might receive a different result each time you use this function.
- For each sort key, the default sort direction is
LIMIT: Specifies the maximum number ofexpressioninputs in the result. The limit applies to the number of input arrays, not the number of elements in the arrays. An empty array counts as 1. A NULL array is not counted. The limitnmust be a constant INT64.
Returned Data Types
ARRAY
Returns NULL if there are zero input
rows or expression evaluates to NULL for all rows.
Examples
SELECT FORMAT("%T", ARRAY_CONCAT_AGG(x)) AS array_concat_agg FROM (
SELECT [NULL, 1, 2, 3, 4] AS x
UNION ALL SELECT NULL
UNION ALL SELECT [5, 6]
UNION ALL SELECT [7, 8, 9]
);
+-----------------------------------+
| array_concat_agg |
+-----------------------------------+
| [NULL, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
+-----------------------------------+
SELECT FORMAT("%T", ARRAY_CONCAT_AGG(x ORDER BY ARRAY_LENGTH(x))) AS array_concat_agg FROM (
SELECT [1, 2, 3, 4] AS x
UNION ALL SELECT [5, 6]
UNION ALL SELECT [7, 8, 9]
);
+-----------------------------------+
| array_concat_agg |
+-----------------------------------+
| [5, 6, 7, 8, 9, 1, 2, 3, 4] |
+-----------------------------------+
SELECT FORMAT("%T", ARRAY_CONCAT_AGG(x LIMIT 2)) AS array_concat_agg FROM (
SELECT [1, 2, 3, 4] AS x
UNION ALL SELECT [5, 6]
UNION ALL SELECT [7, 8, 9]
);
+--------------------------+
| array_concat_agg |
+--------------------------+
| [1, 2, 3, 4, 5, 6] |
+--------------------------+
SELECT FORMAT("%T", ARRAY_CONCAT_AGG(x ORDER BY ARRAY_LENGTH(x) LIMIT 2)) AS array_concat_agg FROM (
SELECT [1, 2, 3, 4] AS x
UNION ALL SELECT [5, 6]
UNION ALL SELECT [7, 8, 9]
);
+------------------+
| array_concat_agg |
+------------------+
| [5, 6, 7, 8, 9] |
+------------------+
AVG
AVG([DISTINCT] expression) [OVER (...)]
Description
Returns the average of non-NULL input values, or NaN if the input contains a
NaN.
Supported Argument Types
Any numeric input type, such as INT64. Note that, for floating point input types, the return result is non-deterministic, which means you might receive a different result each time you use this function.
Optional Clauses
The clauses are applied in the following order:
OVER: Specifies a window. See Analytic Functions. This clause is currently incompatible with all other clauses withinAVG().DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.
Returned Data Types
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
Examples
SELECT AVG(x) as avg
FROM UNNEST([0, 2, 4, 4, 5]) as x;
+-----+
| avg |
+-----+
| 3 |
+-----+
SELECT AVG(DISTINCT x) AS avg
FROM UNNEST([0, 2, 4, 4, 5]) AS x;
+------+
| avg |
+------+
| 2.75 |
+------+
SELECT
x,
AVG(x) OVER (ORDER BY x ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS avg
FROM UNNEST([0, 2, NULL, 4, 4, 5]) AS x;
+------+------+
| x | avg |
+------+------+
| NULL | NULL |
| 0 | 0 |
| 2 | 1 |
| 4 | 3 |
| 4 | 4 |
| 5 | 4.5 |
+------+------+
BIT_AND
BIT_AND(expression)
Description
Performs a bitwise AND operation on expression and returns the result.
Supported Argument Types
- INT64
Returned Data Types
INT64
Examples
SELECT BIT_AND(x) as bit_and FROM UNNEST([0xF001, 0x00A1]) as x;
+---------+
| bit_and |
+---------+
| 1 |
+---------+
BIT_OR
BIT_OR(expression)
Description
Performs a bitwise OR operation on expression and returns the result.
Supported Argument Types
- INT64
Returned Data Types
INT64
Examples
SELECT BIT_OR(x) as bit_or FROM UNNEST([0xF001, 0x00A1]) as x;
+--------+
| bit_or |
+--------+
| 61601 |
+--------+
BIT_XOR
BIT_XOR([DISTINCT] expression)
Description
Performs a bitwise XOR operation on expression and returns the result.
Supported Argument Types
- INT64
Optional Clause
DISTINCT: Each distinct value of
expression is aggregated only once into the result.
Returned Data Types
INT64
Examples
SELECT BIT_XOR(x) AS bit_xor FROM UNNEST([5678, 1234]) AS x;
+---------+
| bit_xor |
+---------+
| 4860 |
+---------+
SELECT BIT_XOR(x) AS bit_xor FROM UNNEST([1234, 5678, 1234]) AS x;
+---------+
| bit_xor |
+---------+
| 5678 |
+---------+
SELECT BIT_XOR(DISTINCT x) AS bit_xor FROM UNNEST([1234, 5678, 1234]) AS x;
+---------+
| bit_xor |
+---------+
| 4860 |
+---------+
COUNT
1.
COUNT(*) [OVER (...)]
2.
COUNT([DISTINCT] expression) [OVER (...)]
Description
- Returns the number of rows in the input.
- Returns the number of rows with
expressionevaluated to any value other thanNULL.
Supported Argument Types
expression can be any data type. If
DISTINCT is present, expression can only be a data type that is
groupable.
Optional Clauses
The clauses are applied in the following order:
OVER: Specifies a window. See Analytic Functions.DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.
Return Data Types
INT64
Examples
SELECT
COUNT(*) AS count_star,
COUNT(DISTINCT x) AS count_dist_x
FROM UNNEST([1, 4, 4, 5]) AS x;
+------------+--------------+
| count_star | count_dist_x |
+------------+--------------+
| 4 | 3 |
+------------+--------------+
SELECT
x,
COUNT(*) OVER (PARTITION BY MOD(x, 3)) AS count_star,
COUNT(DISTINCT x) OVER (PARTITION BY MOD(x, 3)) AS count_dist_x
FROM UNNEST([1, 4, 4, 5]) AS x;
+------+------------+--------------+
| x | count_star | count_dist_x |
+------+------------+--------------+
| 1 | 3 | 2 |
| 4 | 3 | 2 |
| 4 | 3 | 2 |
| 5 | 1 | 1 |
+------+------------+--------------+
SELECT
x,
COUNT(*) OVER (PARTITION BY MOD(x, 3)) AS count_star,
COUNT(x) OVER (PARTITION BY MOD(x, 3)) AS count_x
FROM UNNEST([1, 4, NULL, 4, 5]) AS x;
+------+------------+---------+
| x | count_star | count_x |
+------+------------+---------+
| NULL | 1 | 0 |
| 1 | 3 | 3 |
| 4 | 3 | 3 |
| 4 | 3 | 3 |
| 5 | 1 | 1 |
+------+------------+---------+
COUNTIF
COUNTIF(expression) [OVER (...)]
Description
Returns the count of TRUE values for expression. Returns 0 if there are
zero input rows, or if expression evaluates to FALSE or NULL for all rows.
Supported Argument Types
BOOL
Optional Clause
OVER: Specifies a window. See
Analytic Functions.
Return Data Types
INT64
Examples
SELECT COUNTIF(x<0) AS num_negative, COUNTIF(x>0) AS num_positive
FROM UNNEST([5, -2, 3, 6, -10, -7, 4, 0]) AS x;
+--------------+--------------+
| num_negative | num_positive |
+--------------+--------------+
| 3 | 4 |
+--------------+--------------+
SELECT
x,
COUNTIF(x<0) OVER (ORDER BY ABS(x) ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS num_negative
FROM UNNEST([5, -2, 3, 6, -10, NULL, -7, 4, 0]) AS x;
+------+--------------+
| x | num_negative |
+------+--------------+
| NULL | 0 |
| 0 | 1 |
| -2 | 1 |
| 3 | 1 |
| 4 | 0 |
| 5 | 0 |
| 6 | 1 |
| -7 | 2 |
| -10 | 2 |
+------+--------------+
LOGICAL_AND
LOGICAL_AND(expression)
Description
Returns the logical AND of all non-NULL expressions. Returns NULL if there
are zero input rows or expression evaluates to NULL for all rows.
Supported Argument Types
BOOL
Return Data Types
BOOL
Examples
SELECT LOGICAL_AND(x) AS logical_and FROM UNNEST([true, false, true]) AS x;
+-------------+
| logical_and |
+-------------+
| false |
+-------------+
LOGICAL_OR
LOGICAL_OR(expression)
Description
Returns the logical OR of all non-NULL expressions. Returns NULL if there
are zero input rows or expression evaluates to NULL for all rows.
Supported Argument Types
BOOL
Return Data Types
BOOL
Examples
SELECT LOGICAL_OR(x) AS logical_or FROM UNNEST([true, false, true]) AS x;
+------------+
| logical_or |
+------------+
| true |
+------------+
MAX
MAX(expression) [OVER (...)]
Description
Returns the maximum value of non-NULL expressions. Returns NULL if there
are zero input rows or expression evaluates to NULL for all rows.
Returns NaN if the input contains a NaN.
Supported Argument Types
Any orderable data type.
Optional Clause
OVER: Specifies a window. See
Analytic Functions.
Return Data Types
Same as the data type used as the input values.
Examples
SELECT MAX(x) AS max
FROM UNNEST([8, 37, 4, 55]) AS x;
+-----+
| max |
+-----+
| 55 |
+-----+
SELECT x, MAX(x) OVER (PARTITION BY MOD(x, 2)) AS max
FROM UNNEST([8, NULL, 37, 4, NULL, 55]) AS x;
+------+------+
| x | max |
+------+------+
| NULL | NULL |
| NULL | NULL |
| 8 | 8 |
| 4 | 8 |
| 37 | 55 |
| 55 | 55 |
+------+------+
MIN
MIN(expression) [OVER (...)]
Description
Returns the minimum value of non-NULL expressions. Returns NULL if there
are zero input rows or expression evaluates to NULL for all rows.
Returns NaN if the input contains a NaN.
Supported Argument Types
Any orderable data type.
Optional Clause
OVER: Specifies a window. See
Analytic Functions.
Return Data Types
Same as the data type used as the input values.
Examples
SELECT MIN(x) AS min
FROM UNNEST([8, 37, 4, 55]) AS x;
+-----+
| min |
+-----+
| 4 |
+-----+
SELECT x, MIN(x) OVER (PARTITION BY MOD(x, 2)) AS min
FROM UNNEST([8, NULL, 37, 4, NULL, 55]) AS x;
+------+------+
| x | min |
+------+------+
| NULL | NULL |
| NULL | NULL |
| 8 | 4 |
| 4 | 4 |
| 37 | 37 |
| 55 | 37 |
+------+------+
STRING_AGG
STRING_AGG([DISTINCT] expression [, delimiter] [ORDER BY key [{ASC|DESC}] [, ... ]] [LIMIT n])
[OVER (...)]
Description
Returns a value (either STRING or BYTES) obtained by concatenating non-null values.
If a delimiter is specified, concatenated values are separated by that
delimiter; otherwise, a comma is used as a delimiter.
Supported Argument Types
STRING BYTES
Optional Clauses
The clauses are applied in the following order:
OVER: Specifies a window. See Analytic Functions. This clause is currently incompatible with all other clauses withinSTRING_AGG().DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.ORDER BY: Specifies the order of the values.- For each sort key, the default sort direction is
ASC. - NULLs: In the context of the
ORDER BYclause, NULLs are the minimum possible value; that is, NULLs appear first inASCsorts and last inDESCsorts. - Floating point data types: see Floating Point Semantics on ordering and grouping.
- If
DISTINCTis also specified, then the sort key must be the same asexpression. - If
ORDER BYis not specified, the order of the elements in the output array is non-deterministic, which means you might receive a different result each time you use this function.
- For each sort key, the default sort direction is
LIMIT: Specifies the maximum number ofexpressioninputs in the result. The limit applies to the number of input strings, not the number of characters or bytes in the inputs. An empty string counts as 1. A NULL string is not counted. The limitnmust be a constant INT64.
Return Data Types
STRING BYTES
Examples
SELECT STRING_AGG(fruit) AS string_agg
FROM UNNEST(["apple", NULL, "pear", "banana", "pear"]) AS fruit;
+------------------------+
| string_agg |
+------------------------+
| apple,pear,banana,pear |
+------------------------+
SELECT STRING_AGG(fruit, " & ") AS string_agg
FROM UNNEST(["apple", "pear", "banana", "pear"]) AS fruit;
+------------------------------+
| string_agg |
+------------------------------+
| apple & pear & banana & pear |
+------------------------------+
SELECT STRING_AGG(DISTINCT fruit, " & ") AS string_agg
FROM UNNEST(["apple", "pear", "banana", "pear"]) AS fruit;
+-----------------------+
| string_agg |
+-----------------------+
| apple & pear & banana |
+-----------------------+
SELECT STRING_AGG(fruit, " & " ORDER BY LENGTH(fruit)) AS string_agg
FROM UNNEST(["apple", "pear", "banana", "pear"]) AS fruit;
+------------------------------+
| string_agg |
+------------------------------+
| pear & pear & apple & banana |
+------------------------------+
SELECT STRING_AGG(fruit, " & " LIMIT 2) AS string_agg
FROM UNNEST(["apple", "pear", "banana", "pear"]) AS fruit;
+--------------+
| string_agg |
+--------------+
| apple & pear |
+--------------+
SELECT STRING_AGG(DISTINCT fruit, " & " ORDER BY fruit DESC LIMIT 2) AS string_agg
FROM UNNEST(["apple", "pear", "banana", "pear"]) AS fruit;
+---------------+
| string_agg |
+---------------+
| pear & banana |
+---------------+
SELECT
fruit,
STRING_AGG(fruit, " & ") OVER (ORDER BY LENGTH(fruit)) AS string_agg
FROM UNNEST(["apple", NULL, "pear", "banana", "pear"]) AS fruit;
+--------+------------------------------+
| fruit | string_agg |
+--------+------------------------------+
| NULL | NULL |
| pear | pear & pear |
| pear | pear & pear |
| apple | pear & pear & apple |
| banana | pear & pear & apple & banana |
+--------+------------------------------+
SUM
SUM([DISTINCT] expression) [OVER (...)]
Description
Returns the sum of non-null values.
If the expression is a floating point value, the sum is non-deterministic, which means you might receive a different result each time you use this function.
Supported Argument Types
Any supported numeric data types.
Optional Clauses
The clauses are applied in the following order:
OVER: Specifies a window. See Analytic Functions.DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.
Return Data Types
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
Special cases:
Returns NULL if the input contains only NULLs.
Returns NULL if the input contains no rows.
Returns Inf if the input contains Inf.
Returns -Inf if the input contains -Inf.
Returns NaN if the input contains a NaN.
Returns NaN if the input contains a combination of Inf and -Inf.
Examples
SELECT SUM(x) AS sum
FROM UNNEST([1, 2, 3, 4, 5, 4, 3, 2, 1]) AS x;
+-----+
| sum |
+-----+
| 25 |
+-----+
SELECT SUM(DISTINCT x) AS sum
FROM UNNEST([1, 2, 3, 4, 5, 4, 3, 2, 1]) AS x;
+-----+
| sum |
+-----+
| 15 |
+-----+
SELECT
x,
SUM(x) OVER (PARTITION BY MOD(x, 3)) AS sum
FROM UNNEST([1, 2, 3, 4, 5, 4, 3, 2, 1]) AS x;
+---+-----+
| x | sum |
+---+-----+
| 3 | 6 |
| 3 | 6 |
| 1 | 10 |
| 4 | 10 |
| 4 | 10 |
| 1 | 10 |
| 2 | 9 |
| 5 | 9 |
| 2 | 9 |
+---+-----+
SELECT
x,
SUM(DISTINCT x) OVER (PARTITION BY MOD(x, 3)) AS sum
FROM UNNEST([1, 2, 3, 4, 5, 4, 3, 2, 1]) AS x;
+---+-----+
| x | sum |
+---+-----+
| 3 | 3 |
| 3 | 3 |
| 1 | 5 |
| 4 | 5 |
| 4 | 5 |
| 1 | 5 |
| 2 | 7 |
| 5 | 7 |
| 2 | 7 |
+---+-----+
SELECT SUM(x) AS sum
FROM UNNEST([]) AS x;
+------+
| sum |
+------+
| NULL |
+------+
Statistical aggregate functions
BigQuery supports the following statistical aggregate functions.
CORR
CORR(X1, X2) [OVER (...)]
Description
Returns the Pearson coefficient
of correlation of a set of number pairs. For each number pair, the first number
is the dependent variable and the second number is the independent variable.
The return result is between -1 and 1. A result of 0 indicates no
correlation.
All numeric types are supported. If the
input is NUMERIC or BIGNUMERIC then the internal aggregation is
stable with the final output converted to a FLOAT64.
Otherwise the input is converted to a FLOAT64
before aggregation, resulting in a potentially unstable result.
This function ignores any input pairs that contain one or more NULL values. If there are fewer than two input pairs without NULL values, this function returns NULL.
Optional Clause
OVER: Specifies a window. See
Analytic Functions.
Return Data Type
FLOAT64
COVAR_POP
COVAR_POP(X1, X2) [OVER (...)]
Description
Returns the population covariance of
a set of number pairs. The first number is the dependent variable; the second
number is the independent variable. The return result is between -Inf and
+Inf.
All numeric types are supported. If the
input is NUMERIC or BIGNUMERIC then the internal aggregation is
stable with the final output converted to a FLOAT64.
Otherwise the input is converted to a FLOAT64
before aggregation, resulting in a potentially unstable result.
This function ignores any input pairs that contain one or more NULL values. If there is no input pair without NULL values, this function returns NULL. If there is exactly one input pair without NULL values, this function returns 0.
Optional Clause
OVER: Specifies a window. See
Analytic Functions.
Return Data Type
FLOAT64
COVAR_SAMP
COVAR_SAMP(X1, X2) [OVER (...)]
Description
Returns the sample covariance of a
set of number pairs. The first number is the dependent variable; the second
number is the independent variable. The return result is between -Inf and
+Inf.
All numeric types are supported. If the
input is NUMERIC or BIGNUMERIC then the internal aggregation is
stable with the final output converted to a FLOAT64.
Otherwise the input is converted to a FLOAT64
before aggregation, resulting in a potentially unstable result.
This function ignores any input pairs that contain one or more NULL values. If there are fewer than two input pairs without NULL values, this function returns NULL.
Optional Clause
OVER: Specifies a window. See
Analytic Functions.
Return Data Type
FLOAT64
STDDEV_POP
STDDEV_POP([DISTINCT] expression) [OVER (...)]
Description
Returns the population (biased) standard deviation of the values. The return
result is between 0 and +Inf.
All numeric types are supported. If the
input is NUMERIC or BIGNUMERIC then the internal aggregation is
stable with the final output converted to a FLOAT64.
Otherwise the input is converted to a FLOAT64
before aggregation, resulting in a potentially unstable result.
This function ignores any NULL inputs. If all inputs are ignored, this function returns NULL.
If this function receives a single non-NULL input, it returns 0.
Optional Clauses
The clauses are applied in the following order:
OVER: Specifies a window. See Analytic Functions. This clause is currently incompatible with all other clauses withinSTDDEV_POP().DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.
Return Data Type
FLOAT64
STDDEV_SAMP
STDDEV_SAMP([DISTINCT] expression) [OVER (...)]
Description
Returns the sample (unbiased) standard deviation of the values. The return
result is between 0 and +Inf.
All numeric types are supported. If the
input is NUMERIC or BIGNUMERIC then the internal aggregation is
stable with the final output converted to a FLOAT64.
Otherwise the input is converted to a FLOAT64
before aggregation, resulting in a potentially unstable result.
This function ignores any NULL inputs. If there are fewer than two non-NULL inputs, this function returns NULL.
Optional Clauses
The clauses are applied in the following order:
OVER: Specifies a window. See Analytic Functions. This clause is currently incompatible with all other clauses withinSTDDEV_SAMP().DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.
Return Data Type
FLOAT64
STDDEV
STDDEV([DISTINCT] expression) [OVER (...)]
Description
An alias of STDDEV_SAMP.
VAR_POP
VAR_POP([DISTINCT] expression) [OVER (...)]
Description
Returns the population (biased) variance of the values. The return result is
between 0 and +Inf.
All numeric types are supported. If the
input is NUMERIC or BIGNUMERIC then the internal aggregation is
stable with the final output converted to a FLOAT64.
Otherwise the input is converted to a FLOAT64
before aggregation, resulting in a potentially unstable result.
This function ignores any NULL inputs. If all inputs are ignored, this function returns NULL.
If this function receives a single non-NULL input, it returns 0.
Optional Clauses
The clauses are applied in the following order:
OVER: Specifies a window. See Analytic Functions. This clause is currently incompatible with all other clauses withinVAR_POP().DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.
Return Data Type
FLOAT64
VAR_SAMP
VAR_SAMP([DISTINCT] expression) [OVER (...)]
Description
Returns the sample (unbiased) variance of the values. The return result is
between 0 and +Inf.
All numeric types are supported. If the
input is NUMERIC or BIGNUMERIC then the internal aggregation is
stable with the final output converted to a FLOAT64.
Otherwise the input is converted to a FLOAT64
before aggregation, resulting in a potentially unstable result.
This function ignores any NULL inputs. If there are fewer than two non-NULL inputs, this function returns NULL.
Optional Clauses
The clauses are applied in the following order:
OVER: Specifies a window. See Analytic Functions. This clause is currently incompatible with all other clauses withinVAR_SAMP().DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.
Return Data Type
FLOAT64
VARIANCE
VARIANCE([DISTINCT] expression) [OVER (...)]
Description
An alias of VAR_SAMP.
Approximate aggregate functions
Approximate aggregate functions are scalable in terms of memory usage and time,
but produce approximate results instead of exact results. These functions
typically require less memory than exact aggregation functions
like COUNT(DISTINCT ...), but also introduce statistical uncertainty.
This makes approximate aggregation appropriate for large data streams for
which linear memory usage is impractical, as well as for data that is
already approximate.
The approximate aggregate functions in this section work directly on the input data, rather than an intermediate estimation of the data. These functions do not allow users to specify the precision for the estimation with sketches. If you would like specify precision with sketches, see:
- HyperLogLog++ functions to estimate cardinality.
APPROX_COUNT_DISTINCT
APPROX_COUNT_DISTINCT(expression)
Description
Returns the approximate result for COUNT(DISTINCT expression). The value
returned is a statistical estimate—not necessarily the actual value.
This function is less accurate than COUNT(DISTINCT expression), but performs
better on huge input.
Supported Argument Types
Any data type except:
ARRAY
STRUCT
Returned Data Types
INT64
Examples
SELECT APPROX_COUNT_DISTINCT(x) as approx_distinct
FROM UNNEST([0, 1, 1, 2, 3, 5]) as x;
+-----------------+
| approx_distinct |
+-----------------+
| 5 |
+-----------------+
APPROX_QUANTILES
APPROX_QUANTILES([DISTINCT] expression, number [{IGNORE|RESPECT} NULLS])
Description
Returns the approximate boundaries for a group of expression values, where
number represents the number of quantiles to create. This function returns
an array of number + 1 elements, where the first element is the approximate
minimum and the last element is the approximate maximum.
Supported Argument Types
expression can be any supported data type except:
ARRAY
STRUCT
number must be INT64.
Optional Clauses
The clauses are applied in the following order:
DISTINCT: Each distinct value ofexpressionis aggregated only once into the result.IGNORE NULLSorRESPECT NULLS: IfIGNORE NULLSis specified, theNULLvalues are excluded from the result. IfRESPECT NULLSis specified, theNULLvalues are included in the result. If neither is specified, theNULLvalues are excluded from the result. An error is raised if an array in the final query result contains aNULLelement.
Returned Data Types
An ARRAY of the type specified by the expression
parameter.
Returns NULL if there are zero input
rows or expression evaluates to NULL for all rows.
Examples
SELECT APPROX_QUANTILES(x, 2) AS approx_quantiles
FROM UNNEST([1, 1, 1, 4, 5, 6, 7, 8, 9, 10]) AS x;
+------------------+
| approx_quantiles |
+------------------+
| [1, 5, 10] |
+------------------+
SELECT APPROX_QUANTILES(x, 100)[OFFSET(90)] AS percentile_90
FROM UNNEST([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) AS x;
+---------------+
| percentile_90 |
+---------------+
| 9 |
+---------------+
SELECT APPROX_QUANTILES(DISTINCT x, 2) AS approx_quantiles
FROM UNNEST([1, 1, 1, 4, 5, 6, 7, 8, 9, 10]) AS x;
+------------------+
| approx_quantiles |
+------------------+
| [1, 6, 10] |
+------------------+
SELECT FORMAT("%T", APPROX_QUANTILES(x, 2 RESPECT NULLS)) AS approx_quantiles
FROM UNNEST([NULL, NULL, 1, 1, 1, 4, 5, 6, 7, 8, 9, 10]) AS x;
+------------------+
| approx_quantiles |
+------------------+
| [NULL, 4, 10] |
+------------------+
SELECT FORMAT("%T", APPROX_QUANTILES(DISTINCT x, 2 RESPECT NULLS)) AS approx_quantiles
FROM UNNEST([NULL, NULL, 1, 1, 1, 4, 5, 6, 7, 8, 9, 10]) AS x;
+------------------+
| approx_quantiles |
+------------------+
| [NULL, 6, 10] |
+------------------+
APPROX_TOP_COUNT
APPROX_TOP_COUNT(expression, number)
Description
Returns the approximate top elements of expression. The number parameter
specifies the number of elements returned.
Supported Argument Types
expression can be of any data type that the GROUP BY clause supports.
number must be INT64.
Returned Data Types
An ARRAY of type STRUCT.
The STRUCT contains two fields. The first field
(named value) contains an input value. The second field (named count)
contains an INT64 specifying the number of times the
value was returned.
Returns NULL if there are zero input rows.
Examples
SELECT APPROX_TOP_COUNT(x, 2) as approx_top_count
FROM UNNEST(["apple", "apple", "pear", "pear", "pear", "banana"]) as x;
+-------------------------+
| approx_top_count |
+-------------------------+
| [{pear, 3}, {apple, 2}] |
+-------------------------+
NULL handling
APPROX_TOP_COUNT does not ignore NULLs in the input. For example:
SELECT APPROX_TOP_COUNT(x, 2) as approx_top_count
FROM UNNEST([NULL, "pear", "pear", "pear", "apple", NULL]) as x;
+------------------------+
| approx_top_count |
+------------------------+
| [{pear, 3}, {NULL, 2}] |
+------------------------+
APPROX_TOP_SUM
APPROX_TOP_SUM(expression, weight, number)
Description
Returns the approximate top elements of expression, based on the sum of an
assigned weight. The number parameter specifies the number of elements
returned.
If the weight input is negative or NaN, this function returns an error.
Supported Argument Types
expression can be of any data type that the GROUP BY clause supports.
weight must be one of the following:
- INT64
- NUMERIC
- BIGNUMERIC
- FLOAT64
number must be INT64.
Returned Data Types
An ARRAY of type STRUCT.
The STRUCT contains two fields: value and sum.
The value field contains the value of the input expression. The sum field is
the same type as weight, and is the approximate sum of the input weight
associated with the value field.
Returns NULL if there are zero input rows.
Examples
SELECT APPROX_TOP_SUM(x, weight, 2) AS approx_top_sum FROM
UNNEST([
STRUCT("apple" AS x, 3 AS weight),
("pear", 2),
("apple", 0),
("banana", 5),
("pear", 4)
]);
+--------------------------+
| approx_top_sum |
+--------------------------+
| [{pear, 6}, {banana, 5}] |
+--------------------------+
NULL handling
APPROX_TOP_SUM does not ignore NULL values for the expression and weight
parameters.
SELECT APPROX_TOP_SUM(x, weight, 2) AS approx_top_sum FROM
UNNEST([STRUCT("apple" AS x, NULL AS weight), ("pear", 0), ("pear", NULL)]);
+----------------------------+
| approx_top_sum |
+----------------------------+
| [{pear, 0}, {apple, NULL}] |
+----------------------------+
SELECT APPROX_TOP_SUM(x, weight, 2) AS approx_top_sum FROM
UNNEST([STRUCT("apple" AS x, 0 AS weight), (NULL, 2)]);
+-------------------------+
| approx_top_sum |
+-------------------------+
| [{NULL, 2}, {apple, 0}] |
+-------------------------+
SELECT APPROX_TOP_SUM(x, weight, 2) AS approx_top_sum FROM
UNNEST([STRUCT("apple" AS x, 0 AS weight), (NULL, NULL)]);
+----------------------------+
| approx_top_sum |
+----------------------------+
| [{apple, 0}, {NULL, NULL}] |
+----------------------------+
HyperLogLog++ functions
The HyperLogLog++ algorithm (HLL++) estimates cardinality from sketches. If you do not want to work with sketches and do not need customized precision, consider using approximate aggregate functions with system-defined precision.
HLL++ functions are approximate aggregate functions.
Approximate aggregation typically requires less
memory than exact aggregation functions,
like COUNT(DISTINCT), but also introduces statistical uncertainty.
This makes HLL++ functions appropriate for large data streams for
which linear memory usage is impractical, as well as for data that is
already approximate.
BigQuery supports the following HLL++ functions:
HLL_COUNT.INIT
HLL_COUNT.INIT(input [, precision])
Description
An aggregate function that takes one or more input values and aggregates them
into a HLL++ sketch. Each sketch
is represented using the BYTES data type. You can then merge sketches using
HLL_COUNT.MERGE or HLL_COUNT.MERGE_PARTIAL. If no merging is needed,
you can extract the final count of distinct values from the sketch using
HLL_COUNT.EXTRACT.
This function supports an optional parameter, precision. This parameter
defines the accuracy of the estimate at the cost of additional memory required
to process the sketches or store them on disk. The following table shows the
allowed precision values, the maximum sketch size per group, and confidence
interval (CI) of typical precisions:
| Precision | Max. Sketch Size (KiB) | 65% CI | 95% CI | 99% CI |
|---|---|---|---|---|
| 10 | 1 | ±3.25% | ±6.50% | ±9.75% |
| 11 | 2 | ±2.30% | ±4.60% | ±6.89% |
| 12 | 4 | ±1.63% | ±3.25% | ±4.88% |
| 13 | 8 | ±1.15% | ±2.30% | ±3.45% |
| 14 | 16 | ±0.81% | ±1.63% | ±2.44% |
| 15 (default) | 32 | ±0.57% | ±1.15% | ±1.72% |
| 16 | 64 | ±0.41% | ±0.81% | ±1.22% |
| 17 | 128 | ±0.29% | ±0.57% | ±0.86% |
| 18 | 256 | ±0.20% | ±0.41% | ±0.61% |
| 19 | 512 | ±0.14% | ±0.29% | ±0.43% |
| 20 | 1024 | ±0.10% | ±0.20% | ±0.30% |
| 21 | 2048 | ±0.07% | ±0.14% | ±0.22% |
| 22 | 4096 | ±0.05% | ±0.10% | ±0.15% |
| 23 | 8192 | ±0.04% | ±0.07% | ±0.11% |
| 24 | 16384 | ±0.03% | ±0.05% | ±0.08% |
If the input is NULL, this function returns NULL.
For more information, see HyperLogLog in Practice: Algorithmic Engineering of a State of The Art Cardinality Estimation Algorithm.
Supported input types
INT64, NUMERIC, BIGNUMERIC, STRING, BYTES
Return type
BYTES
Example
SELECT
HLL_COUNT.INIT(respondent) AS respondents_hll,
flavor,
country
FROM UNNEST([
STRUCT(1 AS respondent, "Vanilla" AS flavor, "CH" AS country),
(1, "Chocolate", "CH"),
(2, "Chocolate", "US"),
(2, "Strawberry", "US")])
GROUP BY flavor, country;
HLL_COUNT.MERGE
HLL_COUNT.MERGE(sketch)
Description
An aggregate function that returns the cardinality of several HLL++ set sketches by computing their union.
Each sketch must have the same precision and be initialized on the same type.
Attempts to merge sketches with different precisions or for different types
results in an error. For example, you cannot merge a sketch initialized
from INT64 data with one initialized from STRING data.
This function ignores NULL values when merging sketches. If the merge happens
over zero rows or only over NULL values, the function returns 0.
Supported input types
BYTES
Return type
INT64
Example
SELECT HLL_COUNT.MERGE(respondents_hll) AS num_respondents, flavor
FROM (
SELECT
HLL_COUNT.INIT(respondent) AS respondents_hll,
flavor,
country
FROM UNNEST([
STRUCT(1 AS respondent, "Vanilla" AS flavor, "CH" AS country),
(1, "Chocolate", "CH"),
(2, "Chocolate", "US"),
(2, "Strawberry", "US")])
GROUP BY flavor, country)
GROUP BY flavor;
HLL_COUNT.MERGE_PARTIAL
HLL_COUNT.MERGE_PARTIAL(sketch)
Description
An aggregate function that takes one or more
HLL++ sketch
inputs and merges them into a new sketch.
This function returns NULL if there is no input or all inputs are NULL.
Supported input types
BYTES
Return type
BYTES
Example
SELECT HLL_COUNT.MERGE_PARTIAL(respondents_hll) AS num_respondents, flavor
FROM (
SELECT
HLL_COUNT.INIT(respondent) AS respondents_hll,
flavor,
country
FROM UNNEST([
STRUCT(1 AS respondent, "Vanilla" AS flavor, "CH" AS country),
(1, "Chocolate", "CH"),
(2, "Chocolate", "US"),
(2, "Strawberry", "US")])
GROUP BY flavor, country)
GROUP BY flavor;
HLL_COUNT.EXTRACT
HLL_COUNT.EXTRACT(sketch)
Description
A scalar function that extracts a cardinality estimate of a single HLL++ sketch.
If sketch is NULL, this function returns a cardinality estimate of 0.
Supported input types
BYTES
Return type
INT64
Example
SELECT
flavor,
country,
HLL_COUNT.EXTRACT(respondents_hll) AS num_respondents
FROM (
SELECT
HLL_COUNT.INIT(respondent) AS respondents_hll,
flavor,
country
FROM UNNEST([
STRUCT(1 AS respondent, "Vanilla" AS flavor, "CH" AS country),
(1, "Chocolate", "CH"),
(2, "Chocolate", "US"),
(2, "Strawberry", "US")])
GROUP BY flavor, country);
+------------+---------+-----------------+
| flavor | country | num_respondents |
+------------+---------+-----------------+
| Vanilla | CH | 1 |
| Chocolate | CH | 1 |
| Chocolate | US | 1 |
| Strawberry | US | 1 |
+------------+---------+-----------------+
About the HLL++ algorithm
The HLL++ algorithm improves on the HLL algorithm by more accurately estimating very small and large cardinalities. The HLL++ algorithm includes a 64-bit hash function, sparse representation to reduce memory requirements for small cardinality estimates, and empirical bias correction for small cardinality estimates.
About sketches
A sketch is a summary of a large data stream. You can extract statistics from a sketch to estimate particular statistics of the original data, or merge sketches to summarize multiple data streams. A sketch has these features:
- It compresses raw data into a fixed-memory representation.
- It's asymptotically smaller than the input.
- It's the serialized form of an in-memory, sublinear data structure.
- It typically requires less memory than the input used to create it.
Sketches allow integration with other systems. For example, it is possible to
build sketches in external applications, like Cloud Dataflow, or
Apache Spark and consume them in BigQuery or
vice versa. Sketches also allow building intermediate aggregations for
non-additive functions like COUNT(DISTINCT).
Numbering functions
The following sections describe the numbering functions that BigQuery supports. Numbering functions are a subset of analytic functions. For an explanation of how analytic functions work, see Analytic Function Concepts. For a description of how numbering functions work, see the Numbering Function Concepts.
OVER clause requirements:
PARTITION BY: Optional.ORDER BY: Required, except forROW_NUMBER().window_frame_clause: Disallowed.
RANK
Description
Returns the ordinal (1-based) rank of each row within the ordered partition.
All peer rows receive the same rank value. The next row or set of peer rows
receives a rank value which increments by the number of peers with the previous
rank value, instead of DENSE_RANK, which always increments by 1.
Supported Argument Types
INT64
DENSE_RANK
Description
Returns the ordinal (1-based) rank of each row within the window partition. All peer rows receive the same rank value, and the subsequent rank value is incremented by one.
Supported Argument Types
INT64
PERCENT_RANK
Description
Return the percentile rank of a row defined as (RK-1)/(NR-1), where RK is
the RANK of the row and NR is the number of rows in the partition.
Returns 0 if NR=1.
Supported Argument Types
FLOAT64
CUME_DIST
Description
Return the relative rank of a row defined as NP/NR. NP is defined to be the number of rows that either precede or are peers with the current row. NR is the number of rows in the partition.
Supported Argument Types
FLOAT64
NTILE
NTILE(constant_integer_expression)
Description
This function divides the rows into constant_integer_expression
buckets based on row ordering and returns the 1-based bucket number that is
assigned to each row. The number of rows in the buckets can differ by at most 1.
The remainder values (the remainder of number of rows divided by buckets) are
distributed one for each bucket, starting with bucket 1. If
constant_integer_expression evaluates to NULL, 0 or negative, an
error is provided.
Supported Argument Types
INT64
ROW_NUMBER
Description
Does not require the ORDER BY clause. Returns the sequential
row ordinal (1-based) of each row for each ordered partition. If the
ORDER BY clause is unspecified then the result is
non-deterministic.
Supported Argument Types
INT64
Bit functions
BigQuery supports the following bit functions.
BIT_COUNT
BIT_COUNT(expression)
Description
The input, expression, must be an
integer or BYTES.
Returns the number of bits that are set in the input expression.
For signed integers, this is the number of bits in two's complement form.
Return Data Type
INT64
Example
SELECT a, BIT_COUNT(a) AS a_bits, FORMAT("%T", b) as b, BIT_COUNT(b) AS b_bits
FROM UNNEST([
STRUCT(0 AS a, b'' AS b), (0, b'\x00'), (5, b'\x05'), (8, b'\x00\x08'),
(0xFFFF, b'\xFF\xFF'), (-2, b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE'),
(-1, b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'),
(NULL, b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF')
]) AS x;
+-------+--------+---------------------------------------------+--------+
| a | a_bits | b | b_bits |
+-------+--------+---------------------------------------------+--------+
| 0 | 0 | b"" | 0 |
| 0 | 0 | b"\x00" | 0 |
| 5 | 2 | b"\x05" | 2 |
| 8 | 1 | b"\x00\x08" | 1 |
| 65535 | 16 | b"\xff\xff" | 16 |
| -2 | 63 | b"\xff\xff\xff\xff\xff\xff\xff\xfe" | 63 |
| -1 | 64 | b"\xff\xff\xff\xff\xff\xff\xff\xff" | 64 |
| NULL | NULL | b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" | 80 |
+-------+--------+---------------------------------------------+--------+
Conversion functions
BigQuery supports the following conversion functions. These data type conversions are explicit, but some conversions can happen implicitly. You can learn more about implicit and explicit conversion here.
CAST overview
CAST(expression AS typename [format_clause])
Description
Cast syntax is used in a query to indicate that the result type of an expression should be converted to some other type.
When using CAST, a query can fail if BigQuery is unable to perform
the cast. If you want to protect your queries from these types of errors, you
can use SAFE_CAST.
Casts between supported types that do not successfully map from the original value to the target domain produce runtime errors. For example, casting BYTES to STRING where the byte sequence is not valid UTF-8 results in a runtime error.
Some casts can include a format clause, which provides instructions for how to conduct the cast. For example, you could instruct a cast to convert a sequence of bytes to a BASE64-encoded string instead of a UTF-8-encoded string.
The structure of the format clause is unique to each type of cast and more information is available in the section for that cast.
Examples
The following query results in "true" if x is 1, "false" for any other
non-NULL value, and NULL if x is NULL.
CAST(x=1 AS STRING)
CAST AS ARRAY
CAST(expression AS ARRAY<element_type>)
Description
BigQuery supports casting to ARRAY. The expression
parameter can represent an expression for these data types:
ARRAY
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| ARRAY | ARRAY | Must be the exact same ARRAY type. |
CAST AS BIGNUMERIC
CAST(expression AS BIGNUMERIC)
Description
BigQuery supports casting to BIGNUMERIC. The
expression parameter can represent an expression for these data types:
INT64FLOAT64NUMERICBIGNUMERICSTRING
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| FLOAT64 | BIGNUMERIC |
The floating point number will round
half away from zero. Casting a NaN, +inf or
-inf will return an error. Casting a value outside the range
of
BIGNUMERIC
will return an overflow error.
|
| STRING | BIGNUMERIC |
The numeric literal contained in the STRING must not exceed
the maximum precision or range of the
BIGNUMERIC
type, or an error will occur. If the number of digits
after the decimal point exceeds 38, then the resulting
BIGNUMERIC value will round
half away from zero to have 38 digits after the decimal point.
|
CAST AS BOOL
CAST(expression AS BOOL)
Description
BigQuery supports casting to BOOL. The
expression parameter can represent an expression for these data types:
INT64BOOLSTRING
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| INT64 | BOOL |
Returns FALSE if x is 0,
TRUE otherwise.
|
| STRING | BOOL |
Returns TRUE if x is "true" and
FALSE if x is "false"All other values of x are invalid and throw an error instead
of casting to BOOL.STRINGs are case-insensitive when converting to BOOL. |
CAST AS BYTES
CAST(expression AS BYTES [format_clause])
Description
BigQuery supports casting to BYTES. The
expression parameter can represent an expression for these data types:
BYTESSTRING
Format clause
When an expression of one type is cast to another type, you can use the
format clause to provide instructions for how to conduct
the cast. You can use the format clause in this section if expression is a
STRING.
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| STRING | BYTES | STRINGs are cast to BYTES using UTF-8 encoding. For example, the STRING "©", when cast to BYTES, would become a 2-byte sequence with the hex values C2 and A9. |
CAST AS DATE
CAST(expression AS DATE [format_clause])
Description
BigQuery supports casting to DATE. The expression
parameter can represent an expression for these data types:
STRINGTIMEDATETIMETIMESTAMP
Format clause
When an expression of one type is cast to another type, you can use the
format clause to provide instructions for how to conduct
the cast. You can use the format clause in this section if expression is a
STRING.
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| STRING | DATE | When casting from string to date, the string must conform to the supported date literal format, and is independent of time zone. If the string expression is invalid or represents a date that is outside of the supported min/max range, then an error is produced. |
| TIMESTAMP | DATE | Casting from a timestamp to date effectively truncates the timestamp as of the default time zone. |
CAST AS DATETIME
CAST(expression AS DATETIME [format_clause])
Description
BigQuery supports casting to DATETIME. The
expression parameter can represent an expression for these data types:
STRINGTIMEDATETIMETIMESTAMP
Format clause
When an expression of one type is cast to another type, you can use the
format clause to provide instructions for how to conduct
the cast. You can use the format clause in this section if expression is a
STRING.
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| STRING | DATETIME | When casting from string to datetime, the string must conform to the supported datetime literal format, and is independent of time zone. If the string expression is invalid or represents a datetime that is outside of the supported min/max range, then an error is produced. |
| TIMESTAMP | DATETIME | Casting from a timestamp to datetime effectively truncates the timestamp as of the default time zone. |
CAST AS FLOAT64
CAST(expression AS FLOAT64)
Description
BigQuery supports casting to floating point types.
The expression parameter can represent an expression for these data types:
INT64FLOAT64NUMERICBIGNUMERICSTRING
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| INT64 | FLOAT64 | Returns a close but potentially not exact floating point value. |
| NUMERIC | FLOAT64 | NUMERIC will convert to the closest floating point number with a possible loss of precision. |
| BIGNUMERIC | FLOAT64 | BIGNUMERIC will convert to the closest floating point number with a possible loss of precision. |
| STRING | FLOAT64 |
Returns x as a floating point value, interpreting it as
having the same form as a valid floating point literal.
Also supports casts from "[+,-]inf" to
[,-]Infinity,
"[+,-]infinity" to [,-]Infinity, and
"[+,-]nan" to NaN.
Conversions are case-insensitive.
|
CAST AS INT64
CAST(expression AS INT64)
Description
BigQuery supports casting to integer types.
The expression parameter can represent an expression for these data types:
INT64FLOAT64NUMERICBIGNUMERICBOOLSTRING
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| FLOAT64 | INT64 |
Returns the closest integer value. Halfway cases such as 1.5 or -0.5 round away from zero. |
| BOOL | INT64 |
Returns 1 if x is TRUE,
0 otherwise.
|
| STRING | INT64 |
A hex string can be cast to an integer. For example,
0x123 to 291 or -0x123 to
-291.
|
Examples
If you are working with hex strings (0x123), you can cast those strings as
integers:
SELECT '0x123' as hex_value, CAST('0x123' as INT64) as hex_to_int;
+-----------+------------+
| hex_value | hex_to_int |
+-----------+------------+
| 0x123 | 291 |
+-----------+------------+
SELECT '-0x123' as hex_value, CAST('-0x123' as INT64) as hex_to_int;
+-----------+------------+
| hex_value | hex_to_int |
+-----------+------------+
| -0x123 | -291 |
+-----------+------------+
CAST AS NUMERIC
CAST(expression AS NUMERIC)
Description
BigQuery supports casting to NUMERIC. The
expression parameter can represent an expression for these data types:
INT64FLOAT64NUMERICBIGNUMERICSTRING
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| FLOAT64 | NUMERIC |
The floating point number will round
half away from zero. Casting a NaN, +inf or
-inf will return an error. Casting a value outside the range
of
NUMERIC
will return an overflow error.
|
| STRING | NUMERIC |
The numeric literal contained in the STRING must not exceed
the maximum precision or range of the
NUMERIC
type, or an error will occur. If the number of digits
after the decimal point exceeds nine, then the resulting
NUMERIC value will round
half away from zero to have nine digits after the decimal point.
|
CAST AS STRING
CAST(expression AS STRING [format_clause [AT TIME ZONE timezone_expr]])
Description
BigQuery supports casting to STRING. The
expression parameter can represent an expression for these data types:
INT64FLOAT64NUMERICBIGNUMERICBOOLBYTESTIMEDATEDATETIMETIMESTAMPSTRING
Format clause
When an expression of one type is cast to another type, you can use the
format clause to provide instructions for how to conduct the cast.
You can use the format clause in this section if expression is one of these
data types:
INT64FLOAT64NUMERICBIGNUMERICBYTESTIMEDATEDATETIMETIMESTAMP
The format clause for STRING has an additional optional clause called
AT TIME ZONE timezone_expr, which you can use to specify a specific time zone
to use during formatting of a TIMESTAMP. If this optional clause is not
included when formatting a TIMESTAMP, your current time zone is used.
For more information, see the following topics:
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| FLOAT64 | STRING | Returns an approximate string representation. |
| BOOL | STRING |
Returns "true" if x is TRUE,
"false" otherwise. |
| BYTES | STRING |
Returns x interpreted as a UTF-8 STRING.For example, the BYTES literal b'\xc2\xa9', when cast to STRING,
is interpreted as UTF-8 and becomes the unicode character "©".An error occurs if x is not valid UTF-8. |
| TIME | STRING |
Casting from a time type to a string is independent of time zone and
is of the form HH:MM:SS.
|
| DATE | STRING |
Casting from a date type to a string is independent of time zone and is
of the form YYYY-MM-DD.
|
| DATETIME | STRING |
Casting from a datetime type to a string is independent of time zone and
is of the form YYYY-MM-DD HH:MM:SS.
|
| TIMESTAMP | STRING | When casting from timestamp types to string, the timestamp is interpreted using the default time zone, UTC. The number of subsecond digits produced depends on the number of trailing zeroes in the subsecond part: the CAST function will truncate zero, three, or six digits. |
Examples
SELECT CAST(CURRENT_DATE() AS STRING) AS current_date
+---------------+
| current_date |
+---------------+
| 2021-03-09 |
+---------------+
SELECT CAST(CURRENT_DATE() AS STRING FORMAT 'DAY') AS current_day
+-------------+
| current_day |
+-------------+
| MONDAY |
+-------------+
SELECT CAST(
TIMESTAMP '2008-12-25 00:00:00+00:00'
AS STRING FORMAT 'YYYY-MM-DD HH24:MI:SS TZH:TZM') AS date_time_to_string
-- Results depend upon where this query was executed.
+------------------------------+
| date_time_to_string |
+------------------------------+
| 2008-12-24 16:00:00 -08:00 |
+------------------------------+
SELECT CAST(
TIMESTAMP '2008-12-25 00:00:00+00:00'
AS STRING FORMAT 'YYYY-MM-DD HH24:MI:SS TZH:TZM'
AT TIME ZONE 'Asia/Kolkata') AS date_time_to_string
-- Because the time zone is specified, the result is always the same.
+------------------------------+
| date_time_to_string |
+------------------------------+
| 2008-12-25 05:30:00 +05:30 |
+------------------------------+
CAST AS STRUCT
CAST(expression AS STRUCT)
Description
BigQuery supports casting to STRUCT. The expression
parameter can represent an expression for these data types:
STRUCT
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| STRUCT | STRUCT |
Allowed if the following conditions are met:
|
CAST AS TIME
CAST(expression AS TIME [format_clause])
Description
BigQuery supports casting to TIME. The expression
parameter can represent an expression for these data types:
STRINGTIMEDATETIMETIMESTAMP
Format clause
When an expression of one type is cast to another type, you can use the
format clause to provide instructions for how to conduct
the cast. You can use the format clause in this section if expression is a
STRING.
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| STRING | TIME | When casting from string to time, the string must conform to the supported time literal format, and is independent of time zone. If the string expression is invalid or represents a time that is outside of the supported min/max range, then an error is produced. |
CAST AS TIMESTAMP
CAST(expression AS TIMESTAMP [format_clause [AT TIME ZONE timezone_expr]])
Description
BigQuery supports casting to TIMESTAMP. The
expression parameter can represent an expression for these data types:
STRINGTIMEDATETIMETIMESTAMP
Format clause
When an expression of one type is cast to another type, you can use the
format clause to provide instructions for how to conduct
the cast. You can use the format clause in this section if expression is a
STRING.
The format clause for TIMESTAMP has an additional optional clause called
AT TIME ZONE timezone_expr, which you can use to specify a specific time zone
to use during formatting. If this optional clause is not included, your
current time zone is used.
Conversion rules
| From | To | Rule(s) when casting x |
|---|---|---|
| STRING | TIMESTAMP |
When casting from string to a timestamp, string_expression
must conform to the supported timestamp literal formats, or else a runtime
error occurs. The string_expression may itself contain a
time zone.
If there is a time zone in the string_expression, that
time zone is used for conversion, otherwise the default time zone,
UTC, is used. If the string has fewer than six digits,
then it is implicitly widened.
An error is produced if the string_expression is invalid,
has more than six subsecond digits (i.e. precision greater than
microseconds), or represents a time outside of the supported timestamp
range.
|
| DATE | TIMESTAMP |
Casting from a date to a timestamp interprets date_expression
as of midnight (start of the day) in the default time zone,
UTC.
|
| DATETIME | TIMESTAMP |
Casting from a datetime to a timestamp interprets
datetime_expression as of midnight (start of the day) in the
default time zone, UTC.
|
Examples
The following example casts a string-formatted timestamp as a timestamp:
SELECT CAST("2020-06-02 17:00:53.110+00:00" AS TIMESTAMP) AS as_timestamp
-- Results depend upon where this query was executed.
+-----------------------------+
| as_timestamp |
+-----------------------------+
| 2020-06-03 00:00:53.110 UTC |
+-----------------------------+
The following examples cast a string-formatted date and time as a timestamp. These examples return the same output as the previous example.
SELECT CAST("06/02/2020 17:00:53.110" AS TIMESTAMP FORMAT 'MM/DD/YYYY HH:MI:SS' AT TIME ZONE 'America/Los_Angeles') AS as_timestamp
SELECT CAST("06/02/2020 17:00:53.110" AS TIMESTAMP FORMAT 'MM/DD/YYYY HH:MI:SS' AT TIME ZONE '00') AS as_timestamp
SELECT CAST('06/02/2020 17:00:53.110 +00' AS TIMESTAMP FORMAT 'YYYY-MM-DD HH:MI:SS TZH') AS as_timestamp
PARSE_BIGNUMERIC
PARSE_BIGNUMERIC(string_expression)
Description
Converts a string to a BIGNUMERIC value.
The numeric literal contained in the string must not exceed the maximum
precision or range of the BIGNUMERIC type, or an error
occurs. If the number of digits after the decimal point exceeds 38, then the
resulting BIGNUMERIC value rounds
half away from zero to have 38 digits after the
decimal point.
-- This example shows how a string with a decimal point is parsed.
SELECT PARSE_BIGNUMERIC("123.45") AS parsed
+--------+
| parsed |
+--------+
| 123.45 |
+--------+
-- This example shows how a string with an exponent is parsed.
SELECT PARSE_BIGNUMERIC("123.456E37") AS parsed
+-----------------------------------------+
| parsed |
+-----------------------------------------+
| 123400000000000000000000000000000000000 |
+-----------------------------------------+
-- This example shows the rounding when digits after the decimal point exceeds 38.
SELECT PARSE_BIGNUMERIC("1.123456789012345678901234567890123456789") as parsed
+------------------------------------------+
| parsed |
+------------------------------------------+
| 1.12345678901234567890123456789012345679 |
+------------------------------------------+
This funcion is similar to using the CAST AS BIGNUMERIC
function except that the PARSE_BIGNUMERIC function only accepts string inputs
and allows the following in the string:
- Spaces between the sign (+/-) and the number
- Signs (+/-) after the number
Rules for valid input strings:
| Rule | Example Input | Output |
|---|---|---|
| The string can only contain digits, commas, decimal points and signs. | "- 12,34567,89.0" | -123456789 |
| Whitepaces are allowed anywhere except between digits. | " - 12.345 " | -12.345 |
| Only digits and commas are allowed before the decimal point. | " 12,345,678" | 12345678 |
| Only digits are allowed after the decimal point. | "1.234 " | 1.234 |
Use E or e for exponents. After the e,
digits and a leading sign indicator are allowed.
|
" 123.45e-1" | 12.345 |
| If the integer part is not empty, then it must contain at least one digit. | " 0,.12 -" | -0.12 |
| If the string contains a decimal point, then it must contain at least one digit. | " .1" | 0.1 |
| The string cannot contain more than one sign. | " 0.5 +" | 0.5 |
Return Data Type
BIGNUMERIC
Examples
This example shows an input with spaces before, after, and between the sign and the number:
SELECT PARSE_BIGNUMERIC(" - 12.34 ") as parsed;
+--------+
| parsed |
+--------+
| -12.34 |
+--------+
This example shows an input with an exponent as well as the sign after the number:
SELECT PARSE_BIGNUMERIC("12.34e-1-") as parsed;
+--------+
| parsed |
+--------+
| -1.234 |
+--------+
This example shows an input with multiple commas in the integer part of the number:
SELECT PARSE_BIGNUMERIC(" 1,2,,3,.45 + ") as parsed;
+--------+
| parsed |
+--------+
| 123.45 |
+--------+
This example shows an input with a decimal point and no digits in the whole number part:
SELECT PARSE_BIGNUMERIC(".1234 ") as parsed;
+--------+
| parsed |
+--------+
| 0.1234 |
+--------+
Examples of invalid inputs
This example is invalid because the whole number part contains no digits:
SELECT PARSE_BIGNUMERIC(",,,.1234 ") as parsed;
This example is invalid because there are whitespaces between digits:
SELECT PARSE_BIGNUMERIC("1 23.4 5 ") as parsed;
This example is invalid because the number is empty except for an exponent:
SELECT PARSE_BIGNUMERIC(" e1 ") as parsed;
This example is invalid because the string contains multiple signs:
SELECT PARSE_BIGNUMERIC(" - 12.3 - ") as parsed;
This example is invalid because the value of the number falls outside the range
of BIGNUMERIC:
SELECT PARSE_BIGNUMERIC("12.34E100 ") as parsed;
This example is invalid because the string contains invalid characters:
SELECT PARSE_BIGNUMERIC("$12.34") as parsed;
PARSE_NUMERIC
PARSE_NUMERIC(string_expression)
Description
Converts a string to a NUMERIC value.
The numeric literal contained in the string must not exceed the maximum
precision or range of the NUMERIC type, or an error
occurs. If the number of digits after the decimal point exceeds nine, then the
resulting NUMERIC value rounds
half away from zero to have nine digits after the
decimal point.
-- This example shows how a string with a decimal point is parsed.
SELECT PARSE_NUMERIC("123.45") AS parsed
+--------+
| parsed |
+--------+
| 123.45 |
+--------+
-- This example shows how a string with an exponent is parsed.
SELECT PARSE_NUMERIC("12.34E27") as parsed
+-------------------------------+
| parsed |
+-------------------------------+
| 12340000000000000000000000000 |
+-------------------------------+
-- This example shows the rounding when digits after the decimal point exceeds 9.
SELECT PARSE_NUMERIC("1.0123456789") as parsed
+-------------+
| parsed |
+-------------+
| 1.012345679 |
+-------------+
This function is similar to using the CAST AS NUMERIC function
except that the PARSE_NUMERIC function only accepts string inputs and allows
the following in the string:
- Spaces between the sign (+/-) and the number
- Signs (+/-) after the number
Rules for valid input strings:
| Rule | Example Input | Output |
|---|---|---|
| The string can only contain digits, commas, decimal points and signs. | "- 12,34567,89.0" | -123456789 |
| Whitepaces are allowed anywhere except between digits. | " - 12.345 " | -12.345 |
| Only digits and commas are allowed before the decimal point. | " 12,345,678" | 12345678 |
| Only digits are allowed after the decimal point. | "1.234 " | 1.234 |
Use E or e for exponents. After the e,
digits and a leading sign indicator are allowed.
|
" 123.45e-1" | 12.345 |
| If the integer part is not empty, then it must contain at least one digit. | " 0,.12 -" | -0.12 |
| If the string contains a decimal point, then it must contain at least one digit. | " .1" | 0.1 |
| The string cannot contain more than one sign. | " 0.5 +" | 0.5 |
Return Data Type
NUMERIC
Examples
This example shows an input with spaces before, after, and between the sign and the number:
SELECT PARSE_NUMERIC(" - 12.34 ") as parsed;
+--------+
| parsed |
+--------+
| -12.34 |
+--------+
This example shows an input with an exponent as well as the sign after the number:
SELECT PARSE_NUMERIC("12.34e-1-") as parsed;
+--------+
| parsed |
+--------+
| -1.234 |
+--------+
This example shows an input with multiple commas in the integer part of the number:
SELECT PARSE_NUMERIC(" 1,2,,3,.45 + ") as parsed;
+--------+
| parsed |
+--------+
| 123.45 |
+--------+
This example shows an input with a decimal point and no digits in the whole number part:
SELECT PARSE_NUMERIC(".1234 ") as parsed;
+--------+
| parsed |
+--------+
| 0.1234 |
+--------+
Examples of invalid inputs
This example is invalid because the whole number part contains no digits:
SELECT PARSE_NUMERIC(",,,.1234 ") as parsed;
This example is invalid because there are whitespaces between digits:
SELECT PARSE_NUMERIC("1 23.4 5 ") as parsed;
This example is invalid because the number is empty except for an exponent:
SELECT PARSE_NUMERIC(" e1 ") as parsed;
This example is invalid because the string contains multiple signs:
SELECT PARSE_NUMERIC(" - 12.3 - ") as parsed;
This example is invalid because the value of the number falls outside the range
of BIGNUMERIC:
SELECT PARSE_NUMERIC("12.34E100 ") as parsed;
This example is invalid because the string contains invalid characters:
SELECT PARSE_NUMERIC("$12.34") as parsed;
SAFE_CAST
SAFE_CAST(expression AS typename [format_clause])
Description
When using CAST, a query can fail if BigQuery is unable to perform
the cast. For example, the following query generates an error:
SELECT CAST("apple" AS INT64) AS not_a_number;
If you want to protect your queries from these types of errors, you can use
SAFE_CAST. SAFE_CAST is identical to CAST, except it returns NULL
instead of raising an error.
SELECT SAFE_CAST("apple" AS INT64) AS not_a_number;
+--------------+
| not_a_number |
+--------------+
| NULL |
+--------------+
If you are casting from bytes to strings, you can also use the
function, SAFE_CONVERT_BYTES_TO_STRING. Any invalid UTF-8 characters are
replaced with the unicode replacement character, U+FFFD. See
SAFE_CONVERT_BYTES_TO_STRING for more
information.
Other conversion functions
You can learn more about these conversion functions elsewhere in the documentation:
| Conversion function | From | To |
|---|---|---|
| ARRAY_TO_STRING | ARRAY | STRING |
| DATE | Various data types | DATE |
| DATETIME | Various data types | DATETIME |
| FROM_BASE32 | STRING | BYTEs |
| FROM_BASE64 | STRING | BYTES |
| FROM_HEX | STRING | BYTES |
| PARSE_DATE | STRING | DATE |
| PARSE_DATETIME | STRING | DATETIME |
| PARSE_TIME | STRING | TIME |
| PARSE_TIMESTAMP | STRING | TIMESTAMP |
| SAFE_CONVERT_BYTES_TO_STRING | BYTES | STRING |
| STRING | TIMESTAMP | STRING |
| TIME | Various data types | TIME |
| TIMESTAMP | Various data types | TIMESTAMP |
| TO_BASE32 | BYTES | STRING |
| TO_BASE64 | BYTES | STRING |
| TO_HEX | BYTES | STRING |
Format clause for CAST
format_clause:
FORMAT format_model
format_model:
format_string_expression
The format clause can be used in some CAST functions. You use a format clause
to provide instructions for how to conduct a
cast. For example, you could
instruct a cast to convert a sequence of bytes to a BASE64-encoded string
instead of a UTF-8-encoded string.
The format clause includes a format model. The format model can contain format elements combined together as a format string.
Format bytes as string
CAST(bytes_expression AS STRING FORMAT format_string_expression)
You can cast a sequence of bytes to a string with a format element in the
format string. If the bytes cannot be formatted with a
format element, an error is returned. If the sequence of bytes is NULL, the
result is NULL. Format elements are case-insensitive.
| Format element | Returns | Example |
|---|---|---|
| HEX | Converts a sequence of bytes into a hexadecimal string. |
Input: b'\x00\x01\xEF\xFF' Output: 0001efff |
| BASEX | Converts a sequence of bytes into a BASEX encoded string. X represents one of these numbers: 2, 8, 16, 32, 64. |
Input as BASE8: b'\x02\x11\x3B' Output: 00410473 |
| BASE64M | Converts a sequence of bytes into a BASE64-encoded string based on rfc 2045 for MIME. Generates a newline character ("\n") every 76 characters. |
Input: b'\xde\xad\xbe\xef' Output: 3q2+7w== |
| ASCII | Converts a sequence of bytes that are ASCII values to a string. If the input contains bytes that are not a valid ASCII encoding, an error is returned. |
Input: b'\x48\x65\x6c\x6c\x6f' Output: Hello |
| UTF-8 | Converts a sequence of bytes that are UTF-8 values to a string. If the input contains bytes that are not a valid UTF-8 encoding, an error is returned. |
Input: b'\x24' Output: $ |
| UTF8 | Same behavior as UTF-8. |
Return type
STRING
Example
SELECT CAST(b'\x48\x65\x6c\x6c\x6f' AS STRING FORMAT 'ASCII') AS bytes_to_string;
+-----------------+
| bytes_to_string |
+-----------------+
| Hello |
+-----------------+
Format string as bytes
CAST(string_expression AS BYTES FORMAT format_string_expression)
You can cast a string to bytes with a format element in the format string. If the string cannot be formatted with the format element, an error is returned. Format elements are case-insensitive.
In the string expression, whitespace characters, such as \n, are ignored
if the BASE64 or BASE64M format element is used.
| Format element | Returns | Example |
|---|---|---|
| HEX | Converts a hexadecimal-encoded string to bytes. If the input contains characters that are not part of the HEX encoding alphabet (0~9, case-insensitive a~f), an error is returned. |
Input: '0001efff' Output: b'\x00\x01\xEF\xFF' |
| BASEX | Converts a BASEX-encoded string to bytes. X represents one of these numbers: 2, 8, 16, 32, 64. An error is returned if the input contains characters that are not part of the BASEX encoding alphabet, except whitespace characters if the format element is BASE64. |
Input as BASE8: '00410473' Output: b'\x02\x11\x3B' |
| BASE64M | Converts a BASE64-encoded string to bytes. If the input contains characters that are not whitespace and not part of the BASE64 encoding alphabet defined at rfc 2045, an error is returned. BASE64M and BASE64 decoding have the same behavior. |
Input: '3q2+7w==' Output: b'\xde\xad\xbe\xef' |
| ASCII | Converts a string with only ASCII characters to bytes. If the input contains characters that are not ASCII characters, an error is returned. |
Input: 'Hello' Output: b'\x48\x65\x6c\x6c\x6f' |
| UTF-8 | Converts a string to a sequence of UTF-8 bytes. |
Input: '$' Output: b'\x24' |
| UTF8 | Same behavior as UTF-8. |
Return type
BYTES
Example
SELECT CAST('Hello' AS BYTES FORMAT 'ASCII') AS string_to_bytes
+-------------------------+
| string_to_bytes |
+-------------------------+
| b'\x48\x65\x6c\x6c\x6f' |
+-------------------------+
Format date and time as string
You can format these date and time parts as a string:
- Format year part as string
- Format month part as string
- Format day part as string
- Format hour part as string
- Format minute part as string
- Format second part as string
- Format meridian indicator as string
- Format time zone as string
- Format literal as string
Case matching is supported when you format some date or time parts as a string and the output contains letters. To learn more, see Case matching.
Case matching
When the output of some format element contains letters, the letter cases of the output is matched with the letter cases of the format element, meaning the words in the output are capitalized according to how the format element is capitalized. This is called case matching. The rules are:
- If the first two letters of the element are both upper case, the words in
the output are capitalized. For example
DAY=THURSDAY. - If the first letter of the element is upper case, and the second letter is
lowercase, the first letter of each word in the output is capitalized and
other letters are lowercase. For example
Day=Thursday. - If the first letter of the element is lowercase, then all letters in the
output are lowercase. For example,
day=thursday.
Format year part as string
CAST(expression AS STRING FORMAT format_string_expression)
Casts a data type that contains the year part to a string. Includes format elements, which provide instructions for how to conduct the cast.
expression: This expression contains the data type with the year that you need to format.format_string_expression: A string which contains format elements, including the year format element.
These data types include a year part:
DATEDATETIMETIMESTAMP
If expression or format_string_expression is NULL the return value is
NULL. If format_string_expression is an empty string, the output is an
empty string. An error is generated if a value that is not a supported
format element appears in format_string_expression or expression does not
contain a value specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| YYYY | Year, 4 or more digits. |
Input: DATE '2018-01-30' Output: 2018 Input: DATE '76-01-30' Output: 0076 Input: DATE '10000-01-30' Output: 10000 |
| YYY | Year, last 3 digits only. |
Input: DATE '2018-01-30' Output: 018 Input: DATE '98-01-30' Output: 098 |
| YY | Year, last 2 digits only. |
Input: DATE '2018-01-30' Output: 18 Input: DATE '8-01-30' Output: 08 |
| Y | Year, last digit only. |
Input: DATE '2018-01-30' Output: 8 |
| RRRR | Same behavior as YYYY. | |
| RR | Same behavior as YY. |
Return type
STRING
Example
SELECT CAST(DATE '2018-01-30' AS STRING FORMAT 'YYYY') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| 2018 |
+---------------------+
Format month part as string
CAST(expression AS STRING FORMAT format_string_expression)
Casts a data type that contains the month part to a string. Includes format elements, which provide instructions for how to conduct the cast.
expression: This expression contains the data type with the month that you need to format.format_string_expression: A string which contains format elements, including the month format element.
These data types include a month part:
DATEDATETIMETIMESTAMP
If expression or format_string_expression is NULL the return value is
NULL. If format_string_expression is an empty string, the output is an
empty string. An error is generated if a value that is not a supported
format element appears in format_string_expression or expression does not
contain a value specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| MM | Month, 2 digits. |
Input: DATE '2018-01-30' Output: 01 |
| MON | Abbreviated, 3-character name of the month. The abbreviated month names for locale en-US are: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC. Case matching is supported. |
Input: DATE '2018-01-30' Output: JAN |
| MONTH | Name of the month. Case matching is supported. |
Input: DATE '2018-01-30' Output: JANUARY |
Return type
STRING
Example
SELECT CAST(DATE '2018-01-30' AS STRING FORMAT 'MONTH') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| JANUARY |
+---------------------+
Format day part as string
CAST(expression AS STRING FORMAT format_string_expression)
Casts a data type that contains the day part to a string. Includes format elements, which provide instructions for how to conduct the cast.
expression: This expression contains the data type with the day that you need to format.format_string_expression: A string which contains format elements, including the day format element.
These data types include a day part:
DATEDATETIMETIMESTAMP
If expression or format_string_expression is NULL the return value is
NULL. If format_string_expression is an empty string, the output is an
empty string. An error is generated if a value that is not a supported
format element appears in format_string_expression or expression does not
contain a value specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| DAY | Name of the day of the week, localized. Spaces are padded on the right side to make the output size exactly 9. Case matching is supported. |
Input: DATE '2020-12-31' Output: THURSDAY |
| DY | Abbreviated, 3-character name of the weekday, localized. The abbreviated weekday names for locale en-US are: MON, TUE, WED, THU, FRI, SAT, SUN. Case matching is supported. |
Input: DATE '2020-12-31' Output: THU |
| D | Day of the week (1 to 7), starting with Sunday as 1. |
Input: DATE '2020-12-31' Output: 4 |
| DD | 2-digit day of the month. |
Input: DATE '2018-12-02' Output: 02 |
| DDD | 3-digit day of the year. |
Input: DATE '2018-02-03' Output: 034 |
Return type
STRING
Example
SELECT CAST(DATE '2018-02-15' AS STRING FORMAT 'DD') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| 15 |
+---------------------+
Format hour part as string
CAST(expression AS STRING FORMAT format_string_expression)
Casts a data type that contains the hour part to a string. Includes format elements, which provide instructions for how to conduct the cast.
expression: This expression contains the data type with the hour that you need to format.format_string_expression: A string which contains format elements, including the hour format element.
These data types include a hour part:
TIMEDATETIMETIMESTAMP
If expression or format_string_expression is NULL the return value is
NULL. If format_string_expression is an empty string, the output is an
empty string. An error is generated if a value that is not a supported
format element appears in format_string_expression or expression does not
contain a value specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| HH | Hour of the day, 12-hour clock, 2 digits. |
Input: TIME '21:30:00' Output: 09 |
| HH12 | Hour of the day, 12-hour clock. |
Input: TIME '21:30:00' Output: 09 |
| HH24 | Hour of the day, 24-hour clock, 2 digits. |
Input: TIME '21:30:00' Output: 21 |
Return type
STRING
Examples
SELECT CAST(TIME '21:30:00' AS STRING FORMAT 'HH24') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| 21 |
+---------------------+
SELECT CAST(TIME '21:30:00' AS STRING FORMAT 'HH12') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| 09 |
+---------------------+
Format minute part as string
CAST(expression AS STRING FORMAT format_string_expression)
Casts a data type that contains the minute part to a string. Includes format elements, which provide instructions for how to conduct the cast.
expression: This expression contains the data type with the minute that you need to format.format_string_expression: A string which contains format elements, including the minute format element.
These data types include a minute part:
TIMEDATETIMETIMESTAMP
If expression or format_string_expression is NULL the return value is
NULL. If format_string_expression is an empty string, the output is an
empty string. An error is generated if a value that is not a supported
format element appears in format_string_expression or expression does not
contain a value specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| MI | Minute, 2 digits. |
Input: TIME '01:02:03' Output: 02 |
Return type
STRING
Example
SELECT CAST(TIME '21:30:00' AS STRING FORMAT 'MI') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| 30 |
+---------------------+
Format second part as string
CAST(expression AS STRING FORMAT format_string_expression)
Casts a data type that contains the second part to a string. Includes format elements, which provide instructions for how to conduct the cast.
expression: This expression contains the data type with the second that you need to format.format_string_expression: A string which contains format elements, including the second format element.
These data types include a second part:
TIMEDATETIMETIMESTAMP
If expression or format_string_expression is NULL the return value is
NULL. If format_string_expression is an empty string, the output is an
empty string. An error is generated if a value that is not a supported
format element appears in format_string_expression or expression does not
contain a value specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| SS | Seconds of the minute, 2 digits. |
Input: TIME '01:02:03' Output: 03 |
| SSSSS | Seconds of the day, 5 digits. |
Input: TIME '01:02:03' Output: 03723 |
| FFn |
Fractional part of the second, n digits long.
Replace n with a value from 1 to 9. For example, FF5.
The fractional part of the second is rounded
to fit the size of the output.
|
Input for FF1: TIME '01:05:07.16' Output: 1 Input for FF2: TIME '01:05:07.16' Output: 16 Input for FF3: TIME '01:05:07.16' Output: 016 |
Return type
STRING
Examples
SELECT CAST(TIME '21:30:25.16' AS STRING FORMAT 'SS') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| 25 |
+---------------------+
SELECT CAST(TIME '21:30:25.16' AS STRING FORMAT 'FF2') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| 16 |
+---------------------+
Format meridian indicator part as string
CAST(expression AS STRING FORMAT format_string_expression)
Casts a data type that contains the meridian indicator part to a string. Includes format elements, which provide instructions for how to conduct the cast.
expression: This expression contains the data type with the meridian indicator that you need to format.format_string_expression: A string which contains format elements, including the meridian indicator format element.
These data types include a meridian indicator part:
TIMEDATETIMETIMESTAMP
If expression or format_string_expression is NULL the return value is
NULL. If format_string_expression is an empty string, the output is an
empty string. An error is generated if a value that is not a supported
format element appears in format_string_expression or expression does not
contain a value specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| A.M. | A.M. if the time is less than 12, otherwise P.M. The letter case of the output is determined by the first letter case of the format element. |
Input for A.M.: TIME '01:02:03' Output: A.M. Input for A.M.: TIME '16:02:03' Output: P.M. Input for a.m.: TIME '01:02:03' Output: a.m. Input for a.M.: TIME '01:02:03' Output: a.m. |
| AM | AM if the time is less than 12, otherwise PM. The letter case of the output is determined by the first letter case of the format element. |
Input for AM: TIME '01:02:03' Output: AM Input for AM: TIME '16:02:03' Output: PM Input for am: TIME '01:02:03' Output: am Input for aM: TIME '01:02:03' Output: am |
| P.M. | Output is the same as A.M. format element. | |
| PM | Output is the same as AM format element. |
Return type
STRING
Examples
SELECT CAST(TIME '21:30:00' AS STRING FORMAT 'AM') AS date_time_to_string;
SELECT CAST(TIME '21:30:00' AS STRING FORMAT 'PM') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| PM |
+---------------------+
SELECT CAST(TIME '01:30:00' AS STRING FORMAT 'AM') AS date_time_to_string;
SELECT CAST(TIME '01:30:00' AS STRING FORMAT 'PM') AS date_time_to_string;
+---------------------+
| date_time_to_string |
+---------------------+
| AM |
+---------------------+
Format time zone part as string
CAST(expression AS STRING FORMAT format_string_expression)
Casts a data type that contains the time zone part to a string. Includes format elements, which provide instructions for how to conduct the cast.
expression: This expression contains the data type with the time zone that you need to format.format_string_expression: A string which contains format elements, including the time zone format element.
These data types include a time zone part:
DATETIMEDATETIMETIMESTAMP
If expression or format_string_expression is NULL the return value is
NULL. If format_string_expression is an empty string, the output is an
empty string. An error is generated if a value that is not a supported
format element appears in format_string_expression or expression does not
contain a value specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| TZH |
Hour offset for a time zone. This includes the +/- sign and
2-digit hour.
|
Inputstamp: TIMESTAMP '2008-12-25 05:30:00+00' Output: −08 |
| TZM | Minute offset for a time zone. This includes only the 2-digit minute. | Inputstamp: TIMESTAMP '2008-12-25 05:30:00+00' Output: 00 |
Return type
STRING
Examples
SELECT CAST(TIMESTAMP '2008-12-25 00:00:00+00:00' AS STRING FORMAT 'TZH') AS date_time_to_string;
-- Results depend upon where this query was executed.
+---------------------+
| date_time_to_string |
+---------------------+
| -08 |
+---------------------+
SELECT CAST(TIMESTAMP '2008-12-25 00:00:00+00:00' AS STRING FORMAT 'TZH' AT TIME ZONE 'Asia/Kolkata')
AS date_time_to_string;
-- Because the time zone is specified, the result is always the same.
+---------------------+
| date_time_to_string |
+---------------------+
| +05 |
+---------------------+
SELECT CAST(TIMESTAMP '2008-12-25 00:00:00+00:00' AS STRING FORMAT 'TZM') AS date_time_to_string;
-- Results depend upon where this query was executed.
+---------------------+
| date_time_to_string |
+---------------------+
| 00 |
+---------------------+
SELECT CAST(TIMESTAMP '2008-12-25 00:00:00+00:00' AS STRING FORMAT 'TZM' AT TIME ZONE 'Asia/Kolkata')
AS date_time_to_string;
-- Because the time zone is specified, the result is always the same.
+---------------------+
| date_time_to_string |
+---------------------+
| 30 |
+---------------------+
Format literal as string
CAST(expression AS STRING FORMAT format_string_expression)
| Format element | Returns | Example |
|---|---|---|
| - | Output is the same as the input. | - |
| . | Output is the same as the input. | . |
| / | Output is the same as the input. | / |
| , | Output is the same as the input. | , |
| ' | Output is the same as the input. | ' |
| ; | Output is the same as the input. | ; |
| : | Output is the same as the input. | : |
| Whitespace | Output is the same as the input. Whitespace means the space character, ASCII 32. It does not mean other types of space like tab or new line. Any whitespace character that is not the ASCII 32 character in the format model generates an error. | |
| "text" |
Output is the value within the double quotes. To preserve a double
quote or backslash character, use the \" or \\
escape sequence. Other escape sequences are not supported.
|
Input: "abc" Output: abc Input: "a\"b\\c" Output: a"b\c |
Format string as date and time
You can format a string with these date and time parts:
- Format string as year part
- Format string as month part
- Format string as day part
- Format string as hour part
- Format string as minute part
- Format string as second part
- Format string as meridian indicator part
- Format string as time zone part
- Format string as literal part
When formatting a string with date and time parts, you must follow the format model rules.
Format model rules
When casting a string to date and time parts, you must ensure the format model
is valid. The format model represents the elements passed into
CAST(string_expression AS type FORMAT format_string_expression) as the
format_string_expression and is validated according to the following
rules:
- It contains at most one of each of the following parts: meridian indicator, year, month, day, hour.
- A non-literal, non-whitespace format element cannot appear more than once.
- If it contains the day of year format element,
DDD, then it cannot contain the month. - If it contains the 24-hour format element,
HH24, then it cannot contain the 12-hour format element or a meridian indicator. - If it contains the 12-hour format element,
HH12orHH, then it must also contain a meridian indicator. - If it contains a meridian indicator, then it must also contain a 12-hour format element.
- If it contains the second of the day format element,
SSSSS, then it cannot contain any of the following: hour, minute, second, or meridian indicator. - It cannot contain a format element such that the value it sets does not exist
in the target type. For example, an hour format element such as
HH24cannot appear in a string you are casting as aDATE.
Format string as year part
CAST(string_expression AS type FORMAT format_string_expression)
Casts a string-formatted year to a data type that contains the year part. Includes format elements, which provide instructions for how to conduct the cast.
string_expression: This expression contains the string with the year that you need to format.type: The data type to which you are casting. Must include the year part.format_string_expression: A string which contains format elements, including the year format element. The formats elements in this string are defined collectively as the format model, which must follow these rules.
These data types include a year part:
DATEDATETIMETIMESTAMP
If the YEAR part is missing from string_expression and the return type
includes this part, YEAR is set to the current year.
An error is generated if a value that is not a supported format element appears
in format_string_expression or string_expression does not contain a value
specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| YYYY | If it is delimited, matches 1 to 5 digits. If it is not delimited, matches 4 digits. Sets the year part to the matched number. |
Input for MM-DD-YYYY: '03-12-2018' Output as DATE: 2018-12-03 Input for YYYY-MMDD: '10000-1203' Output as DATE: 10000-12-03 Input for YYYY: '18' Output as DATE: 2018-03-01 (Assume current date is March 23, 2021) |
| YYY | Matches 3 digits. Sets the last 3 digits of the year part to the matched number. |
Input for YYY-MM-DD: '018-12-03' Output as DATE: 2018-12-03 Input for YYY-MM-DD: '038-12-03' Output as DATE: 2038-12-03 |
| YY | Matches 2 digits. Sets the last 2 digits of the year part to the matched number. |
Input for YY-MM-DD: '18-12-03' Output as DATE: 2018-12-03 Input for YY-MM-DD: '38-12-03' Output as DATE: 2038-12-03 |
| Y | Matches 1 digit. Sets the last digit of the year part to the matched number. |
Input for Y-MM-DD: '8-12-03' Output as DATE: 2008-12-03 |
| Y,YYY | Matches the pattern of 1 to 2 digits, comma, then exactly 3 digits. Sets the year part to the matched number. |
Input for Y,YYY-MM-DD: '2,018-12-03' Output as DATE: 2008-12-03 |
| RRRR | Same behavior as YYYY. | |
| RR |
Matches 2 digits. If the 2 digits entered are between 00 and 49 and the last 2 digits of the current year are between 00 and 49, the returned year has the same first 2 digits as the current year. If the last 2 digits of the current year are between 50 and 99, the first 2 digits of the returned year is 1 greater than the first 2 digits of the current year. If the 2 digits entered are between 50 and 99 and the last 2 digits of the current year are between 00 and 49, the first 2 digits of the returned year are 1 less than the first 2 digits of the current year. If the last 2 digits of the current year are between 50 and 99, the returned year has the same first 2 digits as the current year. |
Input for RR-MM-DD: '18-12-03' Output as DATE: 2018-12-03 (executed in the year 2021) Output as DATE: 2118-12-03 (executed in the year 2050) Input for RR-MM-DD: '50-12-03' Output as DATE: 2050-12-03 (executed in the year 2021) Output as DATE: 2050-12-03 (executed in the year 2050) |
Return type
The data type to which the string was cast. This can be:
DATEDATETIMETIMESTAMP
Examples
SELECT CAST('18-12-03' AS DATE FORMAT 'YY-MM-DD') AS string_to_date_time
+---------------------+
| string_to_date_time |
+---------------------+
| 2018-02-03 |
+---------------------+
Format string as month part
CAST(string_expression AS type FORMAT format_string_expression)
Casts a string-formatted month to a data type that contains the month part. Includes format elements, which provide instructions for how to conduct the cast.
string_expression: This expression contains the string with the month that you need to format.type: The data type to which you are casting. Must include the month part.format_string_expression: A string which contains format elements, including the month format element. The formats elements in this string are defined collectively as the format model, which must follow these rules.
These data types include a month part:
DATEDATETIMETIMESTAMP
If the MONTH part is missing from string_expression and the return type
includes this part, MONTH is set to the current month.
An error is generated if a value that is not a supported format element appears
in format_string_expression or string_expression does not contain a value
specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| MM | Matches 2 digits. Sets the month part to the matched number. |
Input for MM-DD-YYYY: '03-12-2018' Output as DATE: 2018-12-03 |
| MON | Matches 3 letters. Sets the month part to the matched string interpreted as the abbreviated name of the month. |
Input for MON DD, YYYY: 'DEC 03, 2018' Output as DATE: 2018-12-03 |
| MONTH | Matches 9 letters. Sets the month part to the matched string interpreted as the name of the month. |
Input for MONTH DD, YYYY: 'DECEMBER 03, 2018' Output as DATE: 2018-12-03 |
Return type
The data type to which the string was cast. This can be:
DATEDATETIMETIMESTAMP
Examples
SELECT CAST('DEC 03, 2018' AS DATE FORMAT 'MON DD, YYYY') AS string_to_date_time
+---------------------+
| string_to_date_time |
+---------------------+
| 2018-12-03 |
+---------------------+
Format string as day part
CAST(string_expression AS type FORMAT format_string_expression)
Casts a string-formatted day to a data type that contains the day part. Includes format elements, which provide instructions for how to conduct the cast.
string_expression: This expression contains the string with the day that you need to format.type: The data type to which you are casting. Must include the day part.format_string_expression: A string which contains format elements, including the day format element. The formats elements in this string are defined collectively as the format model, which must follow these rules.
These data types include a day part:
DATEDATETIMETIMESTAMP
If the DAY part is missing from string_expression and the return type
includes this part, DAY is set to 1.
An error is generated if a value that is not a supported format element appears
in format_string_expression or string_expression does not contain a value
specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| DD | Matches 2 digits. Sets the day part to the matched number. |
Input for MONTH DD, YYYY: 'DECEMBER 03, 2018' Output as DATE: 2018-12-03 |
Return type
The data type to which the string was cast. This can be:
DATEDATETIMETIMESTAMP
Examples
SELECT CAST('DECEMBER 03, 2018' AS DATE FORMAT 'MONTH DD, YYYY') AS string_to_date_time
+---------------------+
| string_to_date_time |
+---------------------+
| 2018-12-03 |
+---------------------+
Format string as hour part
CAST(string_expression AS type FORMAT format_string_expression)
Casts a string-formatted hour to a data type that contains the hour part. Includes format elements, which provide instructions for how to conduct the cast.
string_expression: This expression contains the string with the hour that you need to format.type: The data type to which you are casting. Must include the hour part.format_string_expression: A string which contains format elements, including the hour format element. The formats elements in this string are defined collectively as the format model, which must follow these rules.
These data types include a hour part:
TIMEDATETIMETIMESTAMP
If the HOUR part is missing from string_expression and the return type
includes this part, HOUR is set to 0.
An error is generated if a value that is not a supported format element appears
in format_string_expression or string_expression does not contain a value
specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| HH |
Matches 2 digits. If the matched number n is 12,
sets temp = 0; otherwise, sets temp = n. If
the matched value of the A.M./P.M. format element is P.M., sets
temp = n + 12. Sets the hour part to temp.
A meridian indicator must be present in the format model, when
HH is present.
|
Input for HH:MI P.M.: '03:30 P.M.' Output as TIME: 15:30:00 |
| HH12 | Same behavior as HH. | |
| HH24 | Matches 2 digits. Sets the hour part to the matched number. |
Input for HH24:MI: '15:30' Output as TIME: 15:30:00 |
Return type
The data type to which the string was cast. This can be:
TIMEDATETIMETIMESTAMP
Examples
SELECT CAST('15:30' AS TIME FORMAT 'HH24:MI') AS string_to_date_time
+---------------------+
| string_to_date_time |
+---------------------+
| 15:30:00 |
+---------------------+
Format string as minute part
CAST(string_expression AS type FORMAT format_string_expression)
Casts a string-formatted minute to a data type that contains the minute part. Includes format elements, which provide instructions for how to conduct the cast.
string_expression: This expression contains the string with the minute that you need to format.type: The data type to which you are casting. Must include the minute part.format_string_expression: A string which contains format elements, including the minute format element. The formats elements in this string are defined collectively as the format model, which must follow these rules.
These data types include a minute part:
TIMEDATETIMETIMESTAMP
If the MINUTE part is missing from string_expression and the return type
includes this part, MINUTE is set to 0.
An error is generated if a value that is not a supported format element appears
in format_string_expression or string_expression does not contain a value
specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| MI | Matches 2 digits. Sets the minute part to the matched number. |
Input for HH:MI P.M.: '03:30 P.M.' Output as TIME: 15:30:00 |
Return type
The data type to which the string was cast. This can be:
TIMEDATETIMETIMESTAMP
Examples
SELECT CAST('03:30 P.M.' AS TIME FORMAT 'HH:MI P.M.') AS string_to_date_time
+---------------------+
| string_to_date_time |
+---------------------+
| 15:30:00 |
+---------------------+
Format string as second part
CAST(string_expression AS type FORMAT format_string_expression)
Casts a string-formatted second to a data type that contains the second part. Includes format elements, which provide instructions for how to conduct the cast.
string_expression: This expression contains the string with the second that you need to format.type: The data type to which you are casting. Must include the second part.format_string_expression: A string which contains format elements, including the second format element. The formats elements in this string are defined collectively as the format model, which must follow these rules.
These data types include a second part:
TIMEDATETIMETIMESTAMP
If the SECOND part is missing from string_expression and the return type
includes this part, SECOND is set to 0.
An error is generated if a value that is not a supported format element appears
in format_string_expression or string_expression does not contain a value
specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| SS | Matches 2 digits. Sets the second part to the matched number. |
Input for HH:MI:SS P.M.: '03:30:02 P.M.' Output as TIME: 15:30:02 |
| SSSSS | Matches 5 digits. Sets the hour, minute and second parts by interpreting the matched number as the number of seconds past midnight. |
Input for SSSSS: '03723' Output as TIME: 01:02:03 |
| FFn |
Matches n digits, where n is the number
following FF in the format element. Sets the fractional part of the
second part to the matched number.
|
Input for HH24:MI:SS.FF1: '01:05:07.16' Output as TIME: 01:05:07.2 Input for HH24:MI:SS.FF2: '01:05:07.16' Output as TIME: 01:05:07.16 Input for HH24:MI:SS.FF3: 'FF3: 01:05:07.16' Output as TIME: 01:05:07.160 |
Return type
The data type to which the string was cast. This can be:
TIMEDATETIMETIMESTAMP
Examples
SELECT CAST('01:05:07.16' AS TIME FORMAT 'HH24:MI:SS.FF1') AS string_to_date_time
+---------------------+
| string_to_date_time |
+---------------------+
| 01:05:07.2 |
+---------------------+
Format string as meridian indicator part
CAST(string_expression AS type FORMAT format_string_expression)
Casts a string-formatted meridian indicator to a data type that contains the meridian indicator part. Includes format elements, which provide instructions for how to conduct the cast.
string_expression: This expression contains the string with the meridian indicator that you need to format.type: The data type to which you are casting. Must include the meridian indicator part.format_string_expression: A string which contains format elements, including the meridian indicator format element. The formats elements in this string are defined collectively as the format model, which must follow these rules.
These data types include a meridian indicator part:
TIMEDATETIMETIMESTAMP
An error is generated if a value that is not a supported format element appears
in format_string_expression or string_expression does not contain a value
specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| A.M. or P.M. |
Matches using the regular expression '(A|P)\.M\.'.
|
Input for HH:MI A.M.: '03:30 A.M.' Output as TIME: 03:30:00 Input for HH:MI P.M.: '03:30 P.M.' Output as TIME: 15:30:00 Input for HH:MI P.M.: '03:30 A.M.' Output as TIME: 03:30:00 Input for HH:MI A.M.: '03:30 P.M.' Output as TIME: 15:30:00 Input for HH:MI a.m.: '03:30 a.m.' Output as TIME: 03:30:00 |
Return type
The data type to which the string was cast. This can be:
TIMEDATETIMETIMESTAMP
Examples
SELECT CAST('03:30 P.M.' AS TIME FORMAT 'HH:MI A.M.') AS string_to_date_time
+---------------------+
| string_to_date_time |
+---------------------+
| 15:30:00 |
+---------------------+
Format string as time zone part
CAST(string_expression AS type FORMAT format_string_expression)
Casts a string-formatted time zone to a data type that contains the time zone part. Includes format elements, which provide instructions for how to conduct the cast.
string_expression: This expression contains the string with the time zone that you need to format.type: The data type to which you are casting. Must include the time zone part.format_string_expression: A string which contains format elements, including the time zone format element. The formats elements in this string are defined collectively as the format model, which must follow these rules.
These data types include a time zone part:
DATETIMEDATETIMETIMESTAMP
An error is generated if a value that is not a supported format element appears
in format_string_expression or string_expression does not contain a value
specified by a format element.
| Format element | Returns | Example |
|---|---|---|
| TZH |
Matches using the regular expression '(\+|\-| )[0-9]{2}'.
Sets the time zone and hour parts to the matched sign and number.
Sets the time zone sign to be the first letter of the matched string.
The number 2 means matching up to 2 digits for non-exact matching, and
exactly 2 digits for exact matching.
|
Input for YYYY-MM-DD HH:MI:SSTZH: '2008-12-25 05:30:00-08' Output as TIMESTAMP: 2008-12-25 05:30:00-08 |
| TZM |
Matches 2 digits. Let n be the matched number. If the
time zone sign is the minus sign, sets the time zone minute part to
-n. Otherwise, sets the time zone minute part to
n.
|
Input for YYYY-MM-DD HH:MI:SSTZH: '2008-12-25 05:30:00+05.30' Output as TIMESTAMP: 2008-12-25 05:30:00+05.30 |
Return type
The data type to which the string was cast. This can be:
DATETIMEDATETIMETIMESTAMP
Examples
SELECT CAST('2020.06.03 00:00:53+00' AS TIMESTAMP FORMAT 'YYYY.MM.DD HH:MI:SSTZH') AS string_to_date_time
+-----------------------------+
| as_timestamp |
+-----------------------------+
| 2020-06-03 00:00:53.110 UTC |
+-----------------------------+
Format string as literal
CAST(string_expression AS data_type FORMAT format_string_expression)
| Format element | Returns | Example |
|---|---|---|
| - | Output is the same as the input. | |
| . | Output is the same as the input. | . |
| / | Output is the same as the input. | / |
| , | Output is the same as the input. | , |
| ' | Output is the same as the input. | ' |
| ; | Output is the same as the input. | ; |
| : | Output is the same as the input. | : |
| Whitespace | A consecutive sequence of one or more spaces in the format model is matched with one or more consecutive Unicode whitespace characters in the input. Space means the ASCII 32 space character. It does not mean the general whitespace such as a tab or new line. Any whitespace character that is not the ASCII 32 character in the format model generates an error. | |
| "text" |
Output generated by the format element in formatting, using this
regular expression, with s representing the string input:
regex.escape(s).
|
Input: "abc" Output: abc Input: "a\"b\\c" Output: a"b\c |
Format numeric type as string
CAST(numeric_expression AS STRING FORMAT format_string_expression)
You can cast a numeric type to a string by combining the following format elements:
Except for the exponent format element (EEEE), all of the format elements
generate a fixed number of characters in the output, and the output is aligned
by the decimal point. To suppress blank characters and trailing zeroes, use the
FM flag.
Return type
STRING
Example
SELECT input, CAST(input AS STRING FORMAT '$999,999.999') AS output
FROM UNNEST([1.2, 12.3, 123.456, 1234.56, -12345.678, 1234567.89]) AS input
+------------+---------------+
| input | output |
+------------+---------------+
| 1.2 | $1.200 |
| 12.3 | $12.300 |
| 123.456 | $123.456 |
| 1234.56 | $1,234.560 |
| -12345.678 | -$12,345.678 |
| 1234567.89 | $###,###.### |
+------------+---------------+
Format digits as string
The following format elements output digits. If there aren't enough digit format
elements to represent the input, all digit format elements are replaced with #
in the output.
| Format element | Returns | Example |
|---|---|---|
| 0 | A decimal digit. Leading and trailing zeros are included. |
Input: 12 Format: '000' Output: ' 012'
Input: 12 Format: '000.000' Output: ' 012.000'
Input: -12 Format: '000.000' Output: '-012.000'
|
| 9 | A decimal digit. Leading zeros are replaced with spaces. Trailing zeros are included. |
Input: 12 Format: '999' Output: ' 12'
Input: 12 Format: '999.999' Output: ' 12.000'
|
| X or x | A hexadecimal digit. Cannot appear with other format elements except 0, FM, and the sign format elements. The maximum number of hexadecimal digits in the format string is 16. X generates uppercase letters and x generates lowercase letters. When 0 is combined with the hexadecimal format element, the letter generated by 0 matches the case of the next X or x element. If there is no subsequent X or x, then 0 generates an uppercase letter. |
Input: 43981 Format: 'XXXX' Output: ' ABCD'
Input: 43981 Format: 'xxxx' Output: ' abcd'
Input: 43981 Format: '0X0x' Output: ' ABcd'
Input: 43981 Format: '0000000X' Output: ' 0000ABCD'
|
Return type
STRING
Example
SELECT
CAST(12 AS STRING FORMAT '999') as a,
CAST(-12 AS STRING FORMAT '999') as b;
+------+------+
| a | b |
+------+------+
| 12 | -12 |
+------+------+
Format decimal point as string
The following format elements output a decimal point. These format elements are mutually exclusive. At most one can appear in the format string.
| Format element | Returns | Example |
|---|---|---|
| . (period) | Decimal point. |
Input: 123.58 Format: '999.999' Output: ' 123.580'
|
| D | The decimal point of the current locale. |
Input: 123.58 Format: '999D999' Output: ' 123.580'
|
Return type
STRING
Example
SELECT CAST(12.5 AS STRING FORMAT '99.99') as a;
+--------+
| a |
+--------+
| 12.50 |
+--------+
Format sign as string
The following format elements output the sign (+/-). These format elements are mutually exclusive. At most one can appear in the format string.
If there are no sign format elements, one extra space is reserved for the sign.
For example, if the input is 12 and the format string is
'99', then the output is ' 12', with a length of three
characters.
The sign appears before the number. If the format model includes a currency symbol element, then the sign appears before the currency symbol.
| Format element | Returns | Example |
|---|---|---|
| S | Explicit sign. Outputs + for positive numbers and
- for negative numbers. The position in the output is
anchored to the number. |
Input: -12 Format: 'S9999' Output: ' -12'
Input: -12 Format: '9999S' Output: ' 12-'
|
| MI | Explicit sign. Outputs a space for positive numbers and -
for negative numbers. This element can only appear in the last position.
|
Input: 12 Format: '9999MI' Output: ' 12 '
Input: -12 Format: '9999MI' Output: ' 12-'
|
| PR | For negative numbers, the value is enclosed in angle brackets. For positive numbers, the value is returned with a leading and trailing space. This element can only appear in the last position. |
Input: 12 Format: '9999PR' Output: ' 12 '
Input: -12 Format: '9999PR' Output: ' <12>'
|
Return type
STRING
Example
SELECT
CAST(12 AS STRING FORMAT 'S99') as a,
CAST(-12 AS STRING FORMAT 'S99') as b;
+-----+-----+
| a | b |
+-----+-----+
| +12 | -12 |
+-----+-----+
Format currency symbol as string
The following format elements output a currency symbol. These format elements are mutually exclusive. At most one can appear in the format string. In the output, the currency symbol appears before the first digit or decimal point.
| Format element | Returns | Example |
|---|---|---|
| $ | Dollar sign ($). |
Input: -12 Format: '$999' Output: ' -$12'
|
| C or c | The ISO-4217 currency code of the current locale. |
Input: -12 Format: 'C999' Output: ' -USD12'
Input: -12 Format: 'c999' Output: ' -usd12'
|
| L | The currency symbol of the current locale. |
Input: -12 Format: 'L999' Output: ' -$12'
|
Return type
STRING
Example
SELECT
CAST(12 AS STRING FORMAT '$99') as a,
CAST(-12 AS STRING FORMAT '$99') as b;
+------+------+
| a | b |
+------+------+
| $12 | -$12 |
+------+------+
Format group separator as string
The following format elements output a group separator.
| Format element | Returns | Example |
|---|---|---|
| , (comma) | Group separator. |
Input: 12345 Format: '999,999' Output: ' 12,345'
|
| G | The group separator point of the current locale. |
Input: 12345 Format: '999G999' Output: ' 12,345'
|
Return type
STRING
Example
SELECT CAST(1234 AS STRING FORMAT '999,999') as a;
+----------+
| a |
+----------+
| 1,234 |
+----------+
Other numeric format elements
| Format element | Returns | Example |
|---|---|---|
| B | Outputs spaces when the integer part is zero. If the integer part of the number is 0, then the following format elements generate spaces in the output: digits (9, X, 0), decimal point, group separator, currency, sign, and exponent. |
Input: 0.23 Format: 'B99.999S' Output: ' '
Input: 1.23 Format: 'B99.999S' Output: ' 1.230+'
|
| EEEE | Outputs the exponent part of the value in scientific notation. If the exponent value is between -99 and 99, the output is four characters. Otherwise, the minimum number of digits is used in the output. |
Input: 20 Format: '9.99EEEE' Output: ' 2.0E+01'
Input: 299792458 Format: 'S9.999EEEE' Output: '+2.998E+08'
|
| FM | Removes all spaces and trailing zeroes from the output. You can use this element to suppress spaces and trailing zeroes that are generated by other format elements. |
Input: 12.5 Format: '999999.000FM' Output: '12.5'
|
| RN | Returns the value as Roman numerals, rounded to the nearest integer.
The input must be between 1 and 3999. The output is padded with spaces
to the left to a length of 15. This element cannot be used with other
format elements except FM.
|
Input: 2021 Format: 'RN' Output: ' MMXXI'
|
| V | The input value is multiplied by 10^n, where n is the number of 9s
after the V. This element cannot be used with a decimal
point or exponent format element.
|
Input: 23.5 Format: 'S000V00' Output: '+02350'
|
Return type
STRING
Example
SELECT CAST(-123456 AS STRING FORMAT '9.999EEEE') as a;"
+------------+
| a |
+------------+
| -1.235E+05 |
+------------+
About BASE encoding
BASE encoding translates binary data in string format into a radix-X representation.
If X is 2, 8, or 16, Arabic numerals 0–9 and the Latin letters a–z are used in the encoded string. So for example, BASE16/Hexadecimal encoding results contain 0~9 and a~f).
If X is 32 or 64, the default character tables are defined in rfc 4648. When you decode a BASE string where X is 2, 8, or 16, the Latin letters in the input string are case-insensitive. For example, both "3a" and "3A" are valid input strings for BASE16/Hexadecimal decoding, and will output the same result.
Mathematical functions
All mathematical functions have the following behaviors:
- They return
NULLif any of the input parameters isNULL. - They return
NaNif any of the arguments isNaN.
ABS
ABS(X)
Description
Computes absolute value. Returns an error if the argument is an integer and the output value cannot be represented as the same type; this happens only for the largest negative input value, which has no positive representation.
| X | ABS(X) |
|---|---|
| 25 | 25 |
| -25 | 25 |
+inf |
+inf |
-inf |
+inf |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
SIGN
SIGN(X)
Description
Returns -1, 0, or +1 for negative, zero and positive arguments
respectively. For floating point arguments, this function does not distinguish
between positive and negative zero.
| X | SIGN(X) |
|---|---|
| 25 | +1 |
| 0 | 0 |
| -25 | -1 |
| NaN | NaN |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
IS_INF
IS_INF(X)
Description
Returns TRUE if the value is positive or negative infinity.
| X | IS_INF(X) |
|---|---|
+inf |
TRUE |
-inf |
TRUE |
| 25 | FALSE |
IS_NAN
IS_NAN(X)
Description
Returns TRUE if the value is a NaN value.
| X | IS_NAN(X) |
|---|---|
NaN |
TRUE |
| 25 | FALSE |
IEEE_DIVIDE
IEEE_DIVIDE(X, Y)
Description
Divides X by Y; this function never fails. Returns
FLOAT64. Unlike the division operator (/),
this function does not generate errors for division by zero or overflow.
| X | Y | IEEE_DIVIDE(X, Y) |
|---|---|---|
| 20.0 | 4.0 | 5.0 |
| 0.0 | 25.0 | 0.0 |
| 25.0 | 0.0 | +inf |
| -25.0 | 0.0 | -inf |
| 0.0 | 0.0 | NaN |
| 0.0 | NaN |
NaN |
NaN |
0.0 | NaN |
+inf |
+inf |
NaN |
-inf |
-inf |
NaN |
RAND
RAND()
Description
Generates a pseudo-random value of type FLOAT64 in
the range of [0, 1), inclusive of 0 and exclusive of 1.
SQRT
SQRT(X)
Description
Computes the square root of X. Generates an error if X is less than 0.
| X | SQRT(X) |
|---|---|
25.0 |
5.0 |
+inf |
+inf |
X < 0 |
Error |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
POW
POW(X, Y)
Description
Returns the value of X raised to the power of Y. If the result underflows and is not representable, then the function returns a value of zero.
| X | Y | POW(X, Y) |
|---|---|---|
| 2.0 | 3.0 | 8.0 |
| 1.0 | Any value including NaN |
1.0 |
Any value including NaN |
0 | 1.0 |
| -1.0 | +inf |
1.0 |
| -1.0 | -inf |
1.0 |
| ABS(X) < 1 | -inf |
+inf |
| ABS(X) > 1 | -inf |
0.0 |
| ABS(X) < 1 | +inf |
0.0 |
| ABS(X) > 1 | +inf |
+inf |
-inf |
Y < 0 | 0.0 |
-inf |
Y > 0 | -inf if Y is an odd integer, +inf otherwise |
+inf |
Y < 0 | 0 |
+inf |
Y > 0 | +inf |
| Finite value < 0 | Non-integer | Error |
| 0 | Finite value < 0 | Error |
Return Data Type
The return data type is determined by the argument types with the following table.
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| INT64 | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
| FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
POWER
POWER(X, Y)
Description
Synonym of POW(X, Y).
EXP
EXP(X)
Description
Computes e to the power of X, also called the natural exponential function. If the result underflows, this function returns a zero. Generates an error if the result overflows.
| X | EXP(X) |
|---|---|
| 0.0 | 1.0 |
+inf |
+inf |
-inf |
0.0 |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
LN
LN(X)
Description
Computes the natural logarithm of X. Generates an error if X is less than or equal to zero.
| X | LN(X) |
|---|---|
| 1.0 | 0.0 |
+inf |
+inf |
X < 0 |
Error |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
LOG
LOG(X [, Y])
Description
If only X is present, LOG is a synonym of LN. If Y is also present,
LOG computes the logarithm of X to base Y.
| X | Y | LOG(X, Y) |
|---|---|---|
| 100.0 | 10.0 | 2.0 |
-inf |
Any value | NaN |
| Any value | +inf |
NaN |
+inf |
0.0 < Y < 1.0 | -inf |
+inf |
Y > 1.0 | +inf |
| X <= 0 | Any value | Error |
| Any value | Y <= 0 | Error |
| Any value | 1.0 | Error |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| INT64 | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
| FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
LOG10
LOG10(X)
Description
Similar to LOG, but computes logarithm to base 10.
| X | LOG10(X) |
|---|---|
| 100.0 | 2.0 |
-inf |
NaN |
+inf |
NaN |
| X <= 0 | Error |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
GREATEST
GREATEST(X1,...,XN)
Description
Returns the largest value among X1,...,XN according to the < comparison.
If any parts of X1,...,XN are NULL, the return value is NULL.
| X1,...,XN | GREATEST(X1,...,XN) |
|---|---|
| 3,5,1 | 5 |
Return Data Types
Data type of the input values.
LEAST
LEAST(X1,...,XN)
Description
Returns the smallest value among X1,...,XN according to the > comparison.
If any parts of X1,...,XN are NULL, the return value is NULL.
| X1,...,XN | LEAST(X1,...,XN) |
|---|---|
| 3,5,1 | 1 |
Return Data Types
Data type of the input values.
DIV
DIV(X, Y)
Description
Returns the result of integer division of X by Y. Division by zero returns an error. Division by -1 may overflow.
| X | Y | DIV(X, Y) |
|---|---|---|
| 20 | 4 | 5 |
| 0 | 20 | 0 |
| 20 | 0 | Error |
Return Data Type
The return data type is determined by the argument types with the following table.
| INPUT | INT64 | NUMERIC | BIGNUMERIC |
|---|---|---|---|
| INT64 | INT64 | NUMERIC | BIGNUMERIC |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC |
SAFE_DIVIDE
SAFE_DIVIDE(X, Y)
Description
Equivalent to the division operator (X / Y), but returns
NULL if an error occurs, such as a division by zero error.
| X | Y | SAFE_DIVIDE(X, Y) |
|---|---|---|
| 20 | 4 | 5 |
| 0 | 20 | 0 |
| 20 | 0 | NULL |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| INT64 | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
| FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
SAFE_MULTIPLY
SAFE_MULTIPLY(X, Y)
Description
Equivalent to the multiplication operator (*), but returns
NULL if overflow occurs.
| X | Y | SAFE_MULTIPLY(X, Y) |
|---|---|---|
| 20 | 4 | 80 |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| INT64 | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
| FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
SAFE_NEGATE
SAFE_NEGATE(X)
Description
Equivalent to the unary minus operator (-), but returns
NULL if overflow occurs.
| X | SAFE_NEGATE(X) |
|---|---|
| +1 | -1 |
| -1 | +1 |
| 0 | 0 |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
SAFE_ADD
SAFE_ADD(X, Y)
Description
Equivalent to the addition operator (+), but returns
NULL if overflow occurs.
| X | Y | SAFE_ADD(X, Y) |
|---|---|---|
| 5 | 4 | 9 |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| INT64 | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
| FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
SAFE_SUBTRACT
SAFE_SUBTRACT(X, Y)
Description
Returns the result of Y subtracted from X.
Equivalent to the subtraction operator (-), but returns
NULL if overflow occurs.
| X | Y | SAFE_SUBTRACT(X, Y) |
|---|---|---|
| 5 | 4 | 1 |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| INT64 | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
| FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
MOD
MOD(X, Y)
Description
Modulo function: returns the remainder of the division of X by Y. Returned value has the same sign as X. An error is generated if Y is 0.
| X | Y | MOD(X, Y) |
|---|---|---|
| 25 | 12 | 1 |
| 25 | 0 | Error |
Return Data Type
The return data type is determined by the argument types with the following table.
| INPUT | INT64 | NUMERIC | BIGNUMERIC |
|---|---|---|---|
| INT64 | INT64 | NUMERIC | BIGNUMERIC |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC |
ROUND
ROUND(X [, N])
Description
If only X is present, ROUND rounds X to the nearest integer. If N is present,
ROUND rounds X to N decimal places after the decimal point. If N is negative,
ROUND will round off digits to the left of the decimal point. Rounds halfway
cases away from zero. Generates an error if overflow occurs.
| X | ROUND(X) |
|---|---|
| 2.0 | 2.0 |
| 2.3 | 2.0 |
| 2.8 | 3.0 |
| 2.5 | 3.0 |
| -2.3 | -2.0 |
| -2.8 | -3.0 |
| -2.5 | -3.0 |
| 0 | 0 |
+inf |
+inf |
-inf |
-inf |
NaN |
NaN |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
TRUNC
TRUNC(X [, N])
Description
If only X is present, TRUNC rounds X to the nearest integer whose absolute
value is not greater than the absolute value of X. If N is also present, TRUNC
behaves like ROUND(X, N), but always rounds towards zero and never overflows.
| X | TRUNC(X) |
|---|---|
| 2.0 | 2.0 |
| 2.3 | 2.0 |
| 2.8 | 2.0 |
| 2.5 | 2.0 |
| -2.3 | -2.0 |
| -2.8 | -2.0 |
| -2.5 | -2.0 |
| 0 | 0 |
+inf |
+inf |
-inf |
-inf |
NaN |
NaN |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
CEIL
CEIL(X)
Description
Returns the smallest integral value that is not less than X.
| X | CEIL(X) |
|---|---|
| 2.0 | 2.0 |
| 2.3 | 3.0 |
| 2.8 | 3.0 |
| 2.5 | 3.0 |
| -2.3 | -2.0 |
| -2.8 | -2.0 |
| -2.5 | -2.0 |
| 0 | 0 |
+inf |
+inf |
-inf |
-inf |
NaN |
NaN |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
CEILING
CEILING(X)
Description
Synonym of CEIL(X)
FLOOR
FLOOR(X)
Description
Returns the largest integral value that is not greater than X.
| X | FLOOR(X) |
|---|---|
| 2.0 | 2.0 |
| 2.3 | 2.0 |
| 2.8 | 2.0 |
| 2.5 | 2.0 |
| -2.3 | -3.0 |
| -2.8 | -3.0 |
| -2.5 | -3.0 |
| 0 | 0 |
+inf |
+inf |
-inf |
-inf |
NaN |
NaN |
Return Data Type
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
COS
COS(X)
Description
Computes the cosine of X where X is specified in radians. Never fails.
| X | COS(X) |
|---|---|
+inf |
NaN |
-inf |
NaN |
NaN |
NaN |
COSH
COSH(X)
Description
Computes the hyperbolic cosine of X where X is specified in radians. Generates an error if overflow occurs.
| X | COSH(X) |
|---|---|
+inf |
+inf |
-inf |
+inf |
NaN |
NaN |
ACOS
ACOS(X)
Description
Computes the principal value of the inverse cosine of X. The return value is in the range [0,π]. Generates an error if X is a value outside of the range [-1, 1].
| X | ACOS(X) |
|---|---|
+inf |
NaN |
-inf |
NaN |
NaN |
NaN |
| X < -1 | Error |
| X > 1 | Error |
ACOSH
ACOSH(X)
Description
Computes the inverse hyperbolic cosine of X. Generates an error if X is a value less than 1.
| X | ACOSH(X) |
|---|---|
+inf |
+inf |
-inf |
NaN |
NaN |
NaN |
| X < 1 | Error |
SIN
SIN(X)
Description
Computes the sine of X where X is specified in radians. Never fails.
| X | SIN(X) |
|---|---|
+inf |
NaN |
-inf |
NaN |
NaN |
NaN |
SINH
SINH(X)
Description
Computes the hyperbolic sine of X where X is specified in radians. Generates an error if overflow occurs.
| X | SINH(X) |
|---|---|
+inf |
+inf |
-inf |
-inf |
NaN |
NaN |
ASIN
ASIN(X)
Description
Computes the principal value of the inverse sine of X. The return value is in the range [-π/2,π/2]. Generates an error if X is outside of the range [-1, 1].
| X | ASIN(X) |
|---|---|
+inf |
NaN |
-inf |
NaN |
NaN |
NaN |
| X < -1 | Error |
| X > 1 | Error |
ASINH
ASINH(X)
Description
Computes the inverse hyperbolic sine of X. Does not fail.
| X | ASINH(X) |
|---|---|
+inf |
+inf |
-inf |
-inf |
NaN |
NaN |
TAN
TAN(X)
Description
Computes the tangent of X where X is specified in radians. Generates an error if overflow occurs.
| X | TAN(X) |
|---|---|
+inf |
NaN |
-inf |
NaN |
NaN |
NaN |
TANH
TANH(X)
Description
Computes the hyperbolic tangent of X where X is specified in radians. Does not fail.
| X | TANH(X) |
|---|---|
+inf |
1.0 |
-inf |
-1.0 |
NaN |
NaN |
ATAN
ATAN(X)
Description
Computes the principal value of the inverse tangent of X. The return value is in the range [-π/2,π/2]. Does not fail.
| X | ATAN(X) |
|---|---|
+inf |
π/2 |
-inf |
-π/2 |
NaN |
NaN |
ATANH
ATANH(X)
Description
Computes the inverse hyperbolic tangent of X. Generates an error if X is outside of the range [-1, 1].
| X | ATANH(X) |
|---|---|
+inf |
NaN |
-inf |
NaN |
NaN |
NaN |
| X < -1 | Error |
| X > 1 | Error |
ATAN2
ATAN2(X, Y)
Description
Calculates the principal value of the inverse tangent of X/Y using the signs of the two arguments to determine the quadrant. The return value is in the range [-π,π].
| X | Y | ATAN2(X, Y) |
|---|---|---|
NaN |
Any value | NaN |
| Any value | NaN |
NaN |
| 0.0 | 0.0 | 0.0 |
| Positive Finite value | -inf |
π |
| Negative Finite value | -inf |
-π |
| Finite value | +inf |
0.0 |
+inf |
Finite value | π/2 |
-inf |
Finite value | -π/2 |
+inf |
-inf |
¾π |
-inf |
-inf |
-¾π |
+inf |
+inf |
π/4 |
-inf |
+inf |
-π/4 |
RANGE_BUCKET
RANGE_BUCKET(point, boundaries_array)
Description
RANGE_BUCKET scans through a sorted array and returns the 0-based position
of the point's upper bound. This can be useful if you need to group your data to
build partitions, histograms, business-defined rules, and more.
RANGE_BUCKET follows these rules:
If the point exists in the array, returns the index of the next larger value.
RANGE_BUCKET(20, [0, 10, 20, 30, 40]) -- 3 is return value RANGE_BUCKET(20, [0, 10, 20, 20, 40, 40]) -- 4 is return valueIf the point does not exist in the array, but it falls between two values, returns the index of the larger value.
RANGE_BUCKET(25, [0, 10, 20, 30, 40]) -- 3 is return valueIf the point is smaller than the first value in the array, returns 0.
RANGE_BUCKET(-10, [5, 10, 20, 30, 40]) -- 0 is return valueIf the point is greater than or equal to the last value in the array, returns the length of the array.
RANGE_BUCKET(80, [0, 10, 20, 30, 40]) -- 5 is return valueIf the array is empty, returns 0.
RANGE_BUCKET(80, []) -- 0 is return valueIf the point is
NULLorNaN, returnsNULL.RANGE_BUCKET(NULL, [0, 10, 20, 30, 40]) -- NULL is return valueThe data type for the point and array must be compatible.
RANGE_BUCKET('a', ['a', 'b', 'c', 'd']) -- 1 is return value RANGE_BUCKET(1.2, [1, 1.2, 1.4, 1.6]) -- 2 is return value RANGE_BUCKET(1.2, [1, 2, 4, 6]) -- execution failure
Execution failure occurs when:
The array has a
NaNorNULLvalue in it.RANGE_BUCKET(80, [NULL, 10, 20, 30, 40]) -- execution failureThe array is not sorted in ascending order.
RANGE_BUCKET(30, [10, 30, 20, 40, 50]) -- execution failure
Parameters
point: A generic value.boundaries_array: A generic array of values.
Return Value
INT64
Examples
In a table called students, check to see how many records would
exist in each age_group bucket, based on a student's age:
- age_group 0 (age < 10)
- age_group 1 (age >= 10, age < 20)
- age_group 2 (age >= 20, age < 30)
- age_group 3 (age >= 30)
WITH students AS
(
SELECT 9 AS age UNION ALL
SELECT 20 AS age UNION ALL
SELECT 25 AS age UNION ALL
SELECT 31 AS age UNION ALL
SELECT 32 AS age UNION ALL
SELECT 33 AS age
)
SELECT RANGE_BUCKET(age, [10, 20, 30]) AS age_group, COUNT(*) AS count
FROM students
GROUP BY 1
+--------------+-------+
| age_group | count |
+--------------+-------+
| 0 | 1 |
| 2 | 2 |
| 3 | 3 |
+--------------+-------+
Navigation functions
The following sections describe the navigation functions that BigQuery supports. Navigation functions are a subset of analytic functions. For an explanation of how analytic functions work, see Analytic Function Concepts. For an explanation of how navigation functions work, see Navigation Function Concepts.
FIRST_VALUE
FIRST_VALUE (value_expression [{RESPECT | IGNORE} NULLS])
Description
Returns the value of the value_expression for the first row in the current
window frame.
This function includes NULL values in the calculation unless IGNORE NULLS is
present. If IGNORE NULLS is present, the function excludes NULL values from
the calculation.
Supported Argument Types
value_expression can be any data type that an expression can return.
Return Data Type
Same type as value_expression.
Examples
The following example computes the fastest time for each division.
WITH finishers AS
(SELECT 'Sophia Liu' as name,
TIMESTAMP '2016-10-18 2:51:45' as finish_time,
'F30-34' as division
UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
FORMAT_TIMESTAMP('%X', finish_time) AS finish_time,
division,
FORMAT_TIMESTAMP('%X', fastest_time) AS fastest_time,
TIMESTAMP_DIFF(finish_time, fastest_time, SECOND) AS delta_in_seconds
FROM (
SELECT name,
finish_time,
division,
FIRST_VALUE(finish_time)
OVER (PARTITION BY division ORDER BY finish_time ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS fastest_time
FROM finishers);
+-----------------+-------------+----------+--------------+------------------+
| name | finish_time | division | fastest_time | delta_in_seconds |
+-----------------+-------------+----------+--------------+------------------+
| Carly Forte | 03:08:58 | F25-29 | 03:08:58 | 0 |
| Sophia Liu | 02:51:45 | F30-34 | 02:51:45 | 0 |
| Nikki Leith | 02:59:01 | F30-34 | 02:51:45 | 436 |
| Jen Edwards | 03:06:36 | F30-34 | 02:51:45 | 891 |
| Meghan Lederer | 03:07:41 | F30-34 | 02:51:45 | 956 |
| Lauren Reasoner | 03:10:14 | F30-34 | 02:51:45 | 1109 |
| Lisa Stelzner | 02:54:11 | F35-39 | 02:54:11 | 0 |
| Lauren Matthews | 03:01:17 | F35-39 | 02:54:11 | 426 |
| Desiree Berry | 03:05:42 | F35-39 | 02:54:11 | 691 |
| Suzy Slane | 03:06:24 | F35-39 | 02:54:11 | 733 |
+-----------------+-------------+----------+--------------+------------------+
LAST_VALUE
LAST_VALUE (value_expression [{RESPECT | IGNORE} NULLS])
Description
Returns the value of the value_expression for the last row in the current
window frame.
This function includes NULL values in the calculation unless IGNORE NULLS is
present. If IGNORE NULLS is present, the function excludes NULL values from
the calculation.
Supported Argument Types
value_expression can be any data type that an expression can return.
Return Data Type
Same type as value_expression.
Examples
The following example computes the slowest time for each division.
WITH finishers AS
(SELECT 'Sophia Liu' as name,
TIMESTAMP '2016-10-18 2:51:45' as finish_time,
'F30-34' as division
UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
FORMAT_TIMESTAMP('%X', finish_time) AS finish_time,
division,
FORMAT_TIMESTAMP('%X', slowest_time) AS slowest_time,
TIMESTAMP_DIFF(slowest_time, finish_time, SECOND) AS delta_in_seconds
FROM (
SELECT name,
finish_time,
division,
LAST_VALUE(finish_time)
OVER (PARTITION BY division ORDER BY finish_time ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS slowest_time
FROM finishers);
+-----------------+-------------+----------+--------------+------------------+
| name | finish_time | division | slowest_time | delta_in_seconds |
+-----------------+-------------+----------+--------------+------------------+
| Carly Forte | 03:08:58 | F25-29 | 03:08:58 | 0 |
| Sophia Liu | 02:51:45 | F30-34 | 03:10:14 | 1109 |
| Nikki Leith | 02:59:01 | F30-34 | 03:10:14 | 673 |
| Jen Edwards | 03:06:36 | F30-34 | 03:10:14 | 218 |
| Meghan Lederer | 03:07:41 | F30-34 | 03:10:14 | 153 |
| Lauren Reasoner | 03:10:14 | F30-34 | 03:10:14 | 0 |
| Lisa Stelzner | 02:54:11 | F35-39 | 03:06:24 | 733 |
| Lauren Matthews | 03:01:17 | F35-39 | 03:06:24 | 307 |
| Desiree Berry | 03:05:42 | F35-39 | 03:06:24 | 42 |
| Suzy Slane | 03:06:24 | F35-39 | 03:06:24 | 0 |
+-----------------+-------------+----------+--------------+------------------+
NTH_VALUE
NTH_VALUE (value_expression, constant_integer_expression [{RESPECT | IGNORE} NULLS])
Description
Returns the value of value_expression at the Nth row of the current window
frame, where Nth is defined by constant_integer_expression. Returns NULL if
there is no such row.
This function includes NULL values in the calculation unless IGNORE NULLS is
present. If IGNORE NULLS is present, the function excludes NULL values from
the calculation.
Supported Argument Types
value_expressioncan be any data type that can be returned from an expression.constant_integer_expressioncan be any constant expression that returns an integer.
Return Data Type
Same type as value_expression.
Examples
WITH finishers AS
(SELECT 'Sophia Liu' as name,
TIMESTAMP '2016-10-18 2:51:45' as finish_time,
'F30-34' as division
UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
FORMAT_TIMESTAMP('%X', finish_time) AS finish_time,
division,
FORMAT_TIMESTAMP('%X', fastest_time) AS fastest_time,
FORMAT_TIMESTAMP('%X', second_fastest) AS second_fastest
FROM (
SELECT name,
finish_time,
division,finishers,
FIRST_VALUE(finish_time)
OVER w1 AS fastest_time,
NTH_VALUE(finish_time, 2)
OVER w1 as second_fastest
FROM finishers
WINDOW w1 AS (
PARTITION BY division ORDER BY finish_time ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING));
+-----------------+-------------+----------+--------------+----------------+
| name | finish_time | division | fastest_time | second_fastest |
+-----------------+-------------+----------+--------------+----------------+
| Carly Forte | 03:08:58 | F25-29 | 03:08:58 | NULL |
| Sophia Liu | 02:51:45 | F30-34 | 02:51:45 | 02:59:01 |
| Nikki Leith | 02:59:01 | F30-34 | 02:51:45 | 02:59:01 |
| Jen Edwards | 03:06:36 | F30-34 | 02:51:45 | 02:59:01 |
| Meghan Lederer | 03:07:41 | F30-34 | 02:51:45 | 02:59:01 |
| Lauren Reasoner | 03:10:14 | F30-34 | 02:51:45 | 02:59:01 |
| Lisa Stelzner | 02:54:11 | F35-39 | 02:54:11 | 03:01:17 |
| Lauren Matthews | 03:01:17 | F35-39 | 02:54:11 | 03:01:17 |
| Desiree Berry | 03:05:42 | F35-39 | 02:54:11 | 03:01:17 |
| Suzy Slane | 03:06:24 | F35-39 | 02:54:11 | 03:01:17 |
+-----------------+-------------+----------+--------------+----------------+
LEAD
LEAD (value_expression[, offset [, default_expression]])
Description
Returns the value of the value_expression on a subsequent row. Changing the
offset value changes which subsequent row is returned; the default value is
1, indicating the next row in the window frame. An error occurs if offset is
NULL or a negative value.
The optional default_expression is used if there isn't a row in the window
frame at the specified offset. This expression must be a constant expression and
its type must be implicitly coercible to the type of value_expression. If left
unspecified, default_expression defaults to NULL.
Supported Argument Types
value_expressioncan be any data type that can be returned from an expression.offsetmust be a non-negative integer literal or parameter.default_expressionmust be compatible with the value expression type.
Return Data Type
Same type as value_expression.
Examples
The following example illustrates a basic use of the LEAD function.
WITH finishers AS
(SELECT 'Sophia Liu' as name,
TIMESTAMP '2016-10-18 2:51:45' as finish_time,
'F30-34' as division
UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
finish_time,
division,
LEAD(name)
OVER (PARTITION BY division ORDER BY finish_time ASC) AS followed_by
FROM finishers;
+-----------------+-------------+----------+-----------------+
| name | finish_time | division | followed_by |
+-----------------+-------------+----------+-----------------+
| Carly Forte | 03:08:58 | F25-29 | NULL |
| Sophia Liu | 02:51:45 | F30-34 | Nikki Leith |
| Nikki Leith | 02:59:01 | F30-34 | Jen Edwards |
| Jen Edwards | 03:06:36 | F30-34 | Meghan Lederer |
| Meghan Lederer | 03:07:41 | F30-34 | Lauren Reasoner |
| Lauren Reasoner | 03:10:14 | F30-34 | NULL |
| Lisa Stelzner | 02:54:11 | F35-39 | Lauren Matthews |
| Lauren Matthews | 03:01:17 | F35-39 | Desiree Berry |
| Desiree Berry | 03:05:42 | F35-39 | Suzy Slane |
| Suzy Slane | 03:06:24 | F35-39 | NULL |
+-----------------+-------------+----------+-----------------+
This next example uses the optional offset parameter.
WITH finishers AS
(SELECT 'Sophia Liu' as name,
TIMESTAMP '2016-10-18 2:51:45' as finish_time,
'F30-34' as division
UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
finish_time,
division,
LEAD(name, 2)
OVER (PARTITION BY division ORDER BY finish_time ASC) AS two_runners_back
FROM finishers;
+-----------------+-------------+----------+------------------+
| name | finish_time | division | two_runners_back |
+-----------------+-------------+----------+------------------+
| Carly Forte | 03:08:58 | F25-29 | NULL |
| Sophia Liu | 02:51:45 | F30-34 | Jen Edwards |
| Nikki Leith | 02:59:01 | F30-34 | Meghan Lederer |
| Jen Edwards | 03:06:36 | F30-34 | Lauren Reasoner |
| Meghan Lederer | 03:07:41 | F30-34 | NULL |
| Lauren Reasoner | 03:10:14 | F30-34 | NULL |
| Lisa Stelzner | 02:54:11 | F35-39 | Desiree Berry |
| Lauren Matthews | 03:01:17 | F35-39 | Suzy Slane |
| Desiree Berry | 03:05:42 | F35-39 | NULL |
| Suzy Slane | 03:06:24 | F35-39 | NULL |
+-----------------+-------------+----------+------------------+
The following example replaces NULL values with a default value.
WITH finishers AS
(SELECT 'Sophia Liu' as name,
TIMESTAMP '2016-10-18 2:51:45' as finish_time,
'F30-34' as division
UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
finish_time,
division,
LEAD(name, 2, 'Nobody')
OVER (PARTITION BY division ORDER BY finish_time ASC) AS two_runners_back
FROM finishers;
+-----------------+-------------+----------+------------------+
| name | finish_time | division | two_runners_back |
+-----------------+-------------+----------+------------------+
| Carly Forte | 03:08:58 | F25-29 | Nobody |
| Sophia Liu | 02:51:45 | F30-34 | Jen Edwards |
| Nikki Leith | 02:59:01 | F30-34 | Meghan Lederer |
| Jen Edwards | 03:06:36 | F30-34 | Lauren Reasoner |
| Meghan Lederer | 03:07:41 | F30-34 | Nobody |
| Lauren Reasoner | 03:10:14 | F30-34 | Nobody |
| Lisa Stelzner | 02:54:11 | F35-39 | Desiree Berry |
| Lauren Matthews | 03:01:17 | F35-39 | Suzy Slane |
| Desiree Berry | 03:05:42 | F35-39 | Nobody |
| Suzy Slane | 03:06:24 | F35-39 | Nobody |
+-----------------+-------------+----------+------------------+
LAG
LAG (value_expression[, offset [, default_expression]])
Description
Returns the value of the value_expression on a preceding row. Changing the
offset value changes which preceding row is returned; the default value is
1, indicating the previous row in the window frame. An error occurs if
offset is NULL or a negative value.
The optional default_expression is used if there isn't a row in the window
frame at the specified offset. This expression must be a constant expression and
its type must be implicitly coercible to the type of value_expression. If left
unspecified, default_expression defaults to NULL.
Supported Argument Types
value_expressioncan be any data type that can be returned from an expression.offsetmust be a non-negative integer literal or parameter.default_expressionmust be compatible with the value expression type.
Return Data Type
Same type as value_expression.
Examples
The following example illustrates a basic use of the LAG function.
WITH finishers AS
(SELECT 'Sophia Liu' as name,
TIMESTAMP '2016-10-18 2:51:45' as finish_time,
'F30-34' as division
UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
finish_time,
division,
LAG(name)
OVER (PARTITION BY division ORDER BY finish_time ASC) AS preceding_runner
FROM finishers;
+-----------------+-------------+----------+------------------+
| name | finish_time | division | preceding_runner |
+-----------------+-------------+----------+------------------+
| Carly Forte | 03:08:58 | F25-29 | NULL |
| Sophia Liu | 02:51:45 | F30-34 | NULL |
| Nikki Leith | 02:59:01 | F30-34 | Sophia Liu |
| Jen Edwards | 03:06:36 | F30-34 | Nikki Leith |
| Meghan Lederer | 03:07:41 | F30-34 | Jen Edwards |
| Lauren Reasoner | 03:10:14 | F30-34 | Meghan Lederer |
| Lisa Stelzner | 02:54:11 | F35-39 | NULL |
| Lauren Matthews | 03:01:17 | F35-39 | Lisa Stelzner |
| Desiree Berry | 03:05:42 | F35-39 | Lauren Matthews |
| Suzy Slane | 03:06:24 | F35-39 | Desiree Berry |
+-----------------+-------------+----------+------------------+
This next example uses the optional offset parameter.
WITH finishers AS
(SELECT 'Sophia Liu' as name,
TIMESTAMP '2016-10-18 2:51:45' as finish_time,
'F30-34' as division
UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
finish_time,
division,
LAG(name, 2)
OVER (PARTITION BY division ORDER BY finish_time ASC) AS two_runners_ahead
FROM finishers;
+-----------------+-------------+----------+-------------------+
| name | finish_time | division | two_runners_ahead |
+-----------------+-------------+----------+-------------------+
| Carly Forte | 03:08:58 | F25-29 | NULL |
| Sophia Liu | 02:51:45 | F30-34 | NULL |
| Nikki Leith | 02:59:01 | F30-34 | NULL |
| Jen Edwards | 03:06:36 | F30-34 | Sophia Liu |
| Meghan Lederer | 03:07:41 | F30-34 | Nikki Leith |
| Lauren Reasoner | 03:10:14 | F30-34 | Jen Edwards |
| Lisa Stelzner | 02:54:11 | F35-39 | NULL |
| Lauren Matthews | 03:01:17 | F35-39 | NULL |
| Desiree Berry | 03:05:42 | F35-39 | Lisa Stelzner |
| Suzy Slane | 03:06:24 | F35-39 | Lauren Matthews |
+-----------------+-------------+----------+-------------------+
The following example replaces NULL values with a default value.
WITH finishers AS
(SELECT 'Sophia Liu' as name,
TIMESTAMP '2016-10-18 2:51:45' as finish_time,
'F30-34' as division
UNION ALL SELECT 'Lisa Stelzner', TIMESTAMP '2016-10-18 2:54:11', 'F35-39'
UNION ALL SELECT 'Nikki Leith', TIMESTAMP '2016-10-18 2:59:01', 'F30-34'
UNION ALL SELECT 'Lauren Matthews', TIMESTAMP '2016-10-18 3:01:17', 'F35-39'
UNION ALL SELECT 'Desiree Berry', TIMESTAMP '2016-10-18 3:05:42', 'F35-39'
UNION ALL SELECT 'Suzy Slane', TIMESTAMP '2016-10-18 3:06:24', 'F35-39'
UNION ALL SELECT 'Jen Edwards', TIMESTAMP '2016-10-18 3:06:36', 'F30-34'
UNION ALL SELECT 'Meghan Lederer', TIMESTAMP '2016-10-18 3:07:41', 'F30-34'
UNION ALL SELECT 'Carly Forte', TIMESTAMP '2016-10-18 3:08:58', 'F25-29'
UNION ALL SELECT 'Lauren Reasoner', TIMESTAMP '2016-10-18 3:10:14', 'F30-34')
SELECT name,
finish_time,
division,
LAG(name, 2, 'Nobody')
OVER (PARTITION BY division ORDER BY finish_time ASC) AS two_runners_ahead
FROM finishers;
+-----------------+-------------+----------+-------------------+
| name | finish_time | division | two_runners_ahead |
+-----------------+-------------+----------+-------------------+
| Carly Forte | 03:08:58 | F25-29 | Nobody |
| Sophia Liu | 02:51:45 | F30-34 | Nobody |
| Nikki Leith | 02:59:01 | F30-34 | Nobody |
| Jen Edwards | 03:06:36 | F30-34 | Sophia Liu |
| Meghan Lederer | 03:07:41 | F30-34 | Nikki Leith |
| Lauren Reasoner | 03:10:14 | F30-34 | Jen Edwards |
| Lisa Stelzner | 02:54:11 | F35-39 | Nobody |
| Lauren Matthews | 03:01:17 | F35-39 | Nobody |
| Desiree Berry | 03:05:42 | F35-39 | Lisa Stelzner |
| Suzy Slane | 03:06:24 | F35-39 | Lauren Matthews |
+-----------------+-------------+----------+-------------------+
PERCENTILE_CONT
PERCENTILE_CONT (value_expression, percentile [{RESPECT | IGNORE} NULLS])
Description
Computes the specified percentile value for the value_expression, with linear interpolation.
This function ignores NULL values if RESPECT NULLS is absent. If RESPECT
NULLS is present:
- Interpolation between two
NULLvalues returnsNULL. - Interpolation between a
NULLvalue and a non-NULLvalue returns the non-NULLvalue.
Supported Argument Types
value_expressionandpercentilemust have one of the following types:NUMERICBIGNUMERICFLOAT64
percentilemust be a literal in the range[0, 1].
Return Data Type
The return data type is determined by the argument types with the following table.
| INPUT | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|
| NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
| FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
Examples
The following example computes the value for some percentiles from a column of values while ignoring nulls.
SELECT
PERCENTILE_CONT(x, 0) OVER() AS min,
PERCENTILE_CONT(x, 0.01) OVER() AS percentile1,
PERCENTILE_CONT(x, 0.5) OVER() AS median,
PERCENTILE_CONT(x, 0.9) OVER() AS percentile90,
PERCENTILE_CONT(x, 1) OVER() AS max
FROM UNNEST([0, 3, NULL, 1, 2]) AS x LIMIT 1;
+-----+-------------+--------+--------------+-----+
| min | percentile1 | median | percentile90 | max |
+-----+-------------+--------+--------------+-----+
| 0 | 0.03 | 1.5 | 2.7 | 3 |
+-----+-------------+--------+--------------+-----+
The following example computes the value for some percentiles from a column of values while respecting nulls.
SELECT
PERCENTILE_CONT(x, 0 RESPECT NULLS) OVER() AS min,
PERCENTILE_CONT(x, 0.01 RESPECT NULLS) OVER() AS percentile1,
PERCENTILE_CONT(x, 0.5 RESPECT NULLS) OVER() AS median,
PERCENTILE_CONT(x, 0.9 RESPECT NULLS) OVER() AS percentile90,
PERCENTILE_CONT(x, 1 RESPECT NULLS) OVER() AS max
FROM UNNEST([0, 3, NULL, 1, 2]) AS x LIMIT 1;
+------+-------------+--------+--------------+-----+
| min | percentile1 | median | percentile90 | max |
+------+-------------+--------+--------------+-----+
| NULL | 0 | 1 | 2.6 | 3 |
+------+-------------+--------+--------------+-----+
PERCENTILE_DISC
PERCENTILE_DISC (value_expression, percentile [{RESPECT | IGNORE} NULLS])
Description
Computes the specified percentile value for a discrete value_expression. The
returned value is the first sorted value of value_expression with cumulative
distribution greater than or equal to the given percentile value.
This function ignores NULL values unless RESPECT NULLS is present.
Supported Argument Types
value_expressioncan be any orderable type.percentilemust be a literal in the range[0, 1], with one of the following types:NUMERICBIGNUMERICFLOAT64
Return Data Type
Same type as value_expression.
Examples
The following example computes the value for some percentiles from a column of values while ignoring nulls.
SELECT
x,
PERCENTILE_DISC(x, 0) OVER() AS min,
PERCENTILE_DISC(x, 0.5) OVER() AS median,
PERCENTILE_DISC(x, 1) OVER() AS max
FROM UNNEST(['c', NULL, 'b', 'a']) AS x;
+------+-----+--------+-----+
| x | min | median | max |
+------+-----+--------+-----+
| c | a | b | c |
| NULL | a | b | c |
| b | a | b | c |
| a | a | b | c |
+------+-----+--------+-----+
The following example computes the value for some percentiles from a column of values while respecting nulls.
SELECT
x,
PERCENTILE_DISC(x, 0 RESPECT NULLS) OVER() AS min,
PERCENTILE_DISC(x, 0.5 RESPECT NULLS) OVER() AS median,
PERCENTILE_DISC(x, 1 RESPECT NULLS) OVER() AS max
FROM UNNEST(['c', NULL, 'b', 'a']) AS x;
+------+------+--------+-----+
| x | min | median | max |
+------+------+--------+-----+
| c | NULL | a | c |
| NULL | NULL | a | c |
| b | NULL | a | c |
| a | NULL | a | c |
+------+------+--------+-----+
Aggregate analytic functions
The following sections describe the aggregate analytic functions that BigQuery supports. For an explanation of how analytic functions work, see Analytic Function Concepts. For an explanation of how aggregate analytic functions work, see Aggregate Analytic Function Concepts.
BigQuery supports the following aggregate functions as analytic functions:
- ANY_VALUE
- ARRAY_AGG
- AVG
- CORR
- COUNT
- COUNTIF
- COVAR_POP
- COVAR_SAMP
- MAX
- MIN
- ST_CLUSTERDBSCAN
- STDDEV_POP
- STDDEV_SAMP
- STRING_AGG
- SUM
- VAR_POP
- VAR_SAMP
OVER clause requirements:
PARTITION BY: Optional.ORDER BY: Optional. Disallowed ifDISTINCTis present.window_frame_clause: Optional. Disallowed ifDISTINCTis present.
Example:
COUNT(*) OVER (ROWS UNBOUNDED PRECEDING)
SUM(DISTINCT x) OVER ()
Hash functions
FARM_FINGERPRINT
FARM_FINGERPRINT(value)
Description
Computes the fingerprint of the STRING or BYTES input using the
Fingerprint64 function from the
open-source FarmHash library. The output
of this function for a particular input will never change.
Return type
INT64
Examples
WITH example AS (
SELECT 1 AS x, "foo" AS y, true AS z UNION ALL
SELECT 2 AS x, "apple" AS y, false AS z UNION ALL
SELECT 3 AS x, "" AS y, true AS z
)
SELECT
*,
FARM_FINGERPRINT(CONCAT(CAST(x AS STRING), y, CAST(z AS STRING)))
AS row_fingerprint
FROM example;
+---+-------+-------+----------------------+
| x | y | z | row_fingerprint |
+---+-------+-------+----------------------+
| 1 | foo | true | -1541654101129638711 |
| 2 | apple | false | 2794438866806483259 |
| 3 | | true | -4880158226897771312 |
+---+-------+-------+----------------------+
MD5
MD5(input)
Description
Computes the hash of the input using the
MD5 algorithm. The input can either be
STRING or BYTES. The string version treats the input as an array of bytes.
This function returns 16 bytes.
Return type
BYTES
Example
SELECT MD5("Hello World") as md5;
-- Note that the result of MD5 is of type BYTES, displayed as a base64-encoded string.
+--------------------------+
| md5 |
+--------------------------+
| sQqNsWTgdUEFt6mb5y4/5Q== |
+--------------------------+
SHA1
SHA1(input)
Description
Computes the hash of the input using the
SHA-1 algorithm. The input can either be
STRING or BYTES. The string version treats the input as an array of bytes.
This function returns 20 bytes.
Return type
BYTES
Example
SELECT SHA1("Hello World") as sha1;
-- Note that the result of SHA1 is of type BYTES, displayed as a base64-encoded string.
+------------------------------+
| sha1 |
+------------------------------+
| Ck1VqNd45QIvq3AZd8XYQLvEhtA= |
+------------------------------+
SHA256
SHA256(input)
Description
Computes the hash of the input using the
SHA-256 algorithm. The input can either be
STRING or BYTES. The string version treats the input as an array of bytes.
This function returns 32 bytes.
Return type
BYTES
Example
SELECT SHA256("Hello World") as sha256;
SHA512
SHA512(input)
Description
Computes the hash of the input using the
SHA-512 algorithm. The input can either be
STRING or BYTES. The string version treats the input as an array of bytes.
This function returns 64 bytes.
Return type
BYTES
Example
SELECT SHA512("Hello World") as sha512;
String functions
These string functions work on two different values:
STRING and BYTES data types. STRING values must be well-formed UTF-8.
Functions that return position values, such as STRPOS,
encode those positions as INT64. The value 1
refers to the first character (or byte), 2 refers to the second, and so on.
The value 0 indicates an invalid index. When working on STRING types, the
returned positions refer to character positions.
All string comparisons are done byte-by-byte, without regard to Unicode canonical equivalence.
ASCII
ASCII(value)
Description
Returns the ASCII code for the first character or byte in value. Returns
0 if value is empty or the ASCII code is 0 for the first character
or byte.
Return type
INT64
Examples
SELECT ASCII('abcd') as A, ASCII('a') as B, ASCII('') as C, ASCII(NULL) as D;
+-------+-------+-------+-------+
| A | B | C | D |
+-------+-------+-------+-------+
| 97 | 97 | 0 | NULL |
+-------+-------+-------+-------+
BYTE_LENGTH
BYTE_LENGTH(value)
Description
Returns the length of the STRING or BYTES value in BYTES,
regardless of whether the type of the value is STRING or BYTES.
Return type
INT64
Examples
WITH example AS
(SELECT "абвгд" AS characters, b"абвгд" AS bytes)
SELECT
characters,
BYTE_LENGTH(characters) AS string_example,
bytes,
BYTE_LENGTH(bytes) AS bytes_example
FROM example;
+------------+----------------+-------+---------------+
| characters | string_example | bytes | bytes_example |
+------------+----------------+-------+---------------+
| абвгд | 10 | абвгд | 10 |
+------------+----------------+-------+---------------+
CHAR_LENGTH
CHAR_LENGTH(value)
Description
Returns the length of the STRING in characters.
Return type
INT64
Examples
WITH example AS
(SELECT "абвгд" AS characters)
SELECT
characters,
CHAR_LENGTH(characters) AS char_length_example
FROM example;
+------------+---------------------+
| characters | char_length_example |
+------------+---------------------+
| абвгд | 5 |
+------------+---------------------+
CHARACTER_LENGTH
CHARACTER_LENGTH(value)
Description
Synonym for CHAR_LENGTH.
Return type
INT64
Examples
WITH example AS
(SELECT "абвгд" AS characters)
SELECT
characters,
CHARACTER_LENGTH(characters) AS char_length_example
FROM example;
+------------+---------------------+
| characters | char_length_example |
+------------+---------------------+
| абвгд | 5 |
+------------+---------------------+
CHR
CHR(value)
Description
Takes a Unicode code point and returns
the character that matches the code point. Each valid code point should fall
within the range of [0, 0xD7FF] and [0xE000, 0x10FFFF]. Returns an empty string
if the code point is 0. If an invalid Unicode code point is specified, an
error is returned.
To work with an array of Unicode code points, see
CODE_POINTS_TO_STRING
Return type
STRING
Examples
SELECT CHR(65) AS A, CHR(255) AS B, CHR(513) AS C, CHR(1024) AS D;
+-------+-------+-------+-------+
| A | B | C | D |
+-------+-------+-------+-------+
| A | ÿ | ȁ | Ѐ |
+-------+-------+-------+-------+
SELECT CHR(97) AS A, CHR(0xF9B5) AS B, CHR(0) AS C, CHR(NULL) AS D;
+-------+-------+-------+-------+
| A | B | C | D |
+-------+-------+-------+-------+
| a | 例 | | NULL |
+-------+-------+-------+-------+
CODE_POINTS_TO_BYTES
CODE_POINTS_TO_BYTES(ascii_values)
Description
Takes an array of extended ASCII
code points
(ARRAY of INT64) and returns BYTES.
To convert from BYTES to an array of code points, see
TO_CODE_POINTS.
Return type
BYTES
Examples
The following is a basic example using CODE_POINTS_TO_BYTES.
SELECT CODE_POINTS_TO_BYTES([65, 98, 67, 100]) AS bytes;
-- Note that the result of CODE_POINTS_TO_BYTES is of type BYTES, displayed as a base64-encoded string.
-- In BYTES format, b'AbCd' is the result.
+----------+
| bytes |
+----------+
| QWJDZA== |
+----------+
The following example uses a rotate-by-13 places (ROT13) algorithm to encode a string.
SELECT CODE_POINTS_TO_BYTES(ARRAY_AGG(
(SELECT
CASE
WHEN chr BETWEEN b'a' and b'z'
THEN TO_CODE_POINTS(b'a')[offset(0)] +
MOD(code+13-TO_CODE_POINTS(b'a')[offset(0)],26)
WHEN chr BETWEEN b'A' and b'Z'
THEN TO_CODE_POINTS(b'A')[offset(0)] +
MOD(code+13-TO_CODE_POINTS(b'A')[offset(0)],26)
ELSE code
END
FROM
(SELECT code, CODE_POINTS_TO_BYTES([code]) chr)
) ORDER BY OFFSET)) AS encoded_string
FROM UNNEST(TO_CODE_POINTS(b'Test String!')) code WITH OFFSET;
-- Note that the result of CODE_POINTS_TO_BYTES is of type BYTES, displayed as a base64-encoded string.
-- In BYTES format, b'Grfg Fgevat!' is the result.
+------------------+
| encoded_string |
+------------------+
| R3JmZyBGZ2V2YXQh |
+------------------+
CODE_POINTS_TO_STRING
CODE_POINTS_TO_STRING(value)
Description
Takes an array of Unicode code points
(ARRAY of INT64) and
returns a STRING. If a code point is 0, does not return a character for it
in the STRING.
To convert from a string to an array of code points, see TO_CODE_POINTS.
Return type
STRING
Examples
The following are basic examples using CODE_POINTS_TO_STRING.
SELECT CODE_POINTS_TO_STRING([65, 255, 513, 1024]) AS string;
+--------+
| string |
+--------+
| AÿȁЀ |
+--------+
SELECT CODE_POINTS_TO_STRING([97, 0, 0xF9B5]) AS string;
+--------+
| string |
+--------+
| a例 |
+--------+
SELECT CODE_POINTS_TO_STRING([65, 255, NULL, 1024]) AS string;
+--------+
| string |
+--------+
| NULL |
+--------+
The following example computes the frequency of letters in a set of words.
WITH Words AS (
SELECT word
FROM UNNEST(['foo', 'bar', 'baz', 'giraffe', 'llama']) AS word
)
SELECT
CODE_POINTS_TO_STRING([code_point]) AS letter,
COUNT(*) AS letter_count
FROM Words,
UNNEST(TO_CODE_POINTS(word)) AS code_point
GROUP BY 1
ORDER BY 2 DESC;
+--------+--------------+
| letter | letter_count |
+--------+--------------+
| a | 5 |
| f | 3 |
| r | 2 |
| b | 2 |
| l | 2 |
| o | 2 |
| g | 1 |
| z | 1 |
| e | 1 |
| m | 1 |
| i | 1 |
+--------+--------------+
CONCAT
CONCAT(value1[, ...])
Description
Concatenates one or more values into a single result. All values must be
BYTES or data types that can be cast to STRING.
The function returns NULL if any input argument is NULL.
Return type
STRING or BYTES
Examples
SELECT CONCAT("T.P.", " ", "Bar") as author;
+---------------------+
| author |
+---------------------+
| T.P. Bar |
+---------------------+
SELECT CONCAT("Summer", " ", 1923) as release_date;
+---------------------+
| release_date |
+---------------------+
| Summer 1923 |
+---------------------+
With Employees AS
(SELECT
"John" AS first_name,
"Doe" AS last_name
UNION ALL
SELECT
"Jane" AS first_name,
"Smith" AS last_name
UNION ALL
SELECT
"Joe" AS first_name,
"Jackson" AS last_name)
SELECT
CONCAT(first_name, " ", last_name)
AS full_name
FROM Employees;
+---------------------+
| full_name |
+---------------------+
| John Doe |
| Jane Smith |
| Joe Jackson |
+---------------------+
CONTAINS_SUBSTR
CONTAINS_SUBSTR(expression, search_value_literal)
Description
Performs a normalized, case-insensitive search to see if a value exists in an
expression. Returns TRUE if the value exists, otherwise returns FALSE.
The search value must be a STRING literal, but not the literal NULL.
The expression can be a column or table reference. A table reference is
evaluated as a STRUCT whose fields are the columns of the table. A
column reference is evaluated as one the following data types:
STRINGINT64BOOLNUMERICBIGNUMERICTIMESTAMPTIMEDATEDATETIMEARRAYSTRUCT
When the expression is evaluated, the result is cast to a STRING, and then
the function looks for the search value in the result. If the search value is
NULL, an error is thrown. If the expression is NULL, the return value is
NULL.
You can perform a cross-field search on an expression that evaluates to a
STRUCT or ARRAY. If the expression evaluates to a STRUCT, the cross-field
search is recursive and includes all subfields inside the STRUCT.
In a cross-field search, each field and subfield is individually converted to
a string and searched for the value. The function returns TRUE if at least one
field includes the search value; otherwise, if at least one field is NULL, it
returns NULL; otherwise, if the search value is not found and all fields are
non-NULL, it returns FALSE.
Before values are compared, they are normalized and case folded with
NFKC normalization. Wildcard searches are
not supported.
Return type
BOOL
Examples
The following query returns TRUE because this case-insensitive match
was found: blue house and Blue house.
SELECT CONTAINS_SUBSTR('the blue house', 'Blue house') AS result;
+--------+
| result |
+--------+
| true |
+--------+
The following query returns FALSE because blue was not found
in the red house.
SELECT CONTAINS_SUBSTR('the red house', 'blue') AS result;
+--------+
| result |
+--------+
| false |
+--------+
The following query returns TRUE because Ⅸ and IX represent the same
normalized value.
SELECT '\u2168 day' AS a, 'IX' AS b, CONTAINS_SUBSTR('\u2168', 'IX') AS result;
+----------------------+
| a | b | result |
+----------------------+
| Ⅸ day | IX | true |
+----------------------+
The following query returns TRUE because 35 was found inside a
STRUCT field.
SELECT CONTAINS_SUBSTR((23, 35, 41), '35') AS result;
+--------+
| result |
+--------+
| true |
+--------+
The following query returns TRUE because jk was found during a
recursive search inside a STRUCT.
SELECT CONTAINS_SUBSTR(('abc', ['def', 'ghi', 'jkl'], 'mno'), 'jk');
+--------+
| result |
+--------+
| true |
+--------+
The following query returns TRUE because NULLs are ignored when
a match is found found inside a STRUCT or ARRAY.
SELECT CONTAINS_SUBSTR((23, NULL, 41), '41') AS result;
+--------+
| result |
+--------+
| true |
+--------+
The following query returns NULL because a NULL existed in a STRUCT that
did not result in a match.
SELECT CONTAINS_SUBSTR((23, NULL, 41), '35') AS result;
+--------+
| result |
+--------+
| null |
+--------+
In the following query, an error is thrown because the search value cannot be
a literal NULL.
SELECT CONTAINS_SUBSTR('hello', NULL) AS result;
-- Throws an error
The following examples reference a table called Recipes that you can emulate
with a WITH clause like this:
WITH Recipes AS
(SELECT 'Blueberry pancakes' as Breakfast, 'Egg salad sandwich' as Lunch, 'Potato dumplings' as Dinner UNION ALL
SELECT 'Potato pancakes', 'Toasted cheese sandwich', 'Beef stroganoff' UNION ALL
SELECT 'Ham scramble', 'Steak avocado salad', 'Tomato pasta' UNION ALL
SELECT 'Avocado toast', 'Tomato soup', 'Blueberry salmon' UNION ALL
SELECT 'Corned beef hash', 'Lentil potato soup', 'Glazed ham')
SELECT * FROM Recipes;
+-------------------+-------------------------+------------------+
| Breakfast | Lunch | Dinner |
+-------------------+-------------------------+------------------+
| Bluberry pancakes | Egg salad sandwich | Potato dumplings |
| Potato pancakes | Toasted cheese sandwich | Beef stroganoff |
| Ham scramble | Steak avocado salad | Tomato pasta |
| Avocado toast | Tomato soup | Blueberry samon |
| Corned beef hash | Lentil potato soup | Glazed ham |
+-------------------+-------------------------+------------------+
The following query searches across all columns of the Recipes table for the
value toast and returns the rows that contain this value.
SELECT * FROM Recipes WHERE CONTAINS_SUBSTR(Recipes, 'toast');
+-------------------+-------------------------+------------------+
| Breakfast | Lunch | Dinner |
+-------------------+-------------------------+------------------+
| Potato pancakes | Toasted cheese sandwich | Beef stroganoff |
| Avocado toast | Tomato soup | Blueberry samon |
+-------------------+-------------------------+------------------+
The following query searches the Lunch and Dinner columns of the
Recipe table for the value potato and returns the row if either column
contains this value.
SELECT * FROM Recipes WHERE CONTAINS_SUBSTR((Lunch, Dinner), 'potato');
+-------------------+-------------------------+------------------+
| Breakfast | Lunch | Dinner |
+-------------------+-------------------------+------------------+
| Bluberry pancakes | Egg salad sandwich | Potato dumplings |
| Corned beef hash | Lentil potato soup | Glazed ham |
+-------------------+-------------------------+------------------+
The following queries search across all columns of the Recipes table
except for the Lunch and Dinner columns. They return the rows of any
columns other than Lunch or Dinner that contain the value potato.
SELECT *
FROM Recipes
WHERE CONTAINS_SUBSTR(
(SELECT AS STRUCT Recipes.* EXCEPT (Lunch, Dinner)),
'potato'
);
+-------------------+-------------------------+------------------+
| Breakfast | Lunch | Dinner |
+-------------------+-------------------------+------------------+
| Potato pancakes | Toasted cheese sandwich | Beef stroganoff |
+-------------------+-------------------------+------------------+
ENDS_WITH
ENDS_WITH(value1, value2)
Description
Takes two STRING or BYTES values. Returns TRUE if the second
value is a suffix of the first.
Return type
BOOL
Examples
WITH items AS
(SELECT "apple" as item
UNION ALL
SELECT "banana" as item
UNION ALL
SELECT "orange" as item)
SELECT
ENDS_WITH(item, "e") as example
FROM items;
+---------+
| example |
+---------+
| True |
| False |
| True |
+---------+
FORMAT
FORMAT(format_string_expression, data_type_expression[, ...])
Description
FORMAT formats a data type expression as a string.
format_string_expression: Can contain zero or more format specifiers. Each format specifier is introduced by the%symbol, and must map to one or more of the remaining arguments. In general, this is a one-to-one mapping, except when the*specifier is present. For example,%.*imaps to two arguments—a length argument and a signed integer argument. If the number of arguments related to the format specifiers is not the same as the number of arguments, an error occurs.data_type_expression: The value to format as a string. This can be any BigQuery data type.
Return type
STRING
Examples
| Description | Statement | Result |
|---|---|---|
| Simple integer | FORMAT("%d", 10) | 10 |
| Integer with left blank padding | FORMAT("|%10d|", 11) | | 11| |
| Integer with left zero padding | FORMAT("+%010d+", 12) | +0000000012+ |
| Integer with commas | FORMAT("%'d", 123456789) | 123,456,789 |
| STRING | FORMAT("-%s-", 'abcd efg') | -abcd efg- |
| FLOAT64 | FORMAT("%f %E", 1.1, 2.2) | 1.100000 2.200000E+00 |
| DATE | FORMAT("%t", date "2015-09-01") | 2015-09-01 |
| TIMESTAMP | FORMAT("%t", timestamp "2015-09-01 12:34:56 America/Los_Angeles") | 2015‑09‑01 19:34:56+00 |
The FORMAT() function does not provide fully customizable formatting for all
types and values, nor formatting that is sensitive to locale.
If custom formatting is necessary for a type, you must first format it using
type-specific format functions, such as FORMAT_DATE() or FORMAT_TIMESTAMP().
For example:
SELECT FORMAT("date: %s!", FORMAT_DATE("%B %d, %Y", date '2015-01-02'));
Returns
date: January 02, 2015!
Supported format specifiers
%[flags][width][.precision]specifier
A format specifier adds formatting when casting a value to a string. It can optionally contain these sub-specifiers:
Additional information about format specifiers:
- %g and %G behavior
- %t and %T behavior
- Error conditions
- NULL argument handling
- Additional semantic rules
Format specifiers
| Specifier | Description | Examples | Types |
d or i |
Decimal integer | 392 |
INT64 |
o |
Octal | 610 |
INT64* |
x |
Hexadecimal integer | 7fa |
INT64* |
X |
Hexadecimal integer (uppercase) | 7FA |
INT64* |
f |
Decimal notation, in [-](integer part).(fractional part) for finite values, and in lowercase for non-finite values | 392.650000 inf nan |
NUMERIC BIGNUMERIC FLOAT64 |
F |
Decimal notation, in [-](integer part).(fractional part) for finite values, and in uppercase for non-finite values | 392.650000 INF NAN |
NUMERIC BIGNUMERIC FLOAT64 |
e |
Scientific notation (mantissa/exponent), lowercase | 3.926500e+02 inf nan |
NUMERIC BIGNUMERIC FLOAT64 |
E |
Scientific notation (mantissa/exponent), uppercase | 3.926500E+02 INF NAN |
NUMERIC BIGNUMERIC FLOAT64 |
g |
Either decimal notation or scientific notation, depending on the input value's exponent and the specified precision. Lowercase. See %g and %G behavior for details. | 392.65 3.9265e+07 inf nan |
NUMERIC BIGNUMERIC FLOAT64 |
G |
Either decimal notation or scientific notation, depending on the input value's exponent and the specified precision. Uppercase. See %g and %G behavior for details. |
392.65 3.9265E+07 INF NAN |
NUMERIC BIGNUMERIC FLOAT64 |
s |
String of characters | sample | STRING |
t |
Returns a printable string representing the value. Often looks
similar to casting the argument to STRING.
See %t and %T behavior.
|
sample 2014‑01‑01 |
<any> |
T |
Produces a string that is a valid BigQuery constant with a similar type to the value's type (maybe wider, or maybe string). See %t and %T behavior. |
'sample' b'bytes sample' 1234 2.3 date '2014‑01‑01' |
<any> |
% |
'%%' produces a single '%' | % | n/a |
*The specifiers %o, %x, and %X raise an
error if negative values are used.
The format specifier can optionally contain the sub-specifiers identified above in the specifier prototype.
These sub-specifiers must comply with the following specifications.
Flags
| Flags | Description |
- |
Left-justify within the given field width; Right justification is the default (see width sub-specifier) |
+ |
Forces to precede the result with a plus or minus sign (+
or -) even for positive numbers. By default, only negative numbers
are preceded with a - sign |
| <space> | If no sign is going to be written, a blank space is inserted before the value |
# |
|
0 |
Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier) |
' |
Formats integers using the appropriating grouping character. For example:
This flag is only relevant for decimal, hex, and octal values. |
Flags may be specified in any order. Duplicate flags are not an error. When flags are not relevant for some element type, they are ignored.
Width
| Width | Description |
| <number> | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger |
* |
The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted |
Precision
| Precision | Description |
.<number> |
|
.* |
The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted |
%g and %G behavior
The %g and %G format specifiers choose either the decimal notation (like
the %f and %F specifiers) or the scientific notation (like the %e and %E
specifiers), depending on the input value's exponent and the specified
precision.
Let p stand for the specified precision (defaults to 6; 1 if the specified precision is less than 1). The input value is first converted to scientific notation with precision = (p - 1). If the resulting exponent part x is less than -4 or no less than p, the scientific notation with precision = (p - 1) is used; otherwise the decimal notation with precision = (p - 1 - x) is used.
Unless # flag is present, the trailing zeros after the decimal point
are removed, and the decimal point is also removed if there is no digit after
it.
%t and %T behavior
The %t and %T format specifiers are defined for all types. The
width, precision, and flags act as they do
for %s: the width is the minimum width and the STRING will be
padded to that size, and precision is the maximum width
of content to show and the STRING will be truncated to that size, prior to
padding to width.
The %t specifier is always meant to be a readable form of the value.
The %T specifier is always a valid SQL literal of a similar type, such as a
wider numeric type.
The literal will not include casts or a type name, except for the special case
of non-finite floating point values.
The STRING is formatted as follows:
| Type | %t | %T |
NULL of any type |
NULL | NULL |
| INT64 |
123 | 123 |
| NUMERIC | 123.0 (always with .0) | NUMERIC "123.0" |
| FLOAT64 |
123.0 (always with .0) 123e+10 inf-infNaN
|
123.0 (always with .0) 123e+10 CAST("inf" AS <type>) CAST("-inf" AS <type>) CAST("nan" AS <type>) |
| STRING | unquoted string value | quoted string literal |
| BYTES |
unquoted escaped bytes e.g. abc\x01\x02 |
quoted bytes literal e.g. b"abc\x01\x02" |
| DATE | 2011-02-03 | DATE "2011-02-03" |
| TIMESTAMP | 2011-02-03 04:05:06+00 | TIMESTAMP "2011-02-03 04:05:06+00" |
| ARRAY | [value, value, ...] where values are formatted with %t |
[value, value, ...] where values are formatted with %T |
| STRUCT | (value, value, ...) where fields are formatted with %t |
(value, value, ...) where fields are formatted with %T Special cases: Zero fields: STRUCT() One field: STRUCT(value) |
Error conditions
If a format specifier is invalid, or is not compatible with the related
argument type, or the wrong number or arguments are provided, then an error is
produced. For example, the following <format_string> expressions are invalid:
FORMAT('%s', 1)
FORMAT('%')
NULL argument handling
A NULL format string results in a NULL output STRING. Any other arguments
are ignored in this case.
The function generally produces a NULL value if a NULL argument is present.
For example, FORMAT('%i', NULL_expression) produces a NULL STRING as
output.
However, there are some exceptions: if the format specifier is %t or %T
(both of which produce STRINGs that effectively match CAST and literal value
semantics), a NULL value produces 'NULL' (without the quotes) in the result
STRING. For example, the function:
FORMAT('00-%t-00', NULL_expression);
Returns
00-NULL-00
Additional semantic rules
FLOAT64 values can be +/-inf or NaN.
When an argument has one of those values, the result of the format specifiers
%f, %F, %e, %E, %g, %G, and %t are inf, -inf, or nan
(or the same in uppercase) as appropriate. This is consistent with how
BigQuery casts these values to STRING. For %T,
BigQuery returns quoted strings for
FLOAT64 values that don't have non-string literal
representations.
FROM_BASE32
FROM_BASE32(string_expr)
Description
Converts the base32-encoded input string_expr into BYTES format. To convert
BYTES to a base32-encoded STRING, use TO_BASE32.
Return type
BYTES
Example
SELECT FROM_BASE32('MFRGGZDF74======') AS byte_data;
-- Note that the result of FROM_BASE32 is of type BYTES, displayed as a base64-encoded string.
+-----------+
| byte_data |
+-----------+
| YWJjZGX/ |
+-----------+
FROM_BASE64
FROM_BASE64(string_expr)
Description
Converts the base64-encoded input string_expr into
BYTES format. To convert
BYTES to a base64-encoded STRING,
use TO_BASE64.
There are several base64 encodings in common use that vary in exactly which
alphabet of 65 ASCII characters are used to encode the 64 digits and padding.
See RFC 4648 for details. This
function expects the alphabet [A-Za-z0-9+/=].
Return type
BYTES
Example
SELECT FROM_BASE64('/+A=') AS byte_data;
-- Note that the result of FROM_BASE64 is of type BYTES, displayed as a base64-encoded string.
+-----------+
| byte_data |
+-----------+
| /+A= |
+-----------+
To work with an encoding using a different base64 alphabet, you might need to
compose FROM_BASE64 with the REPLACE function. For instance, the
base64url url-safe and filename-safe encoding commonly used in web programming
uses -_= as the last characters rather than +/=. To decode a
base64url-encoded string, replace + and / with - and _ respectively.
SELECT FROM_BASE64(REPLACE(REPLACE("_-A=", "-", "+"), "_", "/")) AS binary;
-- Note that the result of FROM_BASE64 is of type BYTES, displayed as a base64-encoded string.
+--------+
| binary |
+--------+
| /+A= |
+--------+
FROM_HEX
FROM_HEX(string)
Description
Converts a hexadecimal-encoded STRING into BYTES format. Returns an error
if the input STRING contains characters outside the range
(0..9, A..F, a..f). The lettercase of the characters does not matter. If the
input STRING has an odd number of characters, the function acts as if the
input has an additional leading 0. To convert BYTES to a hexadecimal-encoded
STRING, use TO_HEX.
Return type
BYTES
Example
WITH Input AS (
SELECT '00010203aaeeefff' AS hex_str UNION ALL
SELECT '0AF' UNION ALL
SELECT '666f6f626172'
)
SELECT hex_str, FROM_HEX(hex_str) AS bytes_str
FROM Input;
-- Note that the result of FROM_HEX is of type BYTES, displayed as a base64-encoded string.
+------------------+--------------+
| hex_str | bytes_str |
+------------------+--------------+
| 0AF | AAECA6ru7/8= |
| 00010203aaeeefff | AK8= |
| 666f6f626172 | Zm9vYmFy |
+------------------+--------------+
INITCAP
INITCAP(value[, delimiters])
Description
Takes a STRING and returns it with the first character in each word in
uppercase and all other characters in lowercase. Non-alphabetic characters
remain the same.
delimiters is an optional string argument that is used to override the default
set of characters used to separate words. If delimiters is not specified, it
defaults to the following characters:
<whitespace> [ ] ( ) { } / | \ < > ! ? @ " ^ # $ & ~ _ , . : ; * % + -
If value or delimiters is NULL, the function returns NULL.
Return type
STRING
Examples
WITH example AS
(
SELECT "Hello World-everyone!" AS value UNION ALL
SELECT "tHe dog BARKS loudly+friendly" AS value UNION ALL
SELECT "apples&oranges;&pears" AS value UNION ALL
SELECT "καθίσματα ταινιών" AS value
)
SELECT value, INITCAP(value) AS initcap_value FROM example
+-------------------------------+-------------------------------+
| value | initcap_value |
+-------------------------------+-------------------------------+
| Hello World-everyone! | Hello World-Everyone! |
| tHe dog BARKS loudly+friendly | The Dog Barks Loudly+Friendly |
| apples&oranges;&pears | Apples&Oranges;&Pears |
| καθίσματα ταινιών | Καθίσματα Ταινιών |
+-------------------------------+-------------------------------+
WITH example AS
(
SELECT "hello WORLD!" AS value, "" AS delimiters UNION ALL
SELECT "καθίσματα ταιντιώ@ν" AS value, "τ@" AS delimiters UNION ALL
SELECT "Apples1oranges2pears" AS value, "12" AS delimiters UNION ALL
SELECT "tHisEisEaESentence" AS value, "E" AS delimiters
)
SELECT value, delimiters, INITCAP(value, delimiters) AS initcap_value FROM example;
+----------------------+------------+----------------------+
| value | delimiters | initcap_value |
+----------------------+------------+----------------------+
| hello WORLD! | | Hello world! |
| καθίσματα ταιντιώ@ν | τ@ | ΚαθίσματΑ τΑιντΙώ@Ν |
| Apples1oranges2pears | 12 | Apples1Oranges2Pears |
| tHisEisEaESentence | E | ThisEIsEAESentence |
+----------------------+------------+----------------------+
INSTR
INSTR(source_value, search_value[, position[, occurrence]])
Description
Returns the lowest 1-based index of search_value in source_value. 0 is
returned when no match is found. source_value and search_value must be the
same type, either STRING or BYTES.
If position is specified, the search starts at this position in
source_value, otherwise it starts at the beginning of source_value. If
position is negative, the function searches backwards from the end of
source_value, with -1 indicating the last character. position cannot be 0.
If occurrence is specified, the search returns the position of a specific
instance of search_value in source_value, otherwise it returns the index of
the first occurrence. If occurrence is greater than the number of matches
found, 0 is returned. For occurrence > 1, the function searches for
overlapping occurrences, in other words, the function searches for additional
occurrences beginning with the second character in the previous occurrence.
occurrence cannot be 0 or negative.
Return type
INT64
Examples
WITH example AS
(SELECT 'banana' as source_value, 'an' as search_value, 1 as position, 1 as
occurrence UNION ALL
SELECT 'banana' as source_value, 'an' as search_value, 1 as position, 2 as
occurrence UNION ALL
SELECT 'banana' as source_value, 'an' as search_value, 1 as position, 3 as
occurrence UNION ALL
SELECT 'banana' as source_value, 'an' as search_value, 3 as position, 1 as
occurrence UNION ALL
SELECT 'banana' as source_value, 'an' as search_value, -1 as position, 1 as
occurrence UNION ALL
SELECT 'banana' as source_value, 'an' as search_value, -3 as position, 1 as
occurrence UNION ALL
SELECT 'banana' as source_value, 'ann' as search_value, 1 as position, 1 as
occurrence UNION ALL
SELECT 'helloooo' as source_value, 'oo' as search_value, 1 as position, 1 as
occurrence UNION ALL
SELECT 'helloooo' as source_value, 'oo' as search_value, 1 as position, 2 as
occurrence
)
SELECT source_value, search_value, position, occurrence, INSTR(source_value,
search_value, position, occurrence) AS instr
FROM example;
+--------------+--------------+----------+------------+-------+
| source_value | search_value | position | occurrence | instr |
+--------------+--------------+----------+------------+-------+
| banana | an | 1 | 1 | 2 |
| banana | an | 1 | 2 | 4 |
| banana | an | 1 | 3 | 0 |
| banana | an | 3 | 1 | 4 |
| banana | an | -1 | 1 | 4 |
| banana | an | -3 | 1 | 4 |
| banana | ann | 1 | 1 | 0 |
| helloooo | oo | 1 | 1 | 5 |
| helloooo | oo | 1 | 2 | 6 |
+--------------+--------------+----------+------------+-------+
LEFT
LEFT(value, length)
Description
Returns a STRING or BYTES value that consists of the specified
number of leftmost characters or bytes from value. The length is an
INT64 that specifies the length of the returned
value. If value is of type BYTES, length is the number of leftmost bytes
to return. If value is STRING, length is the number of leftmost characters
to return.
If length is 0, an empty STRING or BYTES value will be
returned. If length is negative, an error will be returned. If length
exceeds the number of characters or bytes from value, the original value
will be returned.
Return type
STRING or BYTES
Examples
WITH examples AS
(SELECT 'apple' as example
UNION ALL
SELECT 'banana' as example
UNION ALL
SELECT 'абвгд' as example
)
SELECT example, LEFT(example, 3) AS left_example
FROM examples;
+---------+--------------+
| example | left_example |
+---------+--------------+
| apple | app |
| banana | ban |
| абвгд | абв |
+---------+--------------+
WITH examples AS
(SELECT b'apple' as example
UNION ALL
SELECT b'banana' as example
UNION ALL
SELECT b'\xab\xcd\xef\xaa\xbb' as example
)
SELECT example, LEFT(example, 3) AS left_example
FROM examples;
-- Note that the result of LEFT is of type BYTES, displayed as a base64-encoded string.
+----------+--------------+
| example | left_example |
+----------+--------------+
| YXBwbGU= | YXBw |
| YmFuYW5h | YmFu |
| q83vqrs= | q83v |
+----------+--------------+
LENGTH
LENGTH(value)
Description
Returns the length of the STRING or BYTES value. The returned
value is in characters for STRING arguments and in bytes for the BYTES
argument.
Return type
INT64
Examples
WITH example AS
(SELECT "абвгд" AS characters)
SELECT
characters,
LENGTH(characters) AS string_example,
LENGTH(CAST(characters AS BYTES)) AS bytes_example
FROM example;
+------------+----------------+---------------+
| characters | string_example | bytes_example |
+------------+----------------+---------------+
| абвгд | 5 | 10 |
+------------+----------------+---------------+
LPAD
LPAD(original_value, return_length[, pattern])
Description
Returns a STRING or BYTES value that consists of original_value prepended
with pattern. The return_length is an INT64 that
specifies the length of the returned value. If original_value is of type
BYTES, return_length is the number of bytes. If original_value is
of type STRING, return_length is the number of characters.
The default value of pattern is a blank space.
Both original_value and pattern must be the same data type.
If return_length is less than or equal to the original_value length, this
function returns the original_value value, truncated to the value of
return_length. For example, LPAD("hello world", 7); returns "hello w".
If original_value, return_length, or pattern is NULL, this function
returns NULL.
This function returns an error if:
return_lengthis negativepatternis empty
Return type
STRING or BYTES
Examples
SELECT t, len, FORMAT("%T", LPAD(t, len)) AS LPAD FROM UNNEST([
STRUCT('abc' AS t, 5 AS len),
('abc', 2),
('例子', 4)
]);
+------+-----+----------+
| t | len | LPAD |
|------|-----|----------|
| abc | 5 | " abc" |
| abc | 2 | "ab" |
| 例子 | 4 | " 例子" |
+------+-----+----------+
SELECT t, len, pattern, FORMAT("%T", LPAD(t, len, pattern)) AS LPAD FROM UNNEST([
STRUCT('abc' AS t, 8 AS len, 'def' AS pattern),
('abc', 5, '-'),
('例子', 5, '中文')
]);
+------+-----+---------+--------------+
| t | len | pattern | LPAD |
|------|-----|---------|--------------|
| abc | 8 | def | "defdeabc" |
| abc | 5 | - | "--abc" |
| 例子 | 5 | 中文 | "中文中例子" |
+------+-----+---------+--------------+
SELECT FORMAT("%T", t) AS t, len, FORMAT("%T", LPAD(t, len)) AS LPAD FROM UNNEST([
STRUCT(b'abc' AS t, 5 AS len),
(b'abc', 2),
(b'\xab\xcd\xef', 4)
]);
+-----------------+-----+------------------+
| t | len | LPAD |
|-----------------|-----|------------------|
| b"abc" | 5 | b" abc" |
| b"abc" | 2 | b"ab" |
| b"\xab\xcd\xef" | 4 | b" \xab\xcd\xef" |
+-----------------+-----+------------------+
SELECT
FORMAT("%T", t) AS t,
len,
FORMAT("%T", pattern) AS pattern,
FORMAT("%T", LPAD(t, len, pattern)) AS LPAD
FROM UNNEST([
STRUCT(b'abc' AS t, 8 AS len, b'def' AS pattern),
(b'abc', 5, b'-'),
(b'\xab\xcd\xef', 5, b'\x00')
]);
+-----------------+-----+---------+-------------------------+
| t | len | pattern | LPAD |
|-----------------|-----|---------|-------------------------|
| b"abc" | 8 | b"def" | b"defdeabc" |
| b"abc" | 5 | b"-" | b"--abc" |
| b"\xab\xcd\xef" | 5 | b"\x00" | b"\x00\x00\xab\xcd\xef" |
+-----------------+-----+---------+-------------------------+
LOWER
LOWER(value)
Description
For STRING arguments, returns the original string with all alphabetic
characters in lowercase. Mapping between lowercase and uppercase is done
according to the
Unicode Character Database
without taking into account language-specific mappings.
For BYTES arguments, the argument is treated as ASCII text, with all bytes
greater than 127 left intact.
Return type
STRING or BYTES
Examples
WITH items AS
(SELECT
"FOO" as item
UNION ALL
SELECT
"BAR" as item
UNION ALL
SELECT
"BAZ" as item)
SELECT
LOWER(item) AS example
FROM items;
+---------+
| example |
+---------+
| foo |
| bar |
| baz |
+---------+
LTRIM
LTRIM(value1[, value2])
Description
Identical to TRIM, but only removes leading characters.
Return type
STRING or BYTES
Examples
WITH items AS
(SELECT " apple " as item
UNION ALL
SELECT " banana " as item
UNION ALL
SELECT " orange " as item)
SELECT
CONCAT("#", LTRIM(item), "#") as example
FROM items;
+-------------+
| example |
+-------------+
| #apple # |
| #banana # |
| #orange # |
+-------------+
WITH items AS
(SELECT "***apple***" as item
UNION ALL
SELECT "***banana***" as item
UNION ALL
SELECT "***orange***" as item)
SELECT
LTRIM(item, "*") as example
FROM items;
+-----------+
| example |
+-----------+
| apple*** |
| banana*** |
| orange*** |
+-----------+
WITH items AS
(SELECT "xxxapplexxx" as item
UNION ALL
SELECT "yyybananayyy" as item
UNION ALL
SELECT "zzzorangezzz" as item
UNION ALL
SELECT "xyzpearxyz" as item)
SELECT
LTRIM(item, "xyz") as example
FROM items;
+-----------+
| example |
+-----------+
| applexxx |
| bananayyy |
| orangezzz |
| pearxyz |
+-----------+
NORMALIZE
NORMALIZE(value[, normalization_mode])
Description
Takes a string value and returns it as a normalized string. If you do not
provide a normalization mode, NFC is used.
Normalization is used to ensure that two strings are equivalent. Normalization is often used in situations in which two strings render the same on the screen but have different Unicode code points.
NORMALIZE supports four optional normalization modes:
| Value | Name | Description |
|---|---|---|
NFC |
Normalization Form Canonical Composition | Decomposes and recomposes characters by canonical equivalence. |
NFKC |
Normalization Form Compatibility Composition | Decomposes characters by compatibility, then recomposes them by canonical equivalence. |
NFD |
Normalization Form Canonical Decomposition | Decomposes characters by canonical equivalence, and multiple combining characters are arranged in a specific order. |
NFKD |
Normalization Form Compatibility Decomposition | Decomposes characters by compatibility, and multiple combining characters are arranged in a specific order. |
Return type
STRING
Examples
SELECT a, b, a = b as normalized
FROM (SELECT NORMALIZE('\u00ea') as a, NORMALIZE('\u0065\u0302') as b);
+---+---+------------+
| a | b | normalized |
+---+---+------------+
| ê | ê | true |
+---+---+------------+
The following example normalizes different space characters.
WITH EquivalentNames AS (
SELECT name
FROM UNNEST([
'Jane\u2004Doe',
'John\u2004Smith',
'Jane\u2005Doe',
'Jane\u2006Doe',
'John Smith']) AS name
)
SELECT
NORMALIZE(name, NFKC) AS normalized_name,
COUNT(*) AS name_count
FROM EquivalentNames
GROUP BY 1;
+-----------------+------------+
| normalized_name | name_count |
+-----------------+------------+
| John Smith | 2 |
| Jane Doe | 3 |
+-----------------+------------+
NORMALIZE_AND_CASEFOLD
NORMALIZE_AND_CASEFOLD(value[, normalization_mode])
Description
Takes a string value and returns it as a normalized string with normalization.
Normalization is used to ensure that two strings are equivalent. Normalization is often used in situations in which two strings render the same on the screen but have different Unicode code points.
Case folding is used for the caseless
comparison of strings. If you need to compare strings and case should not be
considered, use NORMALIZE_AND_CASEFOLD, otherwise use
NORMALIZE.
NORMALIZE_AND_CASEFOLD supports four optional normalization modes:
| Value | Name | Description |
|---|---|---|
NFC |
Normalization Form Canonical Composition | Decomposes and recomposes characters by canonical equivalence. |
NFKC |
Normalization Form Compatibility Composition | Decomposes characters by compatibility, then recomposes them by canonical equivalence. |
NFD |
Normalization Form Canonical Decomposition | Decomposes characters by canonical equivalence, and multiple combining characters are arranged in a specific order. |
NFKD |
Normalization Form Compatibility Decomposition | Decomposes characters by compatibility, and multiple combining characters are arranged in a specific order. |
Return type
STRING
Examples
SELECT
a, b,
NORMALIZE(a) = NORMALIZE(b) as normalized,
NORMALIZE_AND_CASEFOLD(a) = NORMALIZE_AND_CASEFOLD(b) as normalized_with_case_folding
FROM (SELECT 'The red barn' AS a, 'The Red Barn' AS b);
+--------------+--------------+------------+------------------------------+
| a | b | normalized | normalized_with_case_folding |
+--------------+--------------+------------+------------------------------+
| The red barn | The Red Barn | false | true |
+--------------+--------------+------------+------------------------------+
WITH Strings AS (
SELECT '\u2168' AS a, 'IX' AS b UNION ALL
SELECT '\u0041\u030A', '\u00C5'
)
SELECT a, b,
NORMALIZE_AND_CASEFOLD(a, NFD)=NORMALIZE_AND_CASEFOLD(b, NFD) AS nfd,
NORMALIZE_AND_CASEFOLD(a, NFC)=NORMALIZE_AND_CASEFOLD(b, NFC) AS nfc,
NORMALIZE_AND_CASEFOLD(a, NFKD)=NORMALIZE_AND_CASEFOLD(b, NFKD) AS nkfd,
NORMALIZE_AND_CASEFOLD(a, NFKC)=NORMALIZE_AND_CASEFOLD(b, NFKC) AS nkfc
FROM Strings;
+---+----+-------+-------+------+------+
| a | b | nfd | nfc | nkfd | nkfc |
+---+----+-------+-------+------+------+
| Ⅸ | IX | false | false | true | true |
| Å | Å | true | true | true | true |
+---+----+-------+-------+------+------+
OCTET_LENGTH
OCTET_LENGTH(value)
Alias for BYTE_LENGTH.
REGEXP_CONTAINS
REGEXP_CONTAINS(value, regexp)
Description
Returns TRUE if value is a partial match for the regular expression,
regexp.
If the regexp argument is invalid, the function returns an error.
You can search for a full match by using ^ (beginning of text) and $ (end of
text). Due to regular expression operator precedence, it is good practice to use
parentheses around everything between ^ and $.
Return type
BOOL
Examples
SELECT
email,
REGEXP_CONTAINS(email, r"@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+") AS is_valid
FROM
(SELECT
["foo@example.com", "bar@example.org", "www.example.net"]
AS addresses),
UNNEST(addresses) AS email;
+-----------------+----------+
| email | is_valid |
+-----------------+----------+
| foo@example.com | true |
| bar@example.org | true |
| www.example.net | false |
+-----------------+----------+
# Performs a full match, using ^ and $. Due to regular expression operator
# precedence, it is good practice to use parentheses around everything between ^
# and $.
SELECT
email,
REGEXP_CONTAINS(email, r"^([\w.+-]+@foo\.com|[\w.+-]+@bar\.org)$")
AS valid_email_address,
REGEXP_CONTAINS(email, r"^[\w.+-]+@foo\.com|[\w.+-]+@bar\.org$")
AS without_parentheses
FROM
(SELECT
["a@foo.com", "a@foo.computer", "b@bar.org", "!b@bar.org", "c@buz.net"]
AS addresses),
UNNEST(addresses) AS email;
+----------------+---------------------+---------------------+
| email | valid_email_address | without_parentheses |
+----------------+---------------------+---------------------+
| a@foo.com | true | true |
| a@foo.computer | false | true |
| b@bar.org | true | true |
| !b@bar.org | false | true |
| c@buz.net | false | false |
+----------------+---------------------+---------------------+
REGEXP_EXTRACT
REGEXP_EXTRACT(value, regexp[, position[, occurrence]])
Description
Returns the substring in value that matches the regular expression, regexp.
Returns NULL if there is no match.
If the regular expression contains a capturing group, the function returns the substring that is matched by that capturing group. If the expression does not contain a capturing group, the function returns the entire matching substring.
If position is specified, the search starts at this
position in value, otherwise it starts at the beginning of value. The
position must be a positive integer and cannot be 0. If position is greater
than the length of value, NULL is returned.
If occurrence is specified, the search returns a specific occurrence of the
regexp in value, otherwise returns the first match. If occurrence is
greater than the number of matches found, NULL is returned. For
occurrence > 1, the function searches for additional occurrences beginning
with the character following the previous occurrence.
Returns an error if:
- The regular expression is invalid
- The regular expression has more than one capturing group
- The
positionis not a positive integer - The
occurrenceis not a positive integer
Return type
STRING or BYTES
Examples
WITH email_addresses AS
(SELECT "foo@example.com" as email
UNION ALL
SELECT "bar@example.org" as email
UNION ALL
SELECT "baz@example.net" as email)
SELECT
REGEXP_EXTRACT(email, r"^[a-zA-Z0-9_.+-]+")
AS user_name
FROM email_addresses;
+-----------+
| user_name |
+-----------+
| foo |
| bar |
| baz |
+-----------+
WITH email_addresses AS
(SELECT "foo@example.com" as email
UNION ALL
SELECT "bar@example.org" as email
UNION ALL
SELECT "baz@example.net" as email)
SELECT
REGEXP_EXTRACT(email, r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.([a-zA-Z0-9-.]+$)")
AS top_level_domain
FROM email_addresses;
+------------------+
| top_level_domain |
+------------------+
| com |
| org |
| net |
+------------------+
WITH example AS
(SELECT 'Hello Helloo and Hellooo' AS value, 'H?ello+' AS regex, 1 as position,
1 AS occurrence UNION ALL
SELECT 'Hello Helloo and Hellooo', 'H?ello+', 1, 2 UNION ALL
SELECT 'Hello Helloo and Hellooo', 'H?ello+', 1, 3 UNION ALL
SELECT 'Hello Helloo and Hellooo', 'H?ello+', 1, 4 UNION ALL
SELECT 'Hello Helloo and Hellooo', 'H?ello+', 2, 1 UNION ALL
SELECT 'Hello Helloo and Hellooo', 'H?ello+', 3, 1 UNION ALL
SELECT 'Hello Helloo and Hellooo', 'H?ello+', 3, 2 UNION ALL
SELECT 'Hello Helloo and Hellooo', 'H?ello+', 3, 3 UNION ALL
SELECT 'Hello Helloo and Hellooo', 'H?ello+', 20, 1 UNION ALL
SELECT 'cats&dogs&rabbits' ,'\\w+&', 1, 2 UNION ALL
SELECT 'cats&dogs&rabbits', '\\w+&', 2, 3
)
SELECT value, regex, position, occurrence, REGEXP_EXTRACT(value, regex,
position, occurrence) AS regexp_value FROM example;
+--------------------------+---------+----------+------------+--------------+
| value | regex | position | occurrence | regexp_value |
+--------------------------+---------+----------+------------+--------------+
| Hello Helloo and Hellooo | H?ello+ | 1 | 1 | Hello |
| Hello Helloo and Hellooo | H?ello+ | 1 | 2 | Helloo |
| Hello Helloo and Hellooo | H?ello+ | 1 | 3 | Hellooo |
| Hello Helloo and Hellooo | H?ello+ | 1 | 4 | NULL |
| Hello Helloo and Hellooo | H?ello+ | 2 | 1 | ello |
| Hello Helloo and Hellooo | H?ello+ | 3 | 1 | Helloo |
| Hello Helloo and Hellooo | H?ello+ | 3 | 2 | Hellooo |
| Hello Helloo and Hellooo | H?ello+ | 3 | 3 | NULL |
| Hello Helloo and Hellooo | H?ello+ | 20 | 1 | NULL |
| cats&dogs&rabbits | \w+& | 1 | 2 | dogs& |
| cats&dogs&rabbits | \w+& | 2 | 3 | NULL |
+--------------------------+---------+----------+------------+--------------+
REGEXP_EXTRACT_ALL
REGEXP_EXTRACT_ALL(value, regexp)
Description
Returns an array of all substrings of value that match the regular expression,
regexp.
The REGEXP_EXTRACT_ALL function only returns non-overlapping matches. For
example, using this function to extract ana from banana returns only one
substring, not two.
Return type
An ARRAY of either STRINGs or BYTES
Examples
WITH code_markdown AS
(SELECT "Try `function(x)` or `function(y)`" as code)
SELECT
REGEXP_EXTRACT_ALL(code, "`(.+?)`") AS example
FROM code_markdown;
+----------------------------+
| example |
+----------------------------+
| [function(x), function(y)] |
+----------------------------+
REGEXP_INSTR
REGEXP_INSTR(source_value, regexp [, position[, occurrence, [occurrence_position]]])
Description
Returns the lowest 1-based index of a regular expression, regexp, in
source_value. Returns 0 when no match is found or the regular expression
is empty. Returns an error if the regular expression is invalid or has more than
one capturing group. source_value and regexp must be the same type, either
STRING or BYTES.
If position is specified, the search starts at this position in
source_value, otherwise it starts at the beginning of source_value. If
position is negative, the function searches backwards from the end of
source_value, with -1 indicating the last character. position cannot be 0.
If occurrence is specified, the search returns the position of a specific
instance of regexp in source_value, otherwise it returns the index of
the first occurrence. If occurrence is greater than the number of matches
found, 0 is returned. For occurrence > 1, the function searches for
overlapping occurrences, in other words, the function searches for additional
occurrences beginning with the second character in the previous occurrence.
occurrence cannot be 0 or negative.
You can optionally use occurrence_position to specify where a position
in relation to an occurrence starts. Your choices are:
+ 0: Returns the beginning position of the occurrence.
+ 1: Returns the first position following the end of the occurrence. If the
end of the occurrence is also the end of the input, one off the
end of the occurrence is returned. For example, length of a string + 1.
Return type
INT64
Examples
WITH example AS (
SELECT 'ab@gmail.com' AS source_value, '@[^.]*' AS regexp UNION ALL
SELECT 'ab@mail.com', '@[^.]*' UNION ALL
SELECT 'abc@gmail.com', '@[^.]*' UNION ALL
SELECT 'abc.com', '@[^.]*')
SELECT source_value, regexp, REGEXP_INSTR(source_value, regexp) AS instr
FROM example;
+---------------+--------+-------+
| source_value | regexp | instr |
+---------------+--------+-------+
| ab@gmail.com | @[^.]* | 3 |
| ab@mail.com | @[^.]* | 3 |
| abc@gmail.com | @[^.]* | 4 |
| abc.com | @[^.]* | 0 |
+---------------+--------+-------+
WITH example AS (
SELECT 'a@gmail.com b@gmail.com' AS source_value, '@[^.]*' AS regexp, 1 AS position UNION ALL
SELECT 'a@gmail.com b@gmail.com', '@[^.]*', 2 UNION ALL
SELECT 'a@gmail.com b@gmail.com', '@[^.]*', 3 UNION ALL
SELECT 'a@gmail.com b@gmail.com', '@[^.]*', 4)
SELECT
source_value, regexp, position,
REGEXP_INSTR(source_value, regexp, position) AS instr
FROM example;
+-------------------------+--------+----------+-------+
| source_value | regexp | position | instr |
+-------------------------+--------+----------+-------+
| a@gmail.com b@gmail.com | @[^.]* | 1 | 2 |
| a@gmail.com b@gmail.com | @[^.]* | 2 | 2 |
| a@gmail.com b@gmail.com | @[^.]* | 3 | 14 |
| a@gmail.com b@gmail.com | @[^.]* | 4 | 14 |
+-------------------------+--------+----------+-------+
WITH example AS (
SELECT 'a@gmail.com b@gmail.com c@gmail.com' AS source_value,
'@[^.]*' AS regexp, 1 AS position, 1 AS occurrence UNION ALL
SELECT 'a@gmail.com b@gmail.com c@gmail.com', '@[^.]*', 1, 2 UNION ALL
SELECT 'a@gmail.com b@gmail.com c@gmail.com', '@[^.]*', 1, 3)
SELECT
source_value, regexp, position, occurrence,
REGEXP_INSTR(source_value, regexp, position, occurrence) AS instr
FROM example;
+-------------------------------------+--------+----------+------------+-------+
| source_value | regexp | position | occurrence | instr |
+-------------------------------------+--------+----------+------------+-------+
| a@gmail.com b@gmail.com c@gmail.com | @[^.]* | 1 | 1 | 2 |
| a@gmail.com b@gmail.com c@gmail.com | @[^.]* | 1 | 2 | 14 |
| a@gmail.com b@gmail.com c@gmail.com | @[^.]* | 1 | 3 | 26 |
+-------------------------------------+--------+----------+------------+-------+
WITH example AS (
SELECT 'a@gmail.com' AS source_value, '@[^.]*' AS regexp,
1 AS position, 1 AS occurrence, 0 AS o_position UNION ALL
SELECT 'a@gmail.com', '@[^.]*', 1, 1, 1)
SELECT
source_value, regexp, position, occurrence, o_position,
REGEXP_INSTR(source_value, regexp, position, occurrence, o_position) AS instr
FROM example;
+--------------+--------+----------+------------+------------+-------+
| source_value | regexp | position | occurrence | o_position | instr |
+--------------+--------+----------+------------+------------+-------+
| a@gmail.com | @[^.]* | 1 | 1 | 0 | 2 |
| a@gmail.com | @[^.]* | 1 | 1 | 1 | 8 |
+--------------+--------+----------+------------+------------+-------+
REGEXP_REPLACE
REGEXP_REPLACE(value, regexp, replacement)
Description
Returns a STRING where all substrings of value that
match regular expression regexp are replaced with replacement.
You can use backslashed-escaped digits (\1 to \9) within the replacement
argument to insert text matching the corresponding parenthesized group in the
regexp pattern. Use \0 to refer to the entire matching text.
The REGEXP_REPLACE function only replaces non-overlapping matches. For
example, replacing ana within banana results in only one replacement, not
two.
If the regexp argument is not a valid regular expression, this function
returns an error.
Return type
STRING or BYTES
Examples
WITH markdown AS
(SELECT "# Heading" as heading
UNION ALL
SELECT "# Another heading" as heading)
SELECT
REGEXP_REPLACE(heading, r"^# ([a-zA-Z0-9\s]+$)", "<h1>\\1</h1>")
AS html
FROM markdown;
+--------------------------+
| html |
+--------------------------+
| <h1>Heading</h1> |
| <h1>Another heading</h1> |
+--------------------------+
REGEXP_SUBSTR
REGEXP_SUBSTR(value, regexp[, position[, occurrence]])
Description
Synonym for REGEXP_EXTRACT.
Return type
STRING or BYTES
Examples
WITH example AS
(SELECT 'Hello World Helloo' AS value, 'H?ello+' AS regex, 1 AS position, 1 AS
occurrence
)
SELECT value, regex, position, occurrence, REGEXP_SUBSTR(value, regex,
position, occurrence) AS regexp_value FROM example;
+--------------------+---------+----------+------------+--------------+
| value | regex | position | occurrence | regexp_value |
+--------------------+---------+----------+------------+--------------+
| Hello World Helloo | H?ello+ | 1 | 1 | Hello |
+--------------------+---------+----------+------------+--------------+
REPLACE
REPLACE(original_value, from_value, to_value)
Description
Replaces all occurrences of from_value with to_value in original_value.
If from_value is empty, no replacement is made.
Return type
STRING or BYTES
Examples
WITH desserts AS
(SELECT "apple pie" as dessert
UNION ALL
SELECT "blackberry pie" as dessert
UNION ALL
SELECT "cherry pie" as dessert)
SELECT
REPLACE (dessert, "pie", "cobbler") as example
FROM desserts;
+--------------------+
| example |
+--------------------+
| apple cobbler |
| blackberry cobbler |
| cherry cobbler |
+--------------------+
REPEAT
REPEAT(original_value, repetitions)
Description
Returns a STRING or BYTES value that consists of original_value, repeated.
The repetitions parameter specifies the number of times to repeat
original_value. Returns NULL if either original_value or repetitions
are NULL.
This function returns an error if the repetitions value is negative.
Return type
STRING or BYTES
Examples
SELECT t, n, REPEAT(t, n) AS REPEAT FROM UNNEST([
STRUCT('abc' AS t, 3 AS n),
('例子', 2),
('abc', null),
(null, 3)
]);
+------+------+-----------+
| t | n | REPEAT |
|------|------|-----------|
| abc | 3 | abcabcabc |
| 例子 | 2 | 例子例子 |
| abc | NULL | NULL |
| NULL | 3 | NULL |
+------+------+-----------+
REVERSE
REVERSE(value)
Description
Returns the reverse of the input STRING or BYTES.
Return type
STRING or BYTES
Examples
WITH example AS (
SELECT "foo" AS sample_string, b"bar" AS sample_bytes UNION ALL
SELECT "абвгд" AS sample_string, b"123" AS sample_bytes
)
SELECT
sample_string,
REVERSE(sample_string) AS reverse_string,
sample_bytes,
REVERSE(sample_bytes) AS reverse_bytes
FROM example;
+---------------+----------------+--------------+---------------+
| sample_string | reverse_string | sample_bytes | reverse_bytes |
+---------------+----------------+--------------+---------------+
| foo | oof | bar | rab |
| абвгд | дгвба | 123 | 321 |
+---------------+----------------+--------------+---------------+
RIGHT
RIGHT(value, length)
Description
Returns a STRING or BYTES value that consists of the specified
number of rightmost characters or bytes from value. The length is an
INT64 that specifies the length of the returned
value. If value is BYTES, length is the number of rightmost bytes to
return. If value is STRING, length is the number of rightmost characters
to return.
If length is 0, an empty STRING or BYTES value will be
returned. If length is negative, an error will be returned. If length
exceeds the number of characters or bytes from value, the original value
will be returned.
Return type
STRING or BYTES
Examples
WITH examples AS
(SELECT 'apple' as example
UNION ALL
SELECT 'banana' as example
UNION ALL
SELECT 'абвгд' as example
)
SELECT example, RIGHT(example, 3) AS right_example
FROM examples;
+---------+---------------+
| example | right_example |
+---------+---------------+
| apple | ple |
| banana | ana |
| абвгд | вгд |
+---------+---------------+
WITH examples AS
(SELECT b'apple' as example
UNION ALL
SELECT b'banana' as example
UNION ALL
SELECT b'\xab\xcd\xef\xaa\xbb' as example
)
SELECT example, RIGHT(example, 3) AS right_example
FROM examples;
-- Note that the result of RIGHT is of type BYTES, displayed as a base64-encoded string.
+----------+---------------+
| example | right_example |
+----------+---------------+
| YXBwbGU= | cGxl |
| YmFuYW5h | YW5h |
| q83vqrs= | 76q7 |
+----------+---------------+
RPAD
RPAD(original_value, return_length[, pattern])
Description
Returns a STRING or BYTES value that consists of original_value appended
with pattern. The return_length parameter is an
INT64 that specifies the length of the
returned value. If original_value is BYTES,
return_length is the number of bytes. If original_value is STRING,
return_length is the number of characters.
The default value of pattern is a blank space.
Both original_value and pattern must be the same data type.
If return_length is less than or equal to the original_value length, this
function returns the original_value value, truncated to the value of
return_length. For example, RPAD("hello world", 7); returns "hello w".
If original_value, return_length, or pattern is NULL, this function
returns NULL.
This function returns an error if:
return_lengthis negativepatternis empty
Return type
STRING or BYTES
Examples
SELECT t, len, FORMAT("%T", RPAD(t, len)) AS RPAD FROM UNNEST([
STRUCT('abc' AS t, 5 AS len),
('abc', 2),
('例子', 4)
]);
+------+-----+----------+
| t | len | RPAD |
|------|-----|----------|
| abc | 5 | "abc " |
| abc | 2 | "ab" |
| 例子 | 4 | "例子 " |
+------+-----+----------+
SELECT t, len, pattern, FORMAT("%T", RPAD(t, len, pattern)) AS RPAD FROM UNNEST([
STRUCT('abc' AS t, 8 AS len, 'def' AS pattern),
('abc', 5, '-'),
('例子', 5, '中文')
]);
+------+-----+---------+--------------+
| t | len | pattern | RPAD |
|------|-----|---------|--------------|
| abc | 8 | def | "abcdefde" |
| abc | 5 | - | "abc--" |
| 例子 | 5 | 中文 | "例子中文中" |
+------+-----+---------+--------------+
SELECT FORMAT("%T", t) AS t, len, FORMAT("%T", RPAD(t, len)) AS RPAD FROM UNNEST([
STRUCT(b'abc' AS t, 5 AS len),
(b'abc', 2),
(b'\xab\xcd\xef', 4)
]);
+-----------------+-----+------------------+
| t | len | RPAD |
|-----------------|-----|------------------|
| b"abc" | 5 | b"abc " |
| b"abc" | 2 | b"ab" |
| b"\xab\xcd\xef" | 4 | b"\xab\xcd\xef " |
+-----------------+-----+------------------+
SELECT
FORMAT("%T", t) AS t,
len,
FORMAT("%T", pattern) AS pattern,
FORMAT("%T", RPAD(t, len, pattern)) AS RPAD
FROM UNNEST([
STRUCT(b'abc' AS t, 8 AS len, b'def' AS pattern),
(b'abc', 5, b'-'),
(b'\xab\xcd\xef', 5, b'\x00')
]);
+-----------------+-----+---------+-------------------------+
| t | len | pattern | RPAD |
|-----------------|-----|---------|-------------------------|
| b"abc" | 8 | b"def" | b"abcdefde" |
| b"abc" | 5 | b"-" | b"abc--" |
| b"\xab\xcd\xef" | 5 | b"\x00" | b"\xab\xcd\xef\x00\x00" |
+-----------------+-----+---------+-------------------------+
RTRIM
RTRIM(value1[, value2])
Description
Identical to TRIM, but only removes trailing characters.
Return type
STRING or BYTES
Examples
WITH items AS
(SELECT "***apple***" as item
UNION ALL
SELECT "***banana***" as item
UNION ALL
SELECT "***orange***" as item)
SELECT
RTRIM(item, "*") as example
FROM items;
+-----------+
| example |
+-----------+
| ***apple |
| ***banana |
| ***orange |
+-----------+
WITH items AS
(SELECT "applexxx" as item
UNION ALL
SELECT "bananayyy" as item
UNION ALL
SELECT "orangezzz" as item
UNION ALL
SELECT "pearxyz" as item)
SELECT
RTRIM(item, "xyz") as example
FROM items;
+---------+
| example |
+---------+
| apple |
| banana |
| orange |
| pear |
+---------+
SAFE_CONVERT_BYTES_TO_STRING
SAFE_CONVERT_BYTES_TO_STRING(value)
Description
Converts a sequence of BYTES to a STRING. Any invalid UTF-8 characters are
replaced with the Unicode replacement character, U+FFFD.
Return type
STRING
Examples
The following statement returns the Unicode replacement character, �.
SELECT SAFE_CONVERT_BYTES_TO_STRING(b'\xc2') as safe_convert;
SOUNDEX
SOUNDEX(value)
Description
Returns a STRING that represents the
Soundex code for value.
SOUNDEX produces a phonetic representation of a string. It indexes words by sound, as pronounced in English. It is typically used to help determine whether two strings, such as the family names Levine and Lavine, or the words to and too, have similar English-language pronunciation.
The result of the SOUNDEX consists of a letter followed by 3 digits. Non-latin
characters are ignored. If the remaining string is empty after removing
non-Latin characters, an empty STRING is returned.
Return type
STRING
Examples
WITH example AS (
SELECT 'Ashcraft' AS value UNION ALL
SELECT 'Raven' AS value UNION ALL
SELECT 'Ribbon' AS value UNION ALL
SELECT 'apple' AS value UNION ALL
SELECT 'Hello world!' AS value UNION ALL
SELECT ' H3##!@llo w00orld!' AS value UNION ALL
SELECT '#1' AS value UNION ALL
SELECT NULL AS value
)
SELECT value, SOUNDEX(value) AS soundex
FROM example;
+----------------------+---------+
| value | soundex |
+----------------------+---------+
| Ashcraft | A261 |
| Raven | R150 |
| Ribbon | R150 |
| apple | a140 |
| Hello world! | H464 |
| H3##!@llo w00orld! | H464 |
| #1 | |
| NULL | NULL |
+----------------------+---------+
SPLIT
SPLIT(value[, delimiter])
Description
Splits value using the delimiter argument.
For STRING, the default delimiter is the comma ,.
For BYTES, you must specify a delimiter.
Splitting on an empty delimiter produces an array of UTF-8 characters for
STRING values, and an array of BYTES for BYTES values.
Splitting an empty STRING returns an
ARRAY with a single empty
STRING.
Return type
ARRAY of type STRING or
ARRAY of type BYTES
Examples
WITH letters AS
(SELECT "" as letter_group
UNION ALL
SELECT "a" as letter_group
UNION ALL
SELECT "b c d" as letter_group)
SELECT SPLIT(letter_group, " ") as example
FROM letters;
+----------------------+
| example |
+----------------------+
| [] |
| [a] |
| [b, c, d] |
+----------------------+
STARTS_WITH
STARTS_WITH(value1, value2)
Description
Takes two STRING or BYTES values. Returns TRUE if the second value is a
prefix of the first.
Return type
BOOL
Examples
WITH items AS
(SELECT "foo" as item
UNION ALL
SELECT "bar" as item
UNION ALL
SELECT "baz" as item)
SELECT
STARTS_WITH(item, "b") as example
FROM items;
+---------+
| example |
+---------+
| False |
| True |
| True |
+---------+
STRPOS
STRPOS(value1, value2)
Description
Takes two STRING or BYTES values. Returns the 1-based index of the first
occurrence of value2 inside value1. Returns 0 if value2 is not found.
Return type
INT64
Examples
WITH email_addresses AS
(SELECT
"foo@example.com" AS email_address
UNION ALL
SELECT
"foobar@example.com" AS email_address
UNION ALL
SELECT
"foobarbaz@example.com" AS email_address
UNION ALL
SELECT
"quxexample.com" AS email_address)
SELECT
STRPOS(email_address, "@") AS example
FROM email_addresses;
+---------+
| example |
+---------+
| 4 |
| 7 |
| 10 |
| 0 |
+---------+
SUBSTR
SUBSTR(value, position[, length])
Description
Returns a substring of the supplied STRING or BYTES value. The
position argument is an integer specifying the starting position of the
substring, with position = 1 indicating the first character or byte. The
length argument is the maximum number of characters for STRING arguments,
or bytes for BYTES arguments.
If position is negative, the function counts from the end of value,
with -1 indicating the last character.
If position is a position off the left end of the
STRING (position = 0 or position < -LENGTH(value)), the function
starts from position = 1. If length exceeds the length of value, the
function returns fewer than length characters.
If length is less than 0, the function returns an error.
Return type
STRING or BYTES
Examples
WITH items AS
(SELECT "apple" as item
UNION ALL
SELECT "banana" as item
UNION ALL
SELECT "orange" as item)
SELECT
SUBSTR(item, 2) as example
FROM items;
+---------+
| example |
+---------+
| pple |
| anana |
| range |
+---------+
WITH items AS
(SELECT "apple" as item
UNION ALL
SELECT "banana" as item
UNION ALL
SELECT "orange" as item)
SELECT
SUBSTR(item, 2, 2) as example
FROM items;
+---------+
| example |
+---------+
| pp |
| an |
| ra |
+---------+
WITH items AS
(SELECT "apple" as item
UNION ALL
SELECT "banana" as item
UNION ALL
SELECT "orange" as item)
SELECT
SUBSTR(item, -2) as example
FROM items;
+---------+
| example |
+---------+
| le |
| na |
| ge |
+---------+
SUBSTRING
SUBSTRING(value, position[, length])
Alias for SUBSTR.
TO_BASE32
TO_BASE32(bytes_expr)
Description
Converts a sequence of BYTES into a base32-encoded STRING. To convert a
base32-encoded STRING into BYTES, use FROM_BASE32.
Return type
STRING
Example
SELECT TO_BASE32(b'abcde\xFF') AS base32_string;
+------------------+
| base32_string |
+------------------+
| MFRGGZDF74====== |
+------------------+
TO_BASE64
TO_BASE64(bytes_expr)
Description
Converts a sequence of BYTES into a base64-encoded STRING. To convert a
base64-encoded STRING into BYTES, use FROM_BASE64.
There are several base64 encodings in common use that vary in exactly which
alphabet of 65 ASCII characters are used to encode the 64 digits and padding.
See RFC 4648 for details. This
function adds padding and uses the alphabet [A-Za-z0-9+/=].
Return type
STRING
Example
SELECT TO_BASE64(b'\377\340') AS base64_string;
+---------------+
| base64_string |
+---------------+
| /+A= |
+---------------+
To work with an encoding using a different base64 alphabet, you might need to
compose TO_BASE64 with the REPLACE function. For instance, the
base64url url-safe and filename-safe encoding commonly used in web programming
uses -_= as the last characters rather than +/=. To encode a
base64url-encoded string, replace - and _ with + and / respectively.
SELECT REPLACE(REPLACE(TO_BASE64(b"\377\340"), "+", "-"), "/", "_") as websafe_base64;
+----------------+
| websafe_base64 |
+----------------+
| _-A= |
+----------------+
TO_CODE_POINTS
TO_CODE_POINTS(value)
Description
Takes a value and returns an array of
INT64.
- If
valueis aSTRING, each element in the returned array represents a code point. Each code point falls within the range of [0, 0xD7FF] and [0xE000, 0x10FFFF]. - If
valueisBYTES, each element in the array is an extended ASCII character value in the range of [0, 255].
To convert from an array of code points to a STRING or BYTES, see
CODE_POINTS_TO_STRING or
CODE_POINTS_TO_BYTES.
Return type
ARRAY of INT64
Examples
The following example gets the code points for each element in an array of words.
SELECT word, TO_CODE_POINTS(word) AS code_points
FROM UNNEST(['foo', 'bar', 'baz', 'giraffe', 'llama']) AS word;
+---------+------------------------------------+
| word | code_points |
+---------+------------------------------------+
| foo | [102, 111, 111] |
| bar | [98, 97, 114] |
| baz | [98, 97, 122] |
| giraffe | [103, 105, 114, 97, 102, 102, 101] |
| llama | [108, 108, 97, 109, 97] |
+---------+------------------------------------+
The following example converts integer representations of BYTES to their
corresponding ASCII character values.
SELECT word, TO_CODE_POINTS(word) AS bytes_value_as_integer
FROM UNNEST([b'\x00\x01\x10\xff', b'\x66\x6f\x6f']) AS word;
+------------------+------------------------+
| word | bytes_value_as_integer |
+------------------+------------------------+
| \x00\x01\x10\xff | [0, 1, 16, 255] |
| foo | [102, 111, 111] |
+------------------+------------------------+
The following example demonstrates the difference between a BYTES result and a
STRING result.
SELECT TO_CODE_POINTS(b'Ā') AS b_result, TO_CODE_POINTS('Ā') AS s_result;
+------------+----------+
| b_result | s_result |
+------------+----------+
| [196, 128] | [256] |
+------------+----------+
Notice that the character, Ā, is represented as a two-byte Unicode sequence. As
a result, the BYTES version of TO_CODE_POINTS returns an array with two
elements, while the STRING version returns an array with a single element.
TO_HEX
TO_HEX(bytes)
Description
Converts a sequence of BYTES into a hexadecimal STRING. Converts each byte
in the STRING as two hexadecimal characters in the range
(0..9, a..f). To convert a hexadecimal-encoded
STRING to BYTES, use FROM_HEX.
Return type
STRING
Example
WITH Input AS (
SELECT b'\x00\x01\x02\x03\xAA\xEE\xEF\xFF' AS byte_str UNION ALL
SELECT b'foobar'
)
SELECT byte_str, TO_HEX(byte_str) AS hex_str
FROM Input;
+----------------------------------+------------------+
| byte_string | hex_string |
+----------------------------------+------------------+
| \x00\x01\x02\x03\xaa\xee\xef\xff | 00010203aaeeefff |
| foobar | 666f6f626172 |
+----------------------------------+------------------+
TRANSLATE
TRANSLATE(expression, source_characters, target_characters)
Description
In expression, replaces each character in source_characters with the
corresponding character in target_characters. All inputs must be the same
type, either STRING or BYTES.
- Each character in
expressionis translated at most once. - A character in
expressionthat is not present insource_charactersis left unchanged inexpression. - A character in
source_characterswithout a corresponding character intarget_charactersis omitted from the result. - A duplicate character in
source_charactersresults in an error.
Return type
STRING or BYTES
Examples
WITH example AS (
SELECT 'This is a cookie' AS expression, 'sco' AS source_characters, 'zku' AS
target_characters UNION ALL
SELECT 'A coaster' AS expression, 'co' AS source_characters, 'k' as
target_characters
)
SELECT expression, source_characters, target_characters, TRANSLATE(expression,
source_characters, target_characters) AS translate
FROM example;
+------------------+-------------------+-------------------+------------------+
| expression | source_characters | target_characters | translate |
+------------------+-------------------+-------------------+------------------+
| This is a cookie | sco | zku | Thiz iz a kuukie |
| A coaster | co | k | A kaster |
+------------------+-------------------+-------------------+------------------+
TRIM
TRIM(value1[, value2])
Description
Removes all leading and trailing characters that match value2. If
value2 is not specified, all leading and trailing whitespace characters (as
defined by the Unicode standard) are removed. If the first argument is of type
BYTES, the second argument is required.
If value2 contains more than one character or byte, the function removes all
leading or trailing characters or bytes contained in value2.
Return type
STRING or BYTES
Examples
WITH items AS
(SELECT " apple " as item
UNION ALL
SELECT " banana " as item
UNION ALL
SELECT " orange " as item)
SELECT
CONCAT("#", TRIM(item), "#") as example
FROM items;
+----------+
| example |
+----------+
| #apple# |
| #banana# |
| #orange# |
+----------+
WITH items AS
(SELECT "***apple***" as item
UNION ALL
SELECT "***banana***" as item
UNION ALL
SELECT "***orange***" as item)
SELECT
TRIM(item, "*") as example
FROM items;
+---------+
| example |
+---------+
| apple |
| banana |
| orange |
+---------+
WITH items AS
(SELECT "xxxapplexxx" as item
UNION ALL
SELECT "yyybananayyy" as item
UNION ALL
SELECT "zzzorangezzz" as item
UNION ALL
SELECT "xyzpearxyz" as item)
SELECT
TRIM(item, "xyz") as example
FROM items;
+---------+
| example |
+---------+
| apple |
| banana |
| orange |
| pear |
+---------+
UNICODE
UNICODE(value)
Description
Returns the Unicode code point for the first character in
value. Returns 0 if value is empty, or if the resulting Unicode code
point is 0.
Return type
INT64
Examples
SELECT UNICODE('âbcd') as A, UNICODE('â') as B, UNICODE('') as C, UNICODE(NULL) as D;
+-------+-------+-------+-------+
| A | B | C | D |
+-------+-------+-------+-------+
| 226 | 226 | 0 | NULL |
+-------+-------+-------+-------+
UPPER
UPPER(value)
Description
For STRING arguments, returns the original string with all alphabetic
characters in uppercase. Mapping between uppercase and lowercase is done
according to the
Unicode Character Database
without taking into account language-specific mappings.
For BYTES arguments, the argument is treated as ASCII text, with all bytes
greater than 127 left intact.
Return type
STRING or BYTES
Examples
WITH items AS
(SELECT
"foo" as item
UNION ALL
SELECT
"bar" as item
UNION ALL
SELECT
"baz" as item)
SELECT
UPPER(item) AS example
FROM items;
+---------+
| example |
+---------+
| FOO |
| BAR |
| BAZ |
+---------+
JSON functions
BigQuery supports the following functions, which can retrieve and transform JSON data.
Function overview
Standard JSON extraction functions (recommended)
The following functions use double quotes to escape invalid
JSONPath characters: "a.b".
This behavior is consistent with the ANSI standard.
| JSON function | Description | Return type |
|---|---|---|
JSON_QUERY |
Extracts a JSON value, such as an array or object, or a JSON scalar value, such as a string, integer, or boolean. |
JSON-formatted STRING
|
JSON_VALUE |
Extracts a scalar value.
A scalar value can represent a string, integer, or boolean.
Removes the outermost quotes and unescapes the values.
Returns a SQL NULL if a non-scalar value is selected.
|
STRING |
JSON_QUERY_ARRAY |
Extracts an array of JSON values, such as arrays or objects, and JSON scalar values, such as strings, integers, and booleans. |
ARRAY<JSON-formatted STRING>
|
JSON_VALUE_ARRAY |
Extracts an array of scalar values. A scalar value can represent a
string, integer, or boolean.
Removes the outermost quotes and unescapes the values.
Returns a SQL NULL if the selected value is not an array or
not an array containing only scalar values.
|
ARRAY<STRING> |
Legacy JSON extraction functions
The following functions use single quotes and brackets to escape invalid
JSONPath characters: ['a.b'].
While these functions are supported by BigQuery, we recommend using the functions in the previous table.
| JSON function | Description | Return type |
|---|---|---|
JSON_EXTRACT |
Extracts a JSON value, such as an array or object, or a JSON scalar value, such as a string, integer, or boolean. |
JSON-formatted STRING
|
JSON_EXTRACT_SCALAR |
Extracts a scalar value.
A scalar value can represent a string, integer, or boolean.
Removes the outermost quotes and unescapes the values.
Returns a SQL NULL if a non-scalar value is selected.
|
STRING |
JSON_EXTRACT_ARRAY |
Extracts an array of JSON values, such as arrays or objects, and JSON scalar values, such as strings, integers, and booleans. |
ARRAY<JSON-formatted STRING>
|
JSON_EXTRACT_STRING_ARRAY |
Extracts an array of scalar values. A scalar value can represent a
string, integer, or boolean.
Removes the outermost quotes and unescapes the values.
Returns a SQL NULL if the selected value is not an array or
not an array containing only scalar values.
|
ARRAY<STRING> |
Other JSON functions
| JSON function | Description | Return type |
|---|---|---|
TO_JSON_STRING |
Takes a SQL value and returns a JSON-formatted string representation of the value. | JSON-formatted STRING |
JSON_EXTRACT
JSON_EXTRACT(json_string_expr, json_path)
Description
Extracts a JSON value, such as an array or object, or a JSON scalar value, such as a string, integer, or boolean. If a JSON key uses invalid JSONPath characters, then you can escape those characters using single quotes and brackets.
json_string_expr: A JSON-formatted string. For example:'{"class" : {"students" : [{"name" : "Jane"}]}}'Extracts a SQL
NULLwhen a JSON-formatted string "null" is encountered. For example:SELECT JSON_EXTRACT("null", "$") -- Returns a SQL NULLjson_path: The JSONPath. This identifies the value or values that you want to obtain from the JSON-formatted string.SELECT JSON_EXTRACT('{"a":null}', "$.a"); -- Returns a SQL NULL SELECT JSON_EXTRACT('{"a":null}', "$.b"); -- Returns a SQL NULL
If you want to include non-scalar values such as arrays in the extraction, then
use JSON_EXTRACT. If you only want to extract scalar values such strings,
integers, and booleans, then use JSON_EXTRACT_SCALAR.
Return type
A JSON-formatted STRING
Examples
In the following examples, JSON data is extracted and returned as JSON-formatted strings.
SELECT JSON_EXTRACT(json_text, '$') AS json_text_string
FROM UNNEST([
'{"class" : {"students" : [{"name" : "Jane"}]}}',
'{"class" : {"students" : []}}',
'{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'
]) AS json_text;
+-----------------------------------------------------------+
| json_text_string |
+-----------------------------------------------------------+
| {"class":{"students":[{"name":"Jane"}]}} |
| {"class":{"students":[]}} |
| {"class":{"students":[{"name":"John"},{"name":"Jamie"}]}} |
+-----------------------------------------------------------+
SELECT JSON_EXTRACT(json_text, '$.class.students[0]') AS first_student
FROM UNNEST([
'{"class" : {"students" : [{"name" : "Jane"}]}}',
'{"class" : {"students" : []}}',
'{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'
]) AS json_text;
+-----------------+
| first_student |
+-----------------+
| {"name":"Jane"} |
| NULL |
| {"name":"John"} |
+-----------------+
SELECT JSON_EXTRACT(json_text, '$.class.students[1].name') AS second_student_name
FROM UNNEST([
'{"class" : {"students" : [{"name" : "Jane"}]}}',
'{"class" : {"students" : []}}',
'{"class" : {"students" : [{"name" : "John"}, {"name" : null}]}}',
'{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'
]) AS json_text;
+-------------------+
| second_student |
+-------------------+
| NULL |
| NULL |
| NULL |
| "Jamie" |
+-------------------+
SELECT JSON_EXTRACT(json_text, "$.class['students']") AS student_names
FROM UNNEST([
'{"class" : {"students" : [{"name" : "Jane"}]}}',
'{"class" : {"students" : []}}',
'{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'
]) AS json_text;
+------------------------------------+
| student_names |
+------------------------------------+
| [{"name":"Jane"}] |
| [] |
| [{"name":"John"},{"name":"Jamie"}] |
+------------------------------------+
JSON_QUERY
JSON_QUERY(json_string_expr, json_path)
Description
Extracts a JSON value, such as an array or object, or a JSON scalar value, such as a string, integer, or boolean. If a JSON key uses invalid JSONPath characters, then you can escape those characters using double quotes.
json_string_expr: A JSON-formatted string. For example:'{"class" : {"students" : [{"name" : "Jane"}]}}'Extracts a SQL
NULLwhen a JSON-formatted string "null" is encountered. For example:SELECT JSON_QUERY("null", "$") -- Returns a SQL NULLjson_path: The JSONPath. This identifies the value or values that you want to obtain from the JSON-formatted string.SELECT JSON_QUERY('{"a":null}', "$.a"); -- Returns a SQL NULL SELECT JSON_QUERY('{"a":null}', "$.b"); -- Returns a SQL NULL
If you want to include non-scalar values such as arrays in the extraction, then
use JSON_QUERY. If you only want to extract scalar values such strings,
integers, and booleans, then use JSON_VALUE.
Return type
A JSON-formatted STRING
Examples
In the following examples, JSON data is extracted and returned as JSON-formatted strings.
SELECT JSON_QUERY(json_text, '$') AS json_text_string
FROM UNNEST([
'{"class" : {"students" : [{"name" : "Jane"}]}}',
'{"class" : {"students" : []}}',
'{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'
]) AS json_text;
+-----------------------------------------------------------+
| json_text_string |
+-----------------------------------------------------------+
| {"class":{"students":[{"name":"Jane"}]}} |
| {"class":{"students":[]}} |
| {"class":{"students":[{"name":"John"},{"name":"Jamie"}]}} |
+-----------------------------------------------------------+
SELECT JSON_QUERY(json_text, '$.class.students[0]') AS first_student
FROM UNNEST([
'{"class" : {"students" : [{"name" : "Jane"}]}}',
'{"class" : {"students" : []}}',
'{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'
]) AS json_text;
+-----------------+
| first_student |
+-----------------+
| {"name":"Jane"} |
| NULL |
| {"name":"John"} |
+-----------------+
SELECT JSON_QUERY(json_text, '$.class.students[1].name') AS second_student_name
FROM UNNEST([
'{"class" : {"students" : [{"name" : "Jane"}]}}',
'{"class" : {"students" : []}}',
'{"class" : {"students" : [{"name" : "John"}, {"name" : null}]}}',
'{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'
]) AS json_text;
+-------------------+
| second_student |
+-------------------+
| NULL |
| NULL |
| NULL |
| "Jamie" |
+-------------------+
SELECT JSON_QUERY(json_text, '$.class."students"') AS student_names
FROM UNNEST([
'{"class" : {"students" : [{"name" : "Jane"}]}}',
'{"class" : {"students" : []}}',
'{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'
]) AS json_text;
+------------------------------------+
| student_names |
+------------------------------------+
| [{"name":"Jane"}] |
| [] |
| [{"name":"John"},{"name":"Jamie"}] |
+------------------------------------+
JSON_EXTRACT_SCALAR
JSON_EXTRACT_SCALAR(json_string_expr[, json_path])
Description
Extracts a scalar value and then returns it as a string. A scalar value can represent a string, integer, or boolean. Removes the outermost quotes and unescapes the return values. If a JSON key uses invalid JSONPath characters, then you can escape those characters using single quotes and brackets.
json_string_expr: A JSON-formatted string. For example:'{"class" : {"students" : [{"name" : "Jane"}]}}'json_path: The JSONPath. This identifies the value or values that you want to obtain from the JSON-formatted string. Ifjson_pathreturns a JSONnullor a non-scalar value (in other words, ifjson_pathrefers to an object or an array), then a SQLNULLis returned. If this optional parameter is not provided, then the JSONPath$symbol is applied, which means that the entire JSON-formatted string is analyzed.
If you only want to extract scalar values such strings, integers, and booleans,
then use JSON_EXTRACT_SCALAR. If you want to include non-scalar values such as
arrays in the extraction, then use JSON_EXTRACT.
Return type
STRING
Examples
The following example compares how results are returned for the JSON_EXTRACT
and JSON_EXTRACT_SCALAR functions.
SELECT JSON_EXTRACT('{ "name" : "Jakob", "age" : "6" }', '$.name') AS json_name,
JSON_EXTRACT_SCALAR('{ "name" : "Jakob", "age" : "6" }', '$.name') AS scalar_name,
JSON_EXTRACT('{ "name" : "Jakob", "age" : "6" }', '$.age') AS json_age,
JSON_EXTRACT_SCALAR('{ "name" : "Jakob", "age" : "6" }', '$.age') AS scalar_age;
+-----------+-------------+----------+------------+
| json_name | scalar_name | json_age | scalar_age |
+-----------+-------------+----------+------------+
| "Jakob" | Jakob | "6" | 6 |
+-----------+-------------+----------+------------+
SELECT JSON_EXTRACT('{"fruits": ["apple", "banana"]}', '$.fruits') AS json_extract,
JSON_EXTRACT_SCALAR('{"fruits": ["apple", "banana"]}', '$.fruits') AS json_extract_scalar;
+--------------------+---------------------+
| json_extract | json_extract_scalar |
+--------------------+---------------------+
| ["apple","banana"] | NULL |
+--------------------+---------------------+
In cases where a JSON key uses invalid JSONPath characters, you can escape those
characters using single quotes and brackets, [' ']. For example:
SELECT JSON_EXTRACT_SCALAR('{"a.b": {"c": "world"}}', "$['a.b'].c") AS hello;
+-------+
| hello |
+-------+
| world |
+-------+
JSON_VALUE
JSON_VALUE(json_string_expr[, json_path])
Description
Extracts a scalar value and then returns it as a string. A scalar value can represent a string, integer, or boolean. Removes the outermost quotes and unescapes the return values. If a JSON key uses invalid JSONPath characters, then you can escape those characters using double quotes.
json_string_expr: A JSON-formatted string. For example:'{"class" : {"students" : [{"name" : "Jane"}]}}'json_path: The JSONPath. This identifies the value or values that you want to obtain from the JSON-formatted string. Ifjson_pathreturns a JSONnullor a non-scalar value (in other words, ifjson_pathrefers to an object or an array), then a SQLNULLis returned. If this optional parameter is not provided, then the JSONPath$symbol is applied, which means that the entire JSON-formatted string is analyzed.
If you only want to extract scalar values such strings, integers, and booleans,
then use JSON_VALUE. If you want to include non-scalar values such as arrays
in the extraction, then use JSON_QUERY.
Return type
STRING
Examples
The following example compares how results are returned for the JSON_QUERY
and JSON_VALUE functions.
SELECT JSON_QUERY('{ "name" : "Jakob", "age" : "6" }', '$.name') AS json_name,
JSON_VALUE('{ "name" : "Jakob", "age" : "6" }', '$.name') AS scalar_name,
JSON_QUERY('{ "name" : "Jakob", "age" : "6" }', '$.age') AS json_age,
JSON_VALUE('{ "name" : "Jakob", "age" : "6" }', '$.age') AS scalar_age;
+-----------+-------------+----------+------------+
| json_name | scalar_name | json_age | scalar_age |
+-----------+-------------+----------+------------+
| "Jakob" | Jakob | "6" | 6 |
+-----------+-------------+----------+------------+
SELECT JSON_QUERY('{"fruits": ["apple", "banana"]}', '$.fruits') AS json_query,
JSON_VALUE('{"fruits": ["apple", "banana"]}', '$.fruits') AS json_value;
+--------------------+------------+
| json_query | json_value |
+--------------------+------------+
| ["apple","banana"] | NULL |
+--------------------+------------+
In cases where a JSON key uses invalid JSONPath characters, you can escape those characters using double quotes. For example:
SELECT JSON_VALUE('{"a.b": {"c": "world"}}', '$."a.b".c') AS hello;
+-------+
| hello |
+-------+
| world |
+-------+
JSON_EXTRACT_ARRAY
JSON_EXTRACT_ARRAY(json_string_expr[, json_path])
Description
Extracts an array of JSON values, such as arrays or objects, and JSON scalar values, such as strings, integers, and booleans. If a JSON key uses invalid JSONPath characters, then you can escape those characters using single quotes and brackets.
json_string_expr: A JSON-formatted string. For example:'{"class" : {"students" : [{"name" : "Jane"}]}}'json_path: The JSONPath. This identifies the value or values that you want to obtain from the JSON-formatted string. If this optional parameter is not provided, then the JSONPath$symbol is applied, which means that the entire JSON-formatted string is analyzed.
Return type
ARRAY<JSON-formatted STRING>
Examples
This extracts the items in a JSON-formatted string to a string array:
SELECT JSON_EXTRACT_ARRAY('[1,2,3]') AS string_array;
+--------------+
| string_array |
+--------------+
| [1, 2, 3] |
+--------------+
This extracts a string array and converts it to an integer array:
SELECT ARRAY(
SELECT CAST(integer_element AS INT64)
FROM UNNEST(
JSON_EXTRACT_ARRAY('[1,2,3]','$')
) AS integer_element
) AS integer_array;
+---------------+
| integer_array |
+---------------+
| [1, 2, 3] |
+---------------+
This extracts string values in a JSON-formatted string to an array:
-- Doesn't strip the double quotes
SELECT JSON_EXTRACT_ARRAY('["apples","oranges","grapes"]', '$') AS string_array;
+---------------------------------+
| string_array |
+---------------------------------+
| ["apples", "oranges", "grapes"] |
+---------------------------------+
-- Strips the double quotes
SELECT ARRAY(
SELECT JSON_EXTRACT_SCALAR(string_element, '$')
FROM UNNEST(JSON_EXTRACT_ARRAY('["apples","oranges","grapes"]','$')) AS string_element
) AS string_array;
+---------------------------+
| string_array |
+---------------------------+
| [apples, oranges, grapes] |
+---------------------------+
This extracts only the items in the fruit property to an array:
SELECT JSON_EXTRACT_ARRAY(
'{"fruit":[{"apples":5,"oranges":10},{"apples":2,"oranges":4}],"vegetables":[{"lettuce":7,"kale": 8}]}',
'$.fruit'
) AS string_array;
+-------------------------------------------------------+
| string_array |
+-------------------------------------------------------+
| [{"apples":5,"oranges":10}, {"apples":2,"oranges":4}] |
+-------------------------------------------------------+
These are equivalent:
SELECT JSON_EXTRACT_ARRAY('{"fruits":["apples","oranges","grapes"]}','$[fruits]') AS string_array;
SELECT JSON_EXTRACT_ARRAY('{"fruits":["apples","oranges","grapes"]}','$.fruits') AS string_array;
-- The queries above produce the following result:
+---------------------------------+
| string_array |
+---------------------------------+
| ["apples", "oranges", "grapes"] |
+---------------------------------+
In cases where a JSON key uses invalid JSONPath characters, you can escape those
characters using single quotes and brackets, [' ']. For example:
SELECT JSON_EXTRACT_ARRAY('{"a.b": {"c": ["world"]}}', "$['a.b'].c") AS hello;
+-----------+
| hello |
+-----------+
| ["world"] |
+-----------+
The following examples explore how invalid requests and empty arrays are handled:
- If a JSONPath is invalid, an error is thrown.
- If a JSON-formatted string is invalid, the output is NULL.
- It is okay to have empty arrays in the JSON-formatted string.
-- An error is thrown if you provide an invalid JSONPath.
SELECT JSON_EXTRACT_ARRAY('["foo","bar","baz"]','INVALID_JSONPath') AS result;
-- If the JSONPath does not refer to an array, then NULL is returned.
SELECT JSON_EXTRACT_ARRAY('{"a":"foo"}','$.a') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a key that does not exist is specified, then the result is NULL.
SELECT JSON_EXTRACT_ARRAY('{"a":"foo"}','$.b') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- Empty arrays in JSON-formatted strings are supported.
SELECT JSON_EXTRACT_ARRAY('{"a":"foo","b":[]}','$.b') AS result;
+--------+
| result |
+--------+
| [] |
+--------+
JSON_QUERY_ARRAY
JSON_QUERY_ARRAY(json_string_expr[, json_path])
Description
Extracts an array of JSON values, such as arrays or objects, and JSON scalar values, such as strings, integers, and booleans. If a JSON key uses invalid JSONPath characters, then you can escape those characters using double quotes.
json_string_expr: A JSON-formatted string. For example:'{"class" : {"students" : [{"name" : "Jane"}]}}'json_path: The JSONPath. This identifies the value or values that you want to obtain from the JSON-formatted string. If this optional parameter is not provided, then the JSONPath$symbol is applied, which means that the entire JSON-formatted string is analyzed.
Return type
ARRAY<JSON-formatted STRING>
Examples
This extracts the items in a JSON-formatted string to a string array:
SELECT JSON_QUERY_ARRAY('[1,2,3]') AS string_array;
+--------------+
| string_array |
+--------------+
| [1, 2, 3] |
+--------------+
This extracts a string array and converts it to an integer array:
SELECT ARRAY(
SELECT CAST(integer_element AS INT64)
FROM UNNEST(
JSON_QUERY_ARRAY('[1,2,3]','$')
) AS integer_element
) AS integer_array;
+---------------+
| integer_array |
+---------------+
| [1, 2, 3] |
+---------------+
This extracts string values in a JSON-formatted string to an array:
-- Doesn't strip the double quotes
SELECT JSON_QUERY_ARRAY('["apples","oranges","grapes"]', '$') AS string_array;
+---------------------------------+
| string_array |
+---------------------------------+
| ["apples", "oranges", "grapes"] |
+---------------------------------+
-- Strips the double quotes
SELECT ARRAY(
SELECT JSON_EXTRACT_SCALAR(string_element, '$')
FROM UNNEST(JSON_QUERY_ARRAY('["apples","oranges","grapes"]','$')) AS string_element
) AS string_array;
+---------------------------+
| string_array |
+---------------------------+
| [apples, oranges, grapes] |
+---------------------------+
This extracts only the items in the fruit property to an array:
SELECT JSON_QUERY_ARRAY(
'{"fruit":[{"apples":5,"oranges":10},{"apples":2,"oranges":4}],"vegetables":[{"lettuce":7,"kale": 8}]}',
'$.fruit'
) AS string_array;
+-------------------------------------------------------+
| string_array |
+-------------------------------------------------------+
| [{"apples":5,"oranges":10}, {"apples":2,"oranges":4}] |
+-------------------------------------------------------+
These are equivalent:
SELECT JSON_QUERY_ARRAY('{"fruits":["apples","oranges","grapes"]}','$.fruits') AS string_array;
SELECT JSON_QUERY_ARRAY('{"fruits":["apples","oranges","grapes"]}','$."fruits"') AS string_array;
-- The queries above produce the following result:
+---------------------------------+
| string_array |
+---------------------------------+
| ["apples", "oranges", "grapes"] |
+---------------------------------+
In cases where a JSON key uses invalid JSONPath characters, you can escape those
characters using double quotes: " ". For example:
SELECT JSON_QUERY_ARRAY('{"a.b": {"c": ["world"]}}', '$."a.b".c') AS hello;
+-----------+
| hello |
+-----------+
| ["world"] |
+-----------+
The following examples show how invalid requests and empty arrays are handled:
-- An error is returned if you provide an invalid JSONPath.
SELECT JSON_QUERY_ARRAY('["foo","bar","baz"]','INVALID_JSONPath') AS result;
-- If the JSONPath does not refer to an array, then NULL is returned.
SELECT JSON_QUERY_ARRAY('{"a":"foo"}','$.a') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a key that does not exist is specified, then the result is NULL.
SELECT JSON_QUERY_ARRAY('{"a":"foo"}','$.b') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- Empty arrays in JSON-formatted strings are supported.
SELECT JSON_QUERY_ARRAY('{"a":"foo","b":[]}','$.b') AS result;
+--------+
| result |
+--------+
| [] |
+--------+
JSON_EXTRACT_STRING_ARRAY
JSON_EXTRACT_STRING_ARRAY(json_string_expr[, json_path])
Description
Extracts an array of scalar values and returns an array of string-formatted scalar values. A scalar value can represent a string, integer, or boolean. If a JSON key uses invalid JSONPath characters, you can escape those characters using single quotes and brackets.
json_string_expr: A JSON-formatted string. For example:'{"class" : {"students" : [{"name" : "Jane"}]}}'json_path: The JSONPath. This identifies the value or values that you want to obtain from the JSON-formatted string. If this optional parameter is not provided, then the JSONPath$symbol is applied, which means that the entire JSON-formatted string is analyzed.
Return type
ARRAY<STRING>
Examples
The following example compares how results are returned for the
JSON_EXTRACT_ARRAY and JSON_EXTRACT_STRING_ARRAY functions.
SELECT JSON_EXTRACT_ARRAY('["apples","oranges"]') AS json_array,
JSON_EXTRACT_STRING_ARRAY('["apples","oranges"]') AS string_array;
+-----------------------+-------------------+
| json_array | string_array |
+-----------------------+-------------------+
| ["apples", "oranges"] | [apples, oranges] |
+-----------------------+-------------------+
This extracts the items in a JSON-formatted string to a string array:
-- Strips the double quotes
SELECT JSON_EXTRACT_STRING_ARRAY('["foo","bar","baz"]','$') AS string_array;
+-----------------+
| string_array |
+-----------------+
| [foo, bar, baz] |
+-----------------+
This extracts a string array and converts it to an integer array:
SELECT ARRAY(
SELECT CAST(integer_element AS INT64)
FROM UNNEST(
JSON_EXTRACT_STRING_ARRAY('[1,2,3]','$')
) AS integer_element
) AS integer_array;
+---------------+
| integer_array |
+---------------+
| [1, 2, 3] |
+---------------+
These are equivalent:
SELECT JSON_EXTRACT_STRING_ARRAY('{"fruits":["apples","oranges","grapes"]}','$[fruits]') AS string_array;
SELECT JSON_EXTRACT_STRING_ARRAY('{"fruits":["apples","oranges","grapes"]}','$.fruits') AS string_array;
-- The queries above produce the following result:
+---------------------------+
| string_array |
+---------------------------+
| [apples, oranges, grapes] |
+---------------------------+
In cases where a JSON key uses invalid JSONPath characters, you can escape those
characters using single quotes and brackets: [' ']. For example:
SELECT JSON_EXTRACT_STRING_ARRAY('{"a.b": {"c": ["world"]}}', "$['a.b'].c") AS hello;
+---------+
| hello |
+---------+
| [world] |
+---------+
The following examples explore how invalid requests and empty arrays are handled:
-- An error is thrown if you provide an invalid JSONPath.
SELECT JSON_EXTRACT_STRING_ARRAY('["foo","bar","baz"]','INVALID_JSONPath') AS result;
-- If the JSON formatted string is invalid, then NULL is returned.
SELECT JSON_EXTRACT_STRING_ARRAY('}}','$') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If the JSON document is NULL, then NULL is returned.
SELECT JSON_EXTRACT_STRING_ARRAY(NULL,'$') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath does not match anything, then the output is NULL.
SELECT JSON_EXTRACT_STRING_ARRAY('{"a":["foo","bar","baz"]}','$.b') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath matches an object that is not an array, then the output is NULL.
SELECT JSON_EXTRACT_STRING_ARRAY('{"a":"foo"}','$') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath matches an array of non-scalar objects, then the output is NULL.
SELECT JSON_EXTRACT_STRING_ARRAY('{"a":[{"b":"foo","c":1},{"b":"bar","c":2}],"d":"baz"}','$.a') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath matches an array of mixed scalar and non-scalar objects, then the output is NULL.
SELECT JSON_EXTRACT_STRING_ARRAY('{"a":[10, {"b": 20}]','$.a') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath matches an empty JSON array, then the output is an empty array instead of NULL.
SELECT JSON_EXTRACT_STRING_ARRAY('{"a":"foo","b":[]}','$.b') AS result;
+--------+
| result |
+--------+
| [] |
+--------+
-- If a JSONPath matches an array that contains scalar values and a JSON null,
-- then the output of the JSON_EXTRACT_STRING_ARRAY function must be transformed
-- because the final output cannot be an array with NULL values. This example
-- uses the UNNEST operator to convert the output array into a table as the final output.
SELECT string_value FROM UNNEST(JSON_EXTRACT_STRING_ARRAY('["world", 1, null]')) AS string_value;
+--------------+
| string_value |
+--------------+
| world |
| 1 |
| NULL |
+--------------+
JSON_VALUE_ARRAY
JSON_VALUE_ARRAY(json_string_expr[, json_path])
Description
Extracts an array of scalar values and returns an array of string-formatted scalar values. A scalar value can represent a string, integer, or boolean. If a JSON key uses invalid JSONPath characters, you can escape those characters using double quotes.
json_string_expr: A JSON-formatted string. For example:'{"class" : {"students" : [{"name" : "Jane"}]}}'json_path: The JSONPath. This identifies the value or values that you want to obtain from the JSON-formatted string. If this optional parameter is not provided, then the JSONPath$symbol is applied, which means that the entire JSON-formatted string is analyzed.
Return type
ARRAY<STRING>
Examples
The following example compares how results are returned for the
JSON_QUERY_ARRAY and JSON_VALUE_ARRAY functions.
SELECT JSON_QUERY_ARRAY('["apples","oranges"]') AS json_array,
JSON_VALUE_ARRAY('["apples","oranges"]') AS string_array;
+-----------------------+-------------------+
| json_array | string_array |
+-----------------------+-------------------+
| ["apples", "oranges"] | [apples, oranges] |
+-----------------------+-------------------+
This extracts the items in a JSON-formatted string to a string array:
-- Strips the double quotes
SELECT JSON_VALUE_ARRAY('["foo","bar","baz"]','$') AS string_array;
+-----------------+
| string_array |
+-----------------+
| [foo, bar, baz] |
+-----------------+
This extracts a string array and converts it to an integer array:
SELECT ARRAY(
SELECT CAST(integer_element AS INT64)
FROM UNNEST(
JSON_VALUE_ARRAY('[1,2,3]','$')
) AS integer_element
) AS integer_array;
+---------------+
| integer_array |
+---------------+
| [1, 2, 3] |
+---------------+
These are equivalent:
SELECT JSON_VALUE_ARRAY('{"fruits":["apples","oranges","grapes"]}','$.fruits') AS string_array;
SELECT JSON_VALUE_ARRAY('{"fruits":["apples","oranges","grapes"]}','$."fruits"') AS string_array;
-- The queries above produce the following result:
+---------------------------+
| string_array |
+---------------------------+
| [apples, oranges, grapes] |
+---------------------------+
In cases where a JSON key uses invalid JSONPath characters, you can escape those
characters using double quotes: " ". For example:
SELECT JSON_VALUE_ARRAY('{"a.b": {"c": ["world"]}}', '$."a.b".c') AS hello;
+---------+
| hello |
+---------+
| [world] |
+---------+
The following examples explore how invalid requests and empty arrays are handled:
-- An error is thrown if you provide an invalid JSONPath.
SELECT JSON_VALUE_ARRAY('["foo","bar","baz"]','INVALID_JSONPath') AS result;
-- If the JSON-formatted string is invalid, then NULL is returned.
SELECT JSON_VALUE_ARRAY('}}','$') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If the JSON document is NULL, then NULL is returned.
SELECT JSON_VALUE_ARRAY(NULL,'$') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath does not match anything, then the output is NULL.
SELECT JSON_VALUE_ARRAY('{"a":["foo","bar","baz"]}','$.b') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath matches an object that is not an array, then the output is NULL.
SELECT JSON_VALUE_ARRAY('{"a":"foo"}','$') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath matches an array of non-scalar objects, then the output is NULL.
SELECT JSON_VALUE_ARRAY('{"a":[{"b":"foo","c":1},{"b":"bar","c":2}],"d":"baz"}','$.a') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath matches an array of mixed scalar and non-scalar objects,
-- then the output is NULL.
SELECT JSON_VALUE_ARRAY('{"a":[10, {"b": 20}]','$.a') AS result;
+--------+
| result |
+--------+
| NULL |
+--------+
-- If a JSONPath matches an empty JSON array, then the output is an empty array instead of NULL.
SELECT JSON_VALUE_ARRAY('{"a":"foo","b":[]}','$.b') AS result;
+--------+
| result |
+--------+
| [] |
+--------+
-- If a JSONPath matches an array that contains scalar objects and a JSON null,
-- then the output of the JSON_VALUE_ARRAY function must be transformed
-- because the final output cannot be an array with NULL values. This example
-- uses the UNNEST operator to convert the output array into a table as the final output.
SELECT string_value FROM UNNEST(JSON_VALUE_ARRAY('["world", 1, null]')) AS string_value;
+--------------+
| string_value |
+--------------+
| world |
| 1 |
| NULL |
+--------------+
TO_JSON_STRING
TO_JSON_STRING(value[, pretty_print])
Description
Takes a SQL value and returns a JSON-formatted string representation of the value. The value must be a supported BigQuery data type. You can review the BigQuery data types that this function supports and their JSON encodings here.
This function supports an optional boolean parameter called pretty_print.
If pretty_print is true, the returned value is formatted for easy
readability.
Return type
A JSON-formatted STRING
Examples
Convert rows in a table to JSON-formatted strings.
With CoordinatesTable AS (
(SELECT 1 AS id, [10,20] AS coordinates) UNION ALL
(SELECT 2 AS id, [30,40] AS coordinates) UNION ALL
(SELECT 3 AS id, [50,60] AS coordinates))
SELECT id, coordinates, TO_JSON_STRING(t) AS json_data
FROM CoordinatesTable AS t;
+----+-------------+--------------------------------+
| id | coordinates | json_data |
+----+-------------+--------------------------------+
| 1 | [10, 20] | {"id":1,"coordinates":[10,20]} |
| 2 | [30, 40] | {"id":2,"coordinates":[30,40]} |
| 3 | [50, 60] | {"id":3,"coordinates":[50,60]} |
+----+-------------+--------------------------------+
Convert rows in a table to JSON-formatted strings that are easy to read.
With CoordinatesTable AS (
(SELECT 1 AS id, [10,20] AS coordinates) UNION ALL
(SELECT 2 AS id, [30,40] AS coordinates))
SELECT id, coordinates, TO_JSON_STRING(t, true) AS json_data
FROM CoordinatesTable AS t;
+----+-------------+--------------------+
| id | coordinates | json_data |
+----+-------------+--------------------+
| 1 | [10, 20] | { |
| | | "id": 1, |
| | | "coordinates": [ |
| | | 10, |
| | | 20 |
| | | ] |
| | | } |
+----+-------------+--------------------+
| 2 | [30, 40] | { |
| | | "id": 2, |
| | | "coordinates": [ |
| | | 30, |
| | | 40 |
| | | ] |
| | | } |
+----+-------------+--------------------+
JSON encodings
The following table includes common encodings that are used when a
SQL value is encoded as JSON value with
the TO_JSON_STRING function.
| From SQL | To JSON | Examples |
|---|---|---|
| NULL |
null |
SQL input: NULLJSON output: null
|
| BOOL | boolean |
SQL input: TRUEJSON output: trueSQL input: FALSEJSON output: false |
| INT64 |
number or string Encoded as a number when the value is in the range of [-253, 253], which is the range of integers that can be represented losslessly as IEEE 754 double-precision floating point numbers. A value outside of this range is encoded as a string. |
SQL input: 9007199254740992JSON output: 9007199254740992SQL input: 9007199254740993JSON output: "9007199254740993" |
|
NUMERIC
BIGNUMERIC |
number or string Encoded as a number when the value is in the range of [-253, 253] and has no fractional part. A value outside of this range is encoded as a string. |
SQL input: -1JSON output: -1SQL input: 0JSON output: 0SQL input: 9007199254740993JSON output: "9007199254740993"SQL input: 123.56JSON output: "123.56" |
| FLOAT64 |
number or string
|
SQL input: 1.0JSON output: 1SQL input: 9007199254740993JSON output: 9007199254740993SQL input: "+inf"JSON output: "Infinity"SQL input: "-inf"JSON output: "-Infinity"SQL input: "NaN"JSON output: "NaN" |
| STRING |
string
Encoded as a string, escaped according to the JSON standard.
Specifically, |
SQL input: "abc"JSON output: "abc"SQL input: "\"abc\""JSON output: "\"abc\"" |
| BYTES |
string Uses RFC 4648 Base64 data encoding. |
SQL input: b"Google"JSON output: "R29vZ2xl" |
| DATE | string |
SQL input: DATE '2017-03-06'JSON output: "2017-03-06" |
| TIMESTAMP |
string Encoded as ISO 8601 date and time, where T separates the date and time and Z (Zulu/UTC) represents the time zone. |
SQL input: TIMESTAMP '2017-03-06 12:34:56.789012'JSON output: "2017-03-06T12:34:56.789012Z" |
| DATETIME |
string Encoded as ISO 8601 date and time, where T separates the date and time. |
SQL input: DATETIME '2017-03-06 12:34:56.789012'JSON output: "2017-03-06T12:34:56.789012" |
| TIME |
string Encoded as ISO 8601 time. |
SQL input: TIME '12:34:56.789012'JSON output: "12:34:56.789012" |
| ARRAY |
array Can contain zero or more elements. Each element is formatted according to its type. |
SQL input: ["red", "blue", "green"]JSON output: ["red", "blue", "green"]SQL input: [1, 2, 3]JSON output: [1, 2, 3] |
| STRUCT |
object The object can contain zero or more key/value pairs. Each value is formatted according to its type.
For
Anonymous fields are represented with
Invalid UTF-8 field names might result in unparseable JSON. String
values are escaped according to the JSON standard. Specifically,
|
SQL input: STRUCT(12 AS purchases, TRUE AS inStock)JSON output: {"purchases":12,"inStock": true} |
JSONPath
Most JSON functions pass in a json_string_expr and json_path
parameter. The json_string_expr parameter passes in a JSON-formatted
string, and the json_path parameter identifies the value or
values you want to obtain from the JSON-formatted string.
The json_string_expr parameter must be a JSON string that is
formatted like this:
'{"class" : {"students" : [{"name" : "Jane"}]}}'
You construct the json_path parameter using the
JSONPath format. As part of this format, this parameter must start
with a $ symbol, which refers to the outermost level of the JSON-formatted
string. You can identify child values using dots. If the JSON object is an
array, you can use brackets to specify the array index. If the keys contain
$, dots, or brackets, refer to each JSON function for how to escape
them.
| JSONPath | Description | Example | Result using the above json_string_expr |
|---|---|---|---|
| $ | Root object or element | "$" | {"class":{"students":[{"name":"Jane"}]}} |
| . | Child operator | "$.class.students" | [{"name":"Jane"}] |
| [] | Subscript operator | "$.class.students[0]" | {"name":"Jane"} |
A JSON functions returns NULL if the json_path parameter does
not match a value in json_string_expr. If the selected value for a scalar
function is not scalar, such as an object or an array, the function
returns NULL.
If the JSONPath is invalid, the function raises an error.
Array functions
ARRAY
ARRAY(subquery)
Description
The ARRAY function returns an ARRAY with one element for each row in a
subquery.
If subquery produces a
SQL table,
the table must have exactly one column. Each element in the output ARRAY is
the value of the single column of a row in the table.
If subquery produces a
value table,
then each element in the output ARRAY is the entire corresponding row of the
value table.
Constraints
- Subqueries are unordered, so the elements of the output
ARRAYare not guaranteed to preserve any order in the source table for the subquery. However, if the subquery includes anORDER BYclause, theARRAYfunction will return anARRAYthat honors that clause. - If the subquery returns more than one column, the
ARRAYfunction returns an error. - If the subquery returns an
ARRAYtyped column orARRAYtyped rows, theARRAYfunction returns an error: BigQuery does not supportARRAYs with elements of typeARRAY. - If the subquery returns zero rows, the
ARRAYfunction returns an emptyARRAY. It never returns aNULLARRAY.
Return type
ARRAY
Examples
SELECT ARRAY
(SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3) AS new_array;
+-----------+
| new_array |
+-----------+
| [1, 2, 3] |
+-----------+
To construct an ARRAY from a subquery that contains multiple
columns, change the subquery to use SELECT AS STRUCT. Now
the ARRAY function will return an ARRAY of STRUCTs. The ARRAY will
contain one STRUCT for each row in the subquery, and each of these STRUCTs
will contain a field for each column in that row.
SELECT
ARRAY
(SELECT AS STRUCT 1, 2, 3
UNION ALL SELECT AS STRUCT 4, 5, 6) AS new_array;
+------------------------+
| new_array |
+------------------------+
| [{1, 2, 3}, {4, 5, 6}] |
+------------------------+
Similarly, to construct an ARRAY from a subquery that contains
one or more ARRAYs, change the subquery to use SELECT AS STRUCT.
SELECT ARRAY
(SELECT AS STRUCT [1, 2, 3] UNION ALL
SELECT AS STRUCT [4, 5, 6]) AS new_array;
+----------------------------+
| new_array |
+----------------------------+
| [{[1, 2, 3]}, {[4, 5, 6]}] |
+----------------------------+
ARRAY_CONCAT
ARRAY_CONCAT(array_expression_1 [, array_expression_n])
Description
Concatenates one or more arrays with the same element type into a single array.
Return type
ARRAY
Examples
SELECT ARRAY_CONCAT([1, 2], [3, 4], [5, 6]) as count_to_six;
+--------------------------------------------------+
| count_to_six |
+--------------------------------------------------+
| [1, 2, 3, 4, 5, 6] |
+--------------------------------------------------+
ARRAY_LENGTH
ARRAY_LENGTH(array_expression)
Description
Returns the size of the array. Returns 0 for an empty array. Returns NULL if
the array_expression is NULL.
Return type
INT64
Examples
WITH items AS
(SELECT ["coffee", NULL, "milk" ] as list
UNION ALL
SELECT ["cake", "pie"] as list)
SELECT ARRAY_TO_STRING(list, ', ', 'NULL'), ARRAY_LENGTH(list) AS size
FROM items
ORDER BY size DESC;
+---------------------------------+------+
| list | size |
+---------------------------------+------+
| [coffee, NULL, milk] | 3 |
| [cake, pie] | 2 |
+---------------------------------+------+
ARRAY_TO_STRING
ARRAY_TO_STRING(array_expression, delimiter[, null_text])
Description
Returns a concatenation of the elements in array_expression
as a STRING. The value for array_expression
can either be an array of STRING or
BYTES data types.
If the null_text parameter is used, the function replaces any NULL values in
the array with the value of null_text.
If the null_text parameter is not used, the function omits the NULL value
and its preceding delimiter.
Examples
WITH items AS
(SELECT ["coffee", "tea", "milk" ] as list
UNION ALL
SELECT ["cake", "pie", NULL] as list)
SELECT ARRAY_TO_STRING(list, '--') AS text
FROM items;
+--------------------------------+
| text |
+--------------------------------+
| coffee--tea--milk |
| cake--pie |
+--------------------------------+
WITH items AS
(SELECT ["coffee", "tea", "milk" ] as list
UNION ALL
SELECT ["cake", "pie", NULL] as list)
SELECT ARRAY_TO_STRING(list, '--', 'MISSING') AS text
FROM items;
+--------------------------------+
| text |
+--------------------------------+
| coffee--tea--milk |
| cake--pie--MISSING |
+--------------------------------+
GENERATE_ARRAY
GENERATE_ARRAY(start_expression, end_expression[, step_expression])
Description
Returns an array of values. The start_expression and end_expression
parameters determine the inclusive start and end of the array.
The GENERATE_ARRAY function accepts the following data types as inputs:
- INT64
- NUMERIC
- BIGNUMERIC
- FLOAT64
The step_expression parameter determines the increment used to
generate array values. The default value for this parameter is 1.
This function returns an error if step_expression is set to 0, or if any
input is NaN.
If any argument is NULL, the function will return a NULL array.
Return Data Type
ARRAY
Examples
The following returns an array of integers, with a default step of 1.
SELECT GENERATE_ARRAY(1, 5) AS example_array;
+-----------------+
| example_array |
+-----------------+
| [1, 2, 3, 4, 5] |
+-----------------+
The following returns an array using a user-specified step size.
SELECT GENERATE_ARRAY(0, 10, 3) AS example_array;
+---------------+
| example_array |
+---------------+
| [0, 3, 6, 9] |
+---------------+
The following returns an array using a negative value, -3 for its step size.
SELECT GENERATE_ARRAY(10, 0, -3) AS example_array;
+---------------+
| example_array |
+---------------+
| [10, 7, 4, 1] |
+---------------+
The following returns an array using the same value for the start_expression
and end_expression.
SELECT GENERATE_ARRAY(4, 4, 10) AS example_array;
+---------------+
| example_array |
+---------------+
| [4] |
+---------------+
The following returns an empty array, because the start_expression is greater
than the end_expression, and the step_expression value is positive.
SELECT GENERATE_ARRAY(10, 0, 3) AS example_array;
+---------------+
| example_array |
+---------------+
| [] |
+---------------+
The following returns a NULL array because end_expression is NULL.
SELECT GENERATE_ARRAY(5, NULL, 1) AS example_array;
+---------------+
| example_array |
+---------------+
| NULL |
+---------------+
The following returns multiple arrays.
SELECT GENERATE_ARRAY(start, 5) AS example_array
FROM UNNEST([3, 4, 5]) AS start;
+---------------+
| example_array |
+---------------+
| [3, 4, 5] |
| [4, 5] |
| [5] |
+---------------+
GENERATE_DATE_ARRAY
GENERATE_DATE_ARRAY(start_date, end_date[, INTERVAL INT64_expr date_part])
Description
Returns an array of dates. The start_date and end_date
parameters determine the inclusive start and end of the array.
The GENERATE_DATE_ARRAY function accepts the following data types as inputs:
start_datemust be a DATEend_datemust be a DATEINT64_exprmust be an INT64date_partmust be either DAY, WEEK, MONTH, QUARTER, or YEAR.
The INT64_expr parameter determines the increment used to generate dates. The
default value for this parameter is 1 day.
This function returns an error if INT64_expr is set to 0.
Return Data Type
An ARRAY containing 0 or more DATE values.
Examples
The following returns an array of dates, with a default step of 1.
SELECT GENERATE_DATE_ARRAY('2016-10-05', '2016-10-08') AS example;
+--------------------------------------------------+
| example |
+--------------------------------------------------+
| [2016-10-05, 2016-10-06, 2016-10-07, 2016-10-08] |
+--------------------------------------------------+
The following returns an array using a user-specified step size.
SELECT GENERATE_DATE_ARRAY(
'2016-10-05', '2016-10-09', INTERVAL 2 DAY) AS example;
+--------------------------------------+
| example |
+--------------------------------------+
| [2016-10-05, 2016-10-07, 2016-10-09] |
+--------------------------------------+
The following returns an array using a negative value, -3 for its step size.
SELECT GENERATE_DATE_ARRAY('2016-10-05',
'2016-10-01', INTERVAL -3 DAY) AS example;
+--------------------------+
| example |
+--------------------------+
| [2016-10-05, 2016-10-02] |
+--------------------------+
The following returns an array using the same value for the start_dateand
end_date.
SELECT GENERATE_DATE_ARRAY('2016-10-05',
'2016-10-05', INTERVAL 8 DAY) AS example;
+--------------+
| example |
+--------------+
| [2016-10-05] |
+--------------+
The following returns an empty array, because the start_date is greater
than the end_date, and the step value is positive.
SELECT GENERATE_DATE_ARRAY('2016-10-05',
'2016-10-01', INTERVAL 1 DAY) AS example;
+---------+
| example |
+---------+
| [] |
+---------+
The following returns a NULL array, because one of its inputs is
NULL.
SELECT GENERATE_DATE_ARRAY('2016-10-05', NULL) AS example;
+---------+
| example |
+---------+
| NULL |
+---------+
The following returns an array of dates, using MONTH as the date_part
interval:
SELECT GENERATE_DATE_ARRAY('2016-01-01',
'2016-12-31', INTERVAL 2 MONTH) AS example;
+--------------------------------------------------------------------------+
| example |
+--------------------------------------------------------------------------+
| [2016-01-01, 2016-03-01, 2016-05-01, 2016-07-01, 2016-09-01, 2016-11-01] |
+--------------------------------------------------------------------------+
The following uses non-constant dates to generate an array.
SELECT GENERATE_DATE_ARRAY(date_start, date_end, INTERVAL 1 WEEK) AS date_range
FROM (
SELECT DATE '2016-01-01' AS date_start, DATE '2016-01-31' AS date_end
UNION ALL SELECT DATE "2016-04-01", DATE "2016-04-30"
UNION ALL SELECT DATE "2016-07-01", DATE "2016-07-31"
UNION ALL SELECT DATE "2016-10-01", DATE "2016-10-31"
) AS items;
+--------------------------------------------------------------+
| date_range |
+--------------------------------------------------------------+
| [2016-01-01, 2016-01-08, 2016-01-15, 2016-01-22, 2016-01-29] |
| [2016-04-01, 2016-04-08, 2016-04-15, 2016-04-22, 2016-04-29] |
| [2016-07-01, 2016-07-08, 2016-07-15, 2016-07-22, 2016-07-29] |
| [2016-10-01, 2016-10-08, 2016-10-15, 2016-10-22, 2016-10-29] |
+--------------------------------------------------------------+
GENERATE_TIMESTAMP_ARRAY
GENERATE_TIMESTAMP_ARRAY(start_timestamp, end_timestamp,
INTERVAL step_expression date_part)
Description
Returns an ARRAY of TIMESTAMPS separated by a given interval. The
start_timestamp and end_timestamp parameters determine the inclusive
lower and upper bounds of the ARRAY.
The GENERATE_TIMESTAMP_ARRAY function accepts the following data types as
inputs:
start_timestamp:TIMESTAMPend_timestamp:TIMESTAMPstep_expression:INT64- Allowed
date_partvalues are:MICROSECOND,MILLISECOND,SECOND,MINUTE,HOUR, orDAY.
The step_expression parameter determines the increment used to generate
timestamps.
Return Data Type
An ARRAY containing 0 or more
TIMESTAMP values.
Examples
The following example returns an ARRAY of TIMESTAMPs at intervals of 1 day.
SELECT GENERATE_TIMESTAMP_ARRAY('2016-10-05 00:00:00', '2016-10-07 00:00:00',
INTERVAL 1 DAY) AS timestamp_array;
+--------------------------------------------------------------------------+
| timestamp_array |
+--------------------------------------------------------------------------+
| [2016-10-05 00:00:00+00, 2016-10-06 00:00:00+00, 2016-10-07 00:00:00+00] |
+--------------------------------------------------------------------------+
The following example returns an ARRAY of TIMESTAMPs at intervals of 1
second.
SELECT GENERATE_TIMESTAMP_ARRAY('2016-10-05 00:00:00', '2016-10-05 00:00:02',
INTERVAL 1 SECOND) AS timestamp_array;
+--------------------------------------------------------------------------+
| timestamp_array |
+--------------------------------------------------------------------------+
| [2016-10-05 00:00:00+00, 2016-10-05 00:00:01+00, 2016-10-05 00:00:02+00] |
+--------------------------------------------------------------------------+
The following example returns an ARRAY of TIMESTAMPS with a negative
interval.
SELECT GENERATE_TIMESTAMP_ARRAY('2016-10-06 00:00:00', '2016-10-01 00:00:00',
INTERVAL -2 DAY) AS timestamp_array;
+--------------------------------------------------------------------------+
| timestamp_array |
+--------------------------------------------------------------------------+
| [2016-10-06 00:00:00+00, 2016-10-04 00:00:00+00, 2016-10-02 00:00:00+00] |
+--------------------------------------------------------------------------+
The following example returns an ARRAY with a single element, because
start_timestamp and end_timestamp have the same value.
SELECT GENERATE_TIMESTAMP_ARRAY('2016-10-05 00:00:00', '2016-10-05 00:00:00',
INTERVAL 1 HOUR) AS timestamp_array;
+--------------------------+
| timestamp_array |
+--------------------------+
| [2016-10-05 00:00:00+00] |
+--------------------------+
The following example returns an empty ARRAY, because start_timestamp is
later than end_timestamp.
SELECT GENERATE_TIMESTAMP_ARRAY('2016-10-06 00:00:00', '2016-10-05 00:00:00',
INTERVAL 1 HOUR) AS timestamp_array;
+-----------------+
| timestamp_array |
+-----------------+
| [] |
+-----------------+
The following example returns a null ARRAY, because one of the inputs is
NULL.
SELECT GENERATE_TIMESTAMP_ARRAY('2016-10-05 00:00:00', NULL, INTERVAL 1 HOUR)
AS timestamp_array;
+-----------------+
| timestamp_array |
+-----------------+
| NULL |
+-----------------+
The following example generates ARRAYs of TIMESTAMPs from columns containing
values for start_timestamp and end_timestamp.
SELECT GENERATE_TIMESTAMP_ARRAY(start_timestamp, end_timestamp, INTERVAL 1 HOUR)
AS timestamp_array
FROM
(SELECT
TIMESTAMP '2016-10-05 00:00:00' AS start_timestamp,
TIMESTAMP '2016-10-05 02:00:00' AS end_timestamp
UNION ALL
SELECT
TIMESTAMP '2016-10-05 12:00:00' AS start_timestamp,
TIMESTAMP '2016-10-05 14:00:00' AS end_timestamp
UNION ALL
SELECT
TIMESTAMP '2016-10-05 23:59:00' AS start_timestamp,
TIMESTAMP '2016-10-06 01:59:00' AS end_timestamp);
+--------------------------------------------------------------------------+
| timestamp_array |
+--------------------------------------------------------------------------+
| [2016-10-05 00:00:00+00, 2016-10-05 01:00:00+00, 2016-10-05 02:00:00+00] |
| [2016-10-05 12:00:00+00, 2016-10-05 13:00:00+00, 2016-10-05 14:00:00+00] |
| [2016-10-05 23:59:00+00, 2016-10-06 00:59:00+00, 2016-10-06 01:59:00+00] |
+--------------------------------------------------------------------------+
OFFSET and ORDINAL
array_expression[OFFSET(zero_based_offset)]
array_expression[ORDINAL(one_based_offset)]
Description
Accesses an ARRAY element by position and returns the
element. OFFSET means that the numbering starts at zero, ORDINAL means that
the numbering starts at one.
A given array can be interpreted as either 0-based or 1-based. When accessing an
array element, you must preface the array position with OFFSET or ORDINAL,
respectively; there is no default behavior.
Both OFFSET and ORDINAL generate an error if the index is out of range.
Return type
Varies depending on the elements in the ARRAY.
Examples
WITH items AS
(SELECT ["apples", "bananas", "pears", "grapes"] as list
UNION ALL
SELECT ["coffee", "tea", "milk" ] as list
UNION ALL
SELECT ["cake", "pie"] as list)
SELECT list, list[OFFSET(1)] as offset_1, list[ORDINAL(1)] as ordinal_1
FROM items;
+----------------------------------+-----------+-----------+
| list | offset_1 | ordinal_1 |
+----------------------------------+-----------+-----------+
| [apples, bananas, pears, grapes] | bananas | apples |
| [coffee, tea, milk] | tea | coffee |
| [cake, pie] | pie | cake |
+----------------------------------+-----------+-----------+
ARRAY_REVERSE
ARRAY_REVERSE(value)
Description
Returns the input ARRAY with elements in reverse order.
Return type
ARRAY
Examples
WITH example AS (
SELECT [1, 2, 3] AS arr UNION ALL
SELECT [4, 5] AS arr UNION ALL
SELECT [] AS arr
)
SELECT
arr,
ARRAY_REVERSE(arr) AS reverse_arr
FROM example;
+-----------+-------------+
| arr | reverse_arr |
+-----------+-------------+
| [1, 2, 3] | [3, 2, 1] |
| [4, 5] | [5, 4] |
| [] | [] |
+-----------+-------------+
SAFE_OFFSET and SAFE_ORDINAL
array_expression[SAFE_OFFSET(zero_based_offset)]
array_expression[SAFE_ORDINAL(one_based_offset)]
Description
Identical to OFFSET and ORDINAL, except returns NULL if the index is out
of range.
Return type
Varies depending on the elements in the ARRAY.
Example
WITH items AS
(SELECT ["apples", "bananas", "pears", "grapes"] as list
UNION ALL
SELECT ["coffee", "tea", "milk" ] as list
UNION ALL
SELECT ["cake", "pie"] as list)
SELECT list,
list[SAFE_OFFSET(3)] as safe_offset_3,
list[SAFE_ORDINAL(3)] as safe_ordinal_3
FROM items;
+----------------------------------+---------------+----------------+
| list | safe_offset_3 | safe_ordinal_3 |
+----------------------------------+---------------+----------------+
| [apples, bananas, pears, grapes] | grapes | pears |
| [coffee, tea, milk] | NULL | milk |
| [cake, pie] | NULL | NULL |
+----------------------------------+---------------+----------------+
Date functions
BigQuery supports the following DATE functions.
CURRENT_DATE
CURRENT_DATE([time_zone])
Description
Returns the current date as of the specified or default timezone. Parentheses are optional when called with no arguments.
This function supports an optional
time_zone parameter. This parameter is a string representing the timezone to
use. If no timezone is specified, the default timezone, UTC,
is used. See Timezone definitions
for information on how to specify a time zone.
If the time_zone parameter evaluates to NULL, this function returns NULL.
Return Data Type
DATE
Example
SELECT CURRENT_DATE() AS the_date;
+--------------+
| the_date |
+--------------+
| 2016-12-25 |
+--------------+
When a column named current_date is present, the column name and the function
call without parentheses are ambiguous. To ensure the function call, add
parentheses; to ensure the column name, qualify it with its
range variable. For example, the
following query will select the function in the the_date column and the table
column in the current_date column.
WITH t AS (SELECT 'column value' AS `current_date`)
SELECT current_date() AS the_date, t.current_date FROM t;
+------------+--------------+
| the_date | current_date |
+------------+--------------+
| 2016-12-25 | column value |
+------------+--------------+
EXTRACT
EXTRACT(part FROM date_expression)
Description
Returns the value corresponding to the specified date part. The part must
be one of:
DAYOFWEEK: Returns values in the range [1,7] with Sunday as the first day of the week.DAYDAYOFYEARWEEK: Returns the week number of the date in the range [0, 53]. Weeks begin with Sunday, and dates prior to the first Sunday of the year are in week 0.WEEK(<WEEKDAY>): Returns the week number of the date in the range [0, 53]. Weeks begin onWEEKDAY. Dates prior to the firstWEEKDAYof the year are in week 0. Valid values forWEEKDAYareSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK: Returns the ISO 8601 week number of thedate_expression.ISOWEEKs begin on Monday. Return values are in the range [1, 53]. The firstISOWEEKof each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year.MONTHQUARTER: Returns values in the range [1,4].YEARISOYEAR: Returns the ISO 8601 week-numbering year, which is the Gregorian calendar year containing the Thursday of the week to whichdate_expressionbelongs.
Return Data Type
INT64
Examples
In the following example, EXTRACT returns a value corresponding to the DAY
date part.
SELECT EXTRACT(DAY FROM DATE '2013-12-25') AS the_day;
+---------+
| the_day |
+---------+
| 25 |
+---------+
In the following example, EXTRACT returns values corresponding to different
date parts from a column of dates near the end of the year.
SELECT
date,
EXTRACT(ISOYEAR FROM date) AS isoyear,
EXTRACT(ISOWEEK FROM date) AS isoweek,
EXTRACT(YEAR FROM date) AS year,
EXTRACT(WEEK FROM date) AS week
FROM UNNEST(GENERATE_DATE_ARRAY('2015-12-23', '2016-01-09')) AS date
ORDER BY date;
+------------+---------+---------+------+------+
| date | isoyear | isoweek | year | week |
+------------+---------+---------+------+------+
| 2015-12-23 | 2015 | 52 | 2015 | 51 |
| 2015-12-24 | 2015 | 52 | 2015 | 51 |
| 2015-12-25 | 2015 | 52 | 2015 | 51 |
| 2015-12-26 | 2015 | 52 | 2015 | 51 |
| 2015-12-27 | 2015 | 52 | 2015 | 52 |
| 2015-12-28 | 2015 | 53 | 2015 | 52 |
| 2015-12-29 | 2015 | 53 | 2015 | 52 |
| 2015-12-30 | 2015 | 53 | 2015 | 52 |
| 2015-12-31 | 2015 | 53 | 2015 | 52 |
| 2016-01-01 | 2015 | 53 | 2016 | 0 |
| 2016-01-02 | 2015 | 53 | 2016 | 0 |
| 2016-01-03 | 2015 | 53 | 2016 | 1 |
| 2016-01-04 | 2016 | 1 | 2016 | 1 |
| 2016-01-05 | 2016 | 1 | 2016 | 1 |
| 2016-01-06 | 2016 | 1 | 2016 | 1 |
| 2016-01-07 | 2016 | 1 | 2016 | 1 |
| 2016-01-08 | 2016 | 1 | 2016 | 1 |
| 2016-01-09 | 2016 | 1 | 2016 | 1 |
+------------+---------+---------+------+------+
In the following example, date_expression falls on a Sunday. EXTRACT
calculates the first column using weeks that begin on Sunday, and it calculates
the second column using weeks that begin on Monday.
WITH table AS (SELECT DATE('2017-11-05') AS date)
SELECT
date,
EXTRACT(WEEK(SUNDAY) FROM date) AS week_sunday,
EXTRACT(WEEK(MONDAY) FROM date) AS week_monday FROM table;
+------------+-------------+-------------+
| date | week_sunday | week_monday |
+------------+-------------+-------------+
| 2017-11-05 | 45 | 44 |
+------------+-------------+-------------+
DATE
1. DATE(year, month, day)
2. DATE(timestamp_expression[, timezone])
3. DATE(datetime_expression)
Description
- Constructs a DATE from INT64 values representing the year, month, and day.
- Extracts the DATE from a TIMESTAMP expression. It supports an optional parameter to specify a timezone. If no timezone is specified, the default timezone, UTC, is used.
- Extracts the DATE from a DATETIME expression.
Return Data Type
DATE
Example
SELECT
DATE(2016, 12, 25) AS date_ymd,
DATE(DATETIME "2016-12-25 23:59:59") AS date_dt,
DATE(TIMESTAMP "2016-12-25 05:30:00+07", "America/Los_Angeles") AS date_tstz;
+------------+------------+------------+
| date_ymd | date_dt | date_tstz |
+------------+------------+------------+
| 2016-12-25 | 2016-12-25 | 2016-12-24 |
+------------+------------+------------+
DATE_ADD
DATE_ADD(date_expression, INTERVAL int64_expression date_part)
Description
Adds a specified time interval to a DATE.
DATE_ADD supports the following date_part values:
DAYWEEK. Equivalent to 7DAYs.MONTHQUARTERYEAR
Special handling is required for MONTH, QUARTER, and YEAR parts when the date is at (or near) the last day of the month. If the resulting month has fewer days than the original date's day, then the result day is the last day of the new month.
Return Data Type
DATE
Example
SELECT DATE_ADD(DATE "2008-12-25", INTERVAL 5 DAY) AS five_days_later;
+--------------------+
| five_days_later |
+--------------------+
| 2008-12-30 |
+--------------------+
DATE_SUB
DATE_SUB(date_expression, INTERVAL int64_expression date_part)
Description
Subtracts a specified time interval from a DATE.
DATE_SUB supports the following date_part values:
DAYWEEK. Equivalent to 7DAYs.MONTHQUARTERYEAR
Special handling is required for MONTH, QUARTER, and YEAR parts when the date is at (or near) the last day of the month. If the resulting month has fewer days than the original date's day, then the result day is the last day of the new month.
Return Data Type
DATE
Example
SELECT DATE_SUB(DATE "2008-12-25", INTERVAL 5 DAY) AS five_days_ago;
+---------------+
| five_days_ago |
+---------------+
| 2008-12-20 |
+---------------+
DATE_DIFF
DATE_DIFF(date_expression_a, date_expression_b, date_part)
Description
Returns the number of whole specified date_part intervals between two DATE objects
(date_expression_a - date_expression_b).
If the first DATE is earlier than the second one,
the output is negative.
DATE_DIFF supports the following date_part values:
DAYWEEKThis date part begins on Sunday.WEEK(<WEEKDAY>): This date part begins onWEEKDAY. Valid values forWEEKDAYareSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK: Uses ISO 8601 week boundaries. ISO weeks begin on Monday.MONTHQUARTERYEARISOYEAR: Uses the ISO 8601 week-numbering year boundary. The ISO year boundary is the Monday of the first week whose Thursday belongs to the corresponding Gregorian calendar year.
Return Data Type
INT64
Example
SELECT DATE_DIFF(DATE '2010-07-07', DATE '2008-12-25', DAY) AS days_diff;
+-----------+
| days_diff |
+-----------+
| 559 |
+-----------+
SELECT
DATE_DIFF(DATE '2017-10-15', DATE '2017-10-14', DAY) AS days_diff,
DATE_DIFF(DATE '2017-10-15', DATE '2017-10-14', WEEK) AS weeks_diff;
+-----------+------------+
| days_diff | weeks_diff |
+-----------+------------+
| 1 | 1 |
+-----------+------------+
The example above shows the result of DATE_DIFF for two days in succession.
DATE_DIFF with the date part WEEK returns 1 because DATE_DIFF counts the
number of date part boundaries in this range of dates. Each WEEK begins on
Sunday, so there is one date part boundary between Saturday, 2017-10-14
and Sunday, 2017-10-15.
The following example shows the result of DATE_DIFF for two dates in different
years. DATE_DIFF with the date part YEAR returns 3 because it counts the
number of Gregorian calendar year boundaries between the two dates. DATE_DIFF
with the date part ISOYEAR returns 2 because the second date belongs to the
ISO year 2015. The first Thursday of the 2015 calendar year was 2015-01-01, so
the ISO year 2015 begins on the preceding Monday, 2014-12-29.
SELECT
DATE_DIFF('2017-12-30', '2014-12-30', YEAR) AS year_diff,
DATE_DIFF('2017-12-30', '2014-12-30', ISOYEAR) AS isoyear_diff;
+-----------+--------------+
| year_diff | isoyear_diff |
+-----------+--------------+
| 3 | 2 |
+-----------+--------------+
The following example shows the result of DATE_DIFF for two days in
succession. The first date falls on a Monday and the second date falls on a
Sunday. DATE_DIFF with the date part WEEK returns 0 because this date part
uses weeks that begin on Sunday. DATE_DIFF with the date part WEEK(MONDAY)
returns 1. DATE_DIFF with the date part ISOWEEK also returns 1 because
ISO weeks begin on Monday.
SELECT
DATE_DIFF('2017-12-18', '2017-12-17', WEEK) AS week_diff,
DATE_DIFF('2017-12-18', '2017-12-17', WEEK(MONDAY)) AS week_weekday_diff,
DATE_DIFF('2017-12-18', '2017-12-17', ISOWEEK) AS isoweek_diff;
+-----------+-------------------+--------------+
| week_diff | week_weekday_diff | isoweek_diff |
+-----------+-------------------+--------------+
| 0 | 1 | 1 |
+-----------+-------------------+--------------+
DATE_TRUNC
DATE_TRUNC(date_expression, date_part)
Description
Truncates the date to the specified granularity.
DATE_TRUNC supports the following values for date_part:
DAYWEEKWEEK(<WEEKDAY>): Truncatesdate_expressionto the preceding week boundary, where weeks begin onWEEKDAY. Valid values forWEEKDAYareSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK: Truncatesdate_expressionto the preceding ISO 8601 week boundary.ISOWEEKs begin on Monday. The firstISOWEEKof each ISO year contains the first Thursday of the corresponding Gregorian calendar year. Anydate_expressionearlier than this will truncate to the preceding Monday.MONTHQUARTERYEARISOYEAR: Truncatesdate_expressionto the preceding ISO 8601 week-numbering year boundary. The ISO year boundary is the Monday of the first week whose Thursday belongs to the corresponding Gregorian calendar year.
Return Data Type
DATE
Examples
SELECT DATE_TRUNC(DATE '2008-12-25', MONTH) AS month;
+------------+
| month |
+------------+
| 2008-12-01 |
+------------+
In the following example, the original date falls on a Sunday. Because
the date_part is WEEK(MONDAY), DATE_TRUNC returns the DATE for the
preceding Monday.
SELECT date AS original, DATE_TRUNC(date, WEEK(MONDAY)) AS truncated
FROM (SELECT DATE('2017-11-05') AS date);
+------------+------------+
| original | truncated |
+------------+------------+
| 2017-11-05 | 2017-10-30 |
+------------+------------+
In the following example, the original date_expression is in the Gregorian
calendar year 2015. However, DATE_TRUNC with the ISOYEAR date part
truncates the date_expression to the beginning of the ISO year, not the
Gregorian calendar year. The first Thursday of the 2015 calendar year was
2015-01-01, so the ISO year 2015 begins on the preceding Monday, 2014-12-29.
Therefore the ISO year boundary preceding the date_expression 2015-06-15 is
2014-12-29.
SELECT
DATE_TRUNC('2015-06-15', ISOYEAR) AS isoyear_boundary,
EXTRACT(ISOYEAR FROM DATE '2015-06-15') AS isoyear_number;
+------------------+----------------+
| isoyear_boundary | isoyear_number |
+------------------+----------------+
| 2014-12-29 | 2015 |
+------------------+----------------+
DATE_FROM_UNIX_DATE
DATE_FROM_UNIX_DATE(int64_expression)
Description
Interprets int64_expression as the number of days since 1970-01-01.
Return Data Type
DATE
Example
SELECT DATE_FROM_UNIX_DATE(14238) AS date_from_epoch;
+-----------------+
| date_from_epoch |
+-----------------+
| 2008-12-25 |
+-----------------+
FORMAT_DATE
FORMAT_DATE(format_string, date_expr)
Description
Formats the date_expr according to the specified format_string.
See Supported Format Elements For DATE for a list of format elements that this function supports.
Return Data Type
STRING
Examples
SELECT FORMAT_DATE("%x", DATE "2008-12-25") AS US_format;
+------------+
| US_format |
+------------+
| 12/25/08 |
+------------+
SELECT FORMAT_DATE("%b-%d-%Y", DATE "2008-12-25") AS formatted;
+-------------+
| formatted |
+-------------+
| Dec-25-2008 |
+-------------+
SELECT FORMAT_DATE("%b %Y", DATE "2008-12-25") AS formatted;
+-------------+
| formatted |
+-------------+
| Dec 2008 |
+-------------+
LAST_DAY
LAST_DAY(date_expression[, date_part])
Description
Returns the last day from a date expression. This is commonly used to return the last day of the month.
You can optionally specify the date part for which the last day is returned.
If this parameter is not used, the default value is MONTH.
LAST_DAY supports the following values for date_part:
YEARQUARTERMONTHWEEK. Equivalent to 7DAYs.WEEK(<WEEKDAY>).<WEEKDAY>represents the starting day of the week. Valid values areSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK. Uses ISO 8601 week boundaries. ISO weeks begin on Monday.ISOYEAR. Uses the ISO 8601 week-numbering year boundary. The ISO year boundary is the Monday of the first week whose Thursday belongs to the corresponding Gregorian calendar year.
Return Data Type
DATE
Example
These both return the last day of the month:
SELECT LAST_DAY(DATE '2008-11-25', MONTH) AS last_day
+------------+
| last_day |
+------------+
| 2008-11-30 |
+------------+
SELECT LAST_DAY(DATE '2008-11-25') AS last_day
+------------+
| last_day |
+------------+
| 2008-11-30 |
+------------+
This returns the last day of the year:
SELECT LAST_DAY(DATE '2008-11-25', YEAR) AS last_day
+------------+
| last_day |
+------------+
| 2008-12-31 |
+------------+
This returns the last day of the week for a week that starts on a Sunday:
SELECT LAST_DAY(DATE '2008-11-10', WEEK(SUNDAY)) AS last_day
+------------+
| last_day |
+------------+
| 2008-11-15 |
+------------+
This returns the last day of the week for a week that starts on a Monday:
SELECT LAST_DAY(DATE '2008-11-10', WEEK(MONDAY)) AS last_day
+------------+
| last_day |
+------------+
| 2008-11-16 |
+------------+
PARSE_DATE
PARSE_DATE(format_string, date_string)
Description
Converts a string representation of date to a
DATE object.
format_string contains the format elements
that define how date_string is formatted. Each element in
date_string must have a corresponding element in format_string. The
location of each element in format_string must match the location of
each element in date_string.
-- This works because elements on both sides match.
SELECT PARSE_DATE("%A %b %e %Y", "Thursday Dec 25 2008")
-- This doesn't work because the year element is in different locations.
SELECT PARSE_DATE("%Y %A %b %e", "Thursday Dec 25 2008")
-- This doesn't work because one of the year elements is missing.
SELECT PARSE_DATE("%A %b %e", "Thursday Dec 25 2008")
-- This works because %F can find all matching elements in date_string.
SELECT PARSE_DATE("%F", "2000-12-30")
The format string fully supports most format elements except for
%Q, %a, %A, %g,
%G, %j, %u, %U, %V, %w, and %W.
When using PARSE_DATE, keep the following in mind:
- Unspecified fields. Any unspecified field is initialized from
1970-01-01. - Case insensitive names. Names, such as
Monday,February, and so on, are case insensitive. - Whitespace. One or more consecutive white spaces in the format string matches zero or more consecutive white spaces in the date string. In addition, leading and trailing white spaces in the date string are always allowed -- even if they are not in the format string.
- Format precedence. When two (or more) format elements have overlapping
information (for example both
%Fand%Yaffect the year), the last one generally overrides any earlier ones.
Return Data Type
DATE
Examples
This example converts a MM/DD/YY formatted string to a DATE object:
SELECT PARSE_DATE("%x", "12/25/08") AS parsed;
+------------+
| parsed |
+------------+
| 2008-12-25 |
+------------+
This example converts a YYYYMMDD formatted string to a DATE object:
SELECT PARSE_DATE("%Y%m%d", "20081225") AS parsed;
+------------+
| parsed |
+------------+
| 2008-12-25 |
+------------+
UNIX_DATE
UNIX_DATE(date_expression)
Description
Returns the number of days since 1970-01-01.
Return Data Type
INT64
Example
SELECT UNIX_DATE(DATE "2008-12-25") AS days_from_epoch;
+-----------------+
| days_from_epoch |
+-----------------+
| 14238 |
+-----------------+
Supported format elements for DATE
Unless otherwise noted, DATE functions that use format strings support the following elements:
| Format element | Description | Example |
| %A | The full weekday name. | Wednesday |
| %a | The abbreviated weekday name. | Wed |
| %B | The full month name. | January |
| %b or %h | The abbreviated month name. | Jan |
| %C | The century (a year divided by 100 and truncated to an integer) as a decimal number (00-99). | 20 |
| %D | The date in the format %m/%d/%y. | 01/20/21 |
| %d | The day of the month as a decimal number (01-31). | 20 |
| %e | The day of month as a decimal number (1-31); single digits are preceded by a space. | 20 |
| %F | The date in the format %Y-%m-%d. | 2021-01-20 |
| %G | The ISO 8601 year with century as a decimal number. Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %G and %Y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge. | 2021 |
| %g | The ISO 8601 year without century as a decimal number (00-99). Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %g and %y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge. | 21 |
| %j | The day of the year as a decimal number (001-366). | 020 |
| %m | The month as a decimal number (01-12). | 01 |
| %n | A newline character. | |
| %Q | The quarter as a decimal number (1-4). | 1 |
| %t | A tab character. | |
| %U | The week number of the year (Sunday as the first day of the week) as a decimal number (00-53). | 03 |
| %u | The weekday (Monday as the first day of the week) as a decimal number (1-7). | 3 |
| %V | The ISO 8601 week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is week 53 of the previous year, and the next week is week 1. | 03 |
| %W | The week number of the year (Monday as the first day of the week) as a decimal number (00-53). | 03 |
| %w | The weekday (Sunday as the first day of the week) as a decimal number (0-6). | 3 |
| %x | The date representation in MM/DD/YY format. | 01/20/21 |
| %Y | The year with century as a decimal number. | 2021 |
| %y | The year without century as a decimal number (00-99), with an optional leading zero. Can be mixed with %C. If %C is not specified, years 00-68 are 2000s, while years 69-99 are 1900s. | 21 |
| %E4Y | Four-character years (0001 ... 9999). Note that %Y produces as many characters as it takes to fully render the year. | 2021 |
Datetime functions
BigQuery supports the following DATETIME functions.
All outputs are automatically formatted as per ISO 8601,
separating date and time with aT.
CURRENT_DATETIME
CURRENT_DATETIME([timezone])
Description
Returns the current time as a DATETIME object. Parentheses are optional when
called with no arguments.
This function supports an optional timezone parameter.
See Timezone definitions for
information on how to specify a time zone.
Return Data Type
DATETIME
Example
SELECT CURRENT_DATETIME() as now;
+----------------------------+
| now |
+----------------------------+
| 2016-05-19T10:38:47.046465 |
+----------------------------+
When a column named current_datetime is present, the column name and the
function call without parentheses are ambiguous. To ensure the function call,
add parentheses; to ensure the column name, qualify it with its
range variable. For example, the
following query will select the function in the now column and the table
column in the current_datetime column.
WITH t AS (SELECT 'column value' AS `current_datetime`)
SELECT current_datetime() as now, t.current_datetime FROM t;
+----------------------------+------------------+
| now | current_datetime |
+----------------------------+------------------+
| 2016-05-19T10:38:47.046465 | column value |
+----------------------------+------------------+
DATETIME
1. DATETIME(year, month, day, hour, minute, second)
2. DATETIME(date_expression[, time_expression])
3. DATETIME(timestamp_expression [, timezone])
Description
- Constructs a
DATETIMEobject using INT64 values representing the year, month, day, hour, minute, and second. - Constructs a
DATETIMEobject using a DATE object and an optional TIME object. - Constructs a
DATETIMEobject using a TIMESTAMP object. It supports an optional parameter to specify a timezone. If no timezone is specified, the default timezone, UTC, is used.
Return Data Type
DATETIME
Example
SELECT
DATETIME(2008, 12, 25, 05, 30, 00) as datetime_ymdhms,
DATETIME(TIMESTAMP "2008-12-25 05:30:00+00", "America/Los_Angeles") as datetime_tstz;
+---------------------+---------------------+
| datetime_ymdhms | datetime_tstz |
+---------------------+---------------------+
| 2008-12-25T05:30:00 | 2008-12-24T21:30:00 |
+---------------------+---------------------+
EXTRACT
EXTRACT(part FROM datetime_expression)
Description
Returns a value that corresponds to the
specified part from a supplied datetime_expression.
Allowed part values are:
MICROSECONDMILLISECONDSECONDMINUTEHOURDAYOFWEEKDAYDAYOFYEARWEEK: Returns the week number of the date in the range [0, 53]. Weeks begin with Sunday, and dates prior to the first Sunday of the year are in week 0.WEEK(<WEEKDAY>): Returns the week number ofdatetime_expressionin the range [0, 53]. Weeks begin onWEEKDAY.datetimes prior to the firstWEEKDAYof the year are in week 0. Valid values forWEEKDAYareSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK: Returns the ISO 8601 week number of thedatetime_expression.ISOWEEKs begin on Monday. Return values are in the range [1, 53]. The firstISOWEEKof each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year.MONTHQUARTERYEARISOYEAR: Returns the ISO 8601 week-numbering year, which is the Gregorian calendar year containing the Thursday of the week to whichdate_expressionbelongs.DATETIME
Returned values truncate lower order time periods. For example, when extracting
seconds, EXTRACT truncates the millisecond and microsecond values.
Return Data Type
INT64, except in the following cases:
- If
partisDATE, returns aDATEobject. - If
partisTIME, returns aTIMEobject.
Examples
In the following example, EXTRACT returns a value corresponding to the HOUR
time part.
SELECT EXTRACT(HOUR FROM DATETIME(2008, 12, 25, 15, 30, 00)) as hour;
+------------------+
| hour |
+------------------+
| 15 |
+------------------+
In the following example, EXTRACT returns values corresponding to different
time parts from a column of datetimes.
WITH Datetimes AS (
SELECT DATETIME '2005-01-03 12:34:56' AS datetime UNION ALL
SELECT DATETIME '2007-12-31' UNION ALL
SELECT DATETIME '2009-01-01' UNION ALL
SELECT DATETIME '2009-12-31' UNION ALL
SELECT DATETIME '2017-01-02' UNION ALL
SELECT DATETIME '2017-05-26'
)
SELECT
datetime,
EXTRACT(ISOYEAR FROM datetime) AS isoyear,
EXTRACT(ISOWEEK FROM datetime) AS isoweek,
EXTRACT(YEAR FROM datetime) AS year,
EXTRACT(WEEK FROM datetime) AS week
FROM Datetimes
ORDER BY datetime;
+---------------------+---------+---------+------+------+
| datetime | isoyear | isoweek | year | week |
+---------------------+---------+---------+------+------+
| 2005-01-03T12:34:56 | 2005 | 1 | 2005 | 1 |
| 2007-12-31T00:00:00 | 2008 | 1 | 2007 | 52 |
| 2009-01-01T00:00:00 | 2009 | 1 | 2009 | 0 |
| 2009-12-31T00:00:00 | 2009 | 53 | 2009 | 52 |
| 2017-01-02T00:00:00 | 2017 | 1 | 2017 | 1 |
| 2017-05-26T00:00:00 | 2017 | 21 | 2017 | 21 |
+---------------------+---------+---------+------+------+
In the following example, datetime_expression falls on a Sunday. EXTRACT
calculates the first column using weeks that begin on Sunday, and it calculates
the second column using weeks that begin on Monday.
WITH table AS (SELECT DATETIME(TIMESTAMP "2017-11-05 00:00:00+00", "UTC") AS datetime)
SELECT
datetime,
EXTRACT(WEEK(SUNDAY) FROM datetime) AS week_sunday,
EXTRACT(WEEK(MONDAY) FROM datetime) AS week_monday
FROM table;
+---------------------+-------------+---------------+
| datetime | week_sunday | week_monday |
+---------------------+-------------+---------------+
| 2017-11-05T00:00:00 | 45 | 44 |
+---------------------+-------------+---------------+
DATETIME_ADD
DATETIME_ADD(datetime_expression, INTERVAL int64_expression part)
Description
Adds int64_expression units of part to the DATETIME object.
DATETIME_ADD supports the following values for part:
MICROSECONDMILLISECONDSECONDMINUTEHOURDAYWEEK. Equivalent to 7DAYs.MONTHQUARTERYEAR
Special handling is required for MONTH, QUARTER, and YEAR parts when the date is at (or near) the last day of the month. If the resulting month has fewer days than the original DATETIME's day, then the result day is the last day of the new month.
Return Data Type
DATETIME
Example
SELECT
DATETIME "2008-12-25 15:30:00" as original_date,
DATETIME_ADD(DATETIME "2008-12-25 15:30:00", INTERVAL 10 MINUTE) as later;
+-----------------------------+------------------------+
| original_date | later |
+-----------------------------+------------------------+
| 2008-12-25T15:30:00 | 2008-12-25T15:40:00 |
+-----------------------------+------------------------+
DATETIME_SUB
DATETIME_SUB(datetime_expression, INTERVAL int64_expression part)
Description
Subtracts int64_expression units of part from the DATETIME.
DATETIME_SUB supports the following values for part:
MICROSECONDMILLISECONDSECONDMINUTEHOURDAYWEEK. Equivalent to 7DAYs.MONTHQUARTERYEAR
Special handling is required for MONTH, QUARTER, and YEAR parts when the
date is at (or near) the last day of the month. If the resulting month has fewer
days than the original DATETIME's day, then the result day is the last day of
the new month.
Return Data Type
DATETIME
Example
SELECT
DATETIME "2008-12-25 15:30:00" as original_date,
DATETIME_SUB(DATETIME "2008-12-25 15:30:00", INTERVAL 10 MINUTE) as earlier;
+-----------------------------+------------------------+
| original_date | earlier |
+-----------------------------+------------------------+
| 2008-12-25T15:30:00 | 2008-12-25T15:20:00 |
+-----------------------------+------------------------+
DATETIME_DIFF
DATETIME_DIFF(datetime_expression_a, datetime_expression_b, part)
Description
Returns the number of whole specified part intervals between two
DATETIME objects (datetime_expression_a - datetime_expression_b).
If the first DATETIME is earlier than the second one,
the output is negative. Throws an error if the computation overflows the
result type, such as if the difference in
microseconds
between the two DATETIME objects would overflow an
INT64 value.
DATETIME_DIFF supports the following values for part:
MICROSECONDMILLISECONDSECONDMINUTEHOURDAYWEEK: This date part begins on Sunday.WEEK(<WEEKDAY>): This date part begins onWEEKDAY. Valid values forWEEKDAYareSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK: Uses ISO 8601 week boundaries. ISO weeks begin on Monday.MONTHQUARTERYEARISOYEAR: Uses the ISO 8601 week-numbering year boundary. The ISO year boundary is the Monday of the first week whose Thursday belongs to the corresponding Gregorian calendar year.
Return Data Type
INT64
Example
SELECT
DATETIME "2010-07-07 10:20:00" as first_datetime,
DATETIME "2008-12-25 15:30:00" as second_datetime,
DATETIME_DIFF(DATETIME "2010-07-07 10:20:00",
DATETIME "2008-12-25 15:30:00", DAY) as difference;
+----------------------------+------------------------+------------------------+
| first_datetime | second_datetime | difference |
+----------------------------+------------------------+------------------------+
| 2010-07-07T10:20:00 | 2008-12-25T15:30:00 | 559 |
+----------------------------+------------------------+------------------------+
SELECT
DATETIME_DIFF(DATETIME '2017-10-15 00:00:00',
DATETIME '2017-10-14 00:00:00', DAY) as days_diff,
DATETIME_DIFF(DATETIME '2017-10-15 00:00:00',
DATETIME '2017-10-14 00:00:00', WEEK) as weeks_diff;
+-----------+------------+
| days_diff | weeks_diff |
+-----------+------------+
| 1 | 1 |
+-----------+------------+
The example above shows the result of DATETIME_DIFF for two DATETIMEs that
are 24 hours apart. DATETIME_DIFF with the part WEEK returns 1 because
DATETIME_DIFF counts the number of part boundaries in this range of
DATETIMEs. Each WEEK begins on Sunday, so there is one part boundary between
Saturday, 2017-10-14 00:00:00 and Sunday, 2017-10-15 00:00:00.
The following example shows the result of DATETIME_DIFF for two dates in
different years. DATETIME_DIFF with the date part YEAR returns 3 because it
counts the number of Gregorian calendar year boundaries between the two
DATETIMEs. DATETIME_DIFF with the date part ISOYEAR returns 2 because the
second DATETIME belongs to the ISO year 2015. The first Thursday of the 2015
calendar year was 2015-01-01, so the ISO year 2015 begins on the preceding
Monday, 2014-12-29.
SELECT
DATETIME_DIFF('2017-12-30 00:00:00',
'2014-12-30 00:00:00', YEAR) AS year_diff,
DATETIME_DIFF('2017-12-30 00:00:00',
'2014-12-30 00:00:00', ISOYEAR) AS isoyear_diff;
+-----------+--------------+
| year_diff | isoyear_diff |
+-----------+--------------+
| 3 | 2 |
+-----------+--------------+
The following example shows the result of DATETIME_DIFF for two days in
succession. The first date falls on a Monday and the second date falls on a
Sunday. DATETIME_DIFF with the date part WEEK returns 0 because this time
part uses weeks that begin on Sunday. DATETIME_DIFF with the date part
WEEK(MONDAY) returns 1. DATETIME_DIFF with the date part
ISOWEEK also returns 1 because ISO weeks begin on Monday.
SELECT
DATETIME_DIFF('2017-12-18', '2017-12-17', WEEK) AS week_diff,
DATETIME_DIFF('2017-12-18', '2017-12-17', WEEK(MONDAY)) AS week_weekday_diff,
DATETIME_DIFF('2017-12-18', '2017-12-17', ISOWEEK) AS isoweek_diff;
+-----------+-------------------+--------------+
| week_diff | week_weekday_diff | isoweek_diff |
+-----------+-------------------+--------------+
| 0 | 1 | 1 |
+-----------+-------------------+--------------+
DATETIME_TRUNC
DATETIME_TRUNC(datetime_expression, part)
Description
Truncates a DATETIME object to the granularity of part.
DATETIME_TRUNC supports the following values for part:
MICROSECONDMILLISECONDSECONDMINUTEHOURDAYWEEKWEEK(<WEEKDAY>): Truncatesdatetime_expressionto the preceding week boundary, where weeks begin onWEEKDAY. Valid values forWEEKDAYareSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK: Truncatesdatetime_expressionto the preceding ISO 8601 week boundary.ISOWEEKs begin on Monday. The firstISOWEEKof each ISO year contains the first Thursday of the corresponding Gregorian calendar year. Anydate_expressionearlier than this will truncate to the preceding Monday.MONTHQUARTERYEARISOYEAR: Truncatesdatetime_expressionto the preceding ISO 8601 week-numbering year boundary. The ISO year boundary is the Monday of the first week whose Thursday belongs to the corresponding Gregorian calendar year.
Return Data Type
DATETIME
Examples
SELECT
DATETIME "2008-12-25 15:30:00" as original,
DATETIME_TRUNC(DATETIME "2008-12-25 15:30:00", DAY) as truncated;
+----------------------------+------------------------+
| original | truncated |
+----------------------------+------------------------+
| 2008-12-25T15:30:00 | 2008-12-25T00:00:00 |
+----------------------------+------------------------+
In the following example, the original DATETIME falls on a Sunday. Because the
part is WEEK(MONDAY), DATE_TRUNC returns the DATETIME for the
preceding Monday.
SELECT
datetime AS original,
DATETIME_TRUNC(datetime, WEEK(MONDAY)) AS truncated
FROM (SELECT DATETIME(TIMESTAMP "2017-11-05 00:00:00+00", "UTC") AS datetime);
+---------------------+---------------------+
| original | truncated |
+---------------------+---------------------+
| 2017-11-05T00:00:00 | 2017-10-30T00:00:00 |
+---------------------+---------------------+
In the following example, the original datetime_expression is in the Gregorian
calendar year 2015. However, DATETIME_TRUNC with the ISOYEAR date part
truncates the datetime_expression to the beginning of the ISO year, not the
Gregorian calendar year. The first Thursday of the 2015 calendar year was
2015-01-01, so the ISO year 2015 begins on the preceding Monday, 2014-12-29.
Therefore the ISO year boundary preceding the datetime_expression
2015-06-15 00:00:00 is 2014-12-29.
SELECT
DATETIME_TRUNC('2015-06-15 00:00:00', ISOYEAR) AS isoyear_boundary,
EXTRACT(ISOYEAR FROM DATETIME '2015-06-15 00:00:00') AS isoyear_number;
+---------------------+----------------+
| isoyear_boundary | isoyear_number |
+---------------------+----------------+
| 2014-12-29T00:00:00 | 2015 |
+---------------------+----------------+
FORMAT_DATETIME
FORMAT_DATETIME(format_string, datetime_expression)
Description
Formats a DATETIME object according to the specified format_string. See
Supported Format Elements For DATETIME
for a list of format elements that this function supports.
Return Data Type
STRING
Examples
SELECT
FORMAT_DATETIME("%c", DATETIME "2008-12-25 15:30:00")
AS formatted;
+--------------------------+
| formatted |
+--------------------------+
| Thu Dec 25 15:30:00 2008 |
+--------------------------+
SELECT
FORMAT_DATETIME("%b-%d-%Y", DATETIME "2008-12-25 15:30:00")
AS formatted;
+-------------+
| formatted |
+-------------+
| Dec-25-2008 |
+-------------+
SELECT
FORMAT_DATETIME("%b %Y", DATETIME "2008-12-25 15:30:00")
AS formatted;
+-------------+
| formatted |
+-------------+
| Dec 2008 |
+-------------+
LAST_DAY
LAST_DAY(datetime_expression[, date_part])
Description
Returns the last day from a datetime expression that contains the date. This is commonly used to return the last day of the month.
You can optionally specify the date part for which the last day is returned.
If this parameter is not used, the default value is MONTH.
LAST_DAY supports the following values for date_part:
YEARQUARTERMONTHWEEK. Equivalent to 7DAYs.WEEK(<WEEKDAY>).<WEEKDAY>represents the starting day of the week. Valid values areSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK. Uses ISO 8601 week boundaries. ISO weeks begin on Monday.ISOYEAR. Uses the ISO 8601 week-numbering year boundary. The ISO year boundary is the Monday of the first week whose Thursday belongs to the corresponding Gregorian calendar year.
Return Data Type
DATE
Example
These both return the last day of the month:
SELECT LAST_DAY(DATETIME '2008-11-25', MONTH) AS last_day
+------------+
| last_day |
+------------+
| 2008-11-30 |
+------------+
SELECT LAST_DAY(DATETIME '2008-11-25') AS last_day
+------------+
| last_day |
+------------+
| 2008-11-30 |
+------------+
This returns the last day of the year:
SELECT LAST_DAY(DATETIME '2008-11-25 15:30:00', YEAR) AS last_day
+------------+
| last_day |
+------------+
| 2008-12-31 |
+------------+
This returns the last day of the week for a week that starts on a Sunday:
SELECT LAST_DAY(DATETIME '2008-11-10 15:30:00', WEEK(SUNDAY)) AS last_day
+------------+
| last_day |
+------------+
| 2008-11-15 |
+------------+
This returns the last day of the week for a week that starts on a Monday:
SELECT LAST_DAY(DATETIME '2008-11-10 15:30:00', WEEK(MONDAY)) AS last_day
+------------+
| last_day |
+------------+
| 2008-11-16 |
+------------+
PARSE_DATETIME
PARSE_DATETIME(format_string, datetime_string)
Description
Converts a string representation of a datetime to a
DATETIME object.
format_string contains the format elements
that define how datetime_string is formatted. Each element in
datetime_string must have a corresponding element in format_string. The
location of each element in format_string must match the location of
each element in datetime_string.
-- This works because elements on both sides match.
SELECT PARSE_DATETIME("%a %b %e %I:%M:%S %Y", "Thu Dec 25 07:30:00 2008")
-- This doesn't work because the year element is in different locations.
SELECT PARSE_DATETIME("%a %b %e %Y %I:%M:%S", "Thu Dec 25 07:30:00 2008")
-- This doesn't work because one of the year elements is missing.
SELECT PARSE_DATETIME("%a %b %e %I:%M:%S", "Thu Dec 25 07:30:00 2008")
-- This works because %c can find all matching elements in datetime_string.
SELECT PARSE_DATETIME("%c", "Thu Dec 25 07:30:00 2008")
The format string fully supports most format elements, except for
%Q, %a, %A,
%g, %G, %j, %P, %u, %U, %V, %w, and %W.
PARSE_DATETIME parses string according to the following rules:
- Unspecified fields. Any unspecified field is initialized from
1970-01-01 00:00:00.0. For example, if the year is unspecified then it defaults to1970. - Case insensitive names. Names, such as
MondayandFebruary, are case insensitive. - Whitespace. One or more consecutive white spaces in the format string
matches zero or more consecutive white spaces in the
DATETIMEstring. Leading and trailing white spaces in theDATETIMEstring are always allowed, even if they are not in the format string. - Format precedence. When two or more format elements have overlapping
information, the last one generally overrides any earlier ones, with some
exceptions. For example, both
%Fand%Yaffect the year, so the earlier element overrides the later. See the descriptions of%s,%C, and%yin Supported Format Elements For DATETIME.
Return Data Type
DATETIME
Examples
The following examples parse a STRING literal as a
DATETIME.
SELECT PARSE_DATETIME('%Y-%m-%d %H:%M:%S', '1998-10-18 13:45:55') AS datetime;
+---------------------+
| datetime |
+---------------------+
| 1998-10-18T13:45:55 |
+---------------------+
SELECT PARSE_DATETIME('%m/%d/%Y %I:%M:%S %p', '8/30/2018 2:23:38 PM') AS datetime
+---------------------+
| datetime |
+---------------------+
| 2018-08-30T14:23:38 |
+---------------------+
The following example parses a STRING literal
containing a date in a natural language format as a
DATETIME.
SELECT PARSE_DATETIME('%A, %B %e, %Y','Wednesday, December 19, 2018')
AS datetime;
+---------------------+
| datetime |
+---------------------+
| 2018-12-19T00:00:00 |
+---------------------+
Supported format elements for DATETIME
Unless otherwise noted, DATETIME functions that use format strings support the
following elements:
| Format element | Description | Example |
| %A | The full weekday name. | Wednesday |
| %a | The abbreviated weekday name. | Wed |
| %B | The full month name. | January |
| %b or %h | The abbreviated month name. | Jan |
| %C | The century (a year divided by 100 and truncated to an integer) as a decimal number (00-99). | 20 |
| %c | The date and time representation. | Wed Jan 20 21:47:00 2021 |
| %D | The date in the format %m/%d/%y. | 01/20/21 |
| %d | The day of the month as a decimal number (01-31). | 20 |
| %e | The day of month as a decimal number (1-31); single digits are preceded by a space. | 20 |
| %F | The date in the format %Y-%m-%d. | 2021-01-20 |
| %G | The ISO 8601 year with century as a decimal number. Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %G and %Y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge. | 2021 |
| %g | The ISO 8601 year without century as a decimal number (00-99). Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %g and %y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge. | 21 |
| %H | The hour (24-hour clock) as a decimal number (00-23). | 21 |
| %I | The hour (12-hour clock) as a decimal number (01-12). | 09 |
| %j | The day of the year as a decimal number (001-366). | 020 |
| %k | The hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a space. | 21 |
| %l | The hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a space. | 9 |
| %M | The minute as a decimal number (00-59). | |
| %m | The month as a decimal number (01-12). | 01 |
| %n | A newline character. | |
| %P | Either am or pm. | pm |
| %p | Either AM or PM. | PM |
| %Q | The quarter as a decimal number (1-4). | 1 |
| %R | The time in the format %H:%M. | 21:47 |
| %r | The 12-hour clock time using AM/PM notation. | 09:47:00 PM |
| %S | The second as a decimal number (00-60). | 00 |
| %s | The number of seconds since 1970-01-01 00:00:00. Always overrides all other format elements, independent of where %s appears in the string. If multiple %s elements appear, then the last one takes precedence. | 1611179220 |
| %T | The time in the format %H:%M:%S. | 21:47:00 |
| %t | A tab character. | |
| %U | The week number of the year (Sunday as the first day of the week) as a decimal number (00-53). | 03 |
| %u | The weekday (Monday as the first day of the week) as a decimal number (1-7). | 3 |
| %V | The ISO 8601 week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is week 53 of the previous year, and the next week is week 1. | 03 |
| %W | The week number of the year (Monday as the first day of the week) as a decimal number (00-53). | 03 |
| %w | The weekday (Sunday as the first day of the week) as a decimal number (0-6). | 3 |
| %X | The time representation in HH:MM:SS format. | 21:47:00 |
| %x | The date representation in MM/DD/YY format. | 01/20/21 |
| %Y | The year with century as a decimal number. | 2021 |
| %y | The year without century as a decimal number (00-99), with an optional leading zero. Can be mixed with %C. If %C is not specified, years 00-68 are 2000s, while years 69-99 are 1900s. | 21 |
| %% | A single % character. | % |
| %E#S | Seconds with # digits of fractional precision. | 00.000 |
| %E*S | Seconds with full fractional precision (a literal '*'). | 00 |
| %E4Y | Four-character years (0001 ... 9999). Note that %Y produces as many characters as it takes to fully render the year. | 2021 |
Time functions
BigQuery supports the following TIME functions.
CURRENT_TIME
CURRENT_TIME([timezone])
Description
Returns the current time as a TIME object. Parentheses are optional when
called with no arguments.
This function supports an optional timezone parameter.
See Timezone definitions for information
on how to specify a time zone.
Return Data Type
TIME
Example
SELECT CURRENT_TIME() as now;
+----------------------------+
| now |
+----------------------------+
| 15:31:38.776361 |
+----------------------------+
When a column named current_time is present, the column name and the function
call without parentheses are ambiguous. To ensure the function call, add
parentheses; to ensure the column name, qualify it with its
range variable. For example, the
following query will select the function in the now column and the table
column in the current_time column.
WITH t AS (SELECT 'column value' AS `current_time`)
SELECT current_time() as now, t.current_time FROM t;
+-----------------+--------------+
| now | current_time |
+-----------------+--------------+
| 15:31:38.776361 | column value |
+-----------------+--------------+
TIME
1. TIME(hour, minute, second)
2. TIME(timestamp, [timezone])
3. TIME(datetime)
Description
- Constructs a
TIMEobject usingINT64values representing the hour, minute, and second. - Constructs a
TIMEobject using aTIMESTAMPobject. It supports an optional parameter to specify a timezone. If no timezone is specified, the default timezone, UTC, is used. - Constructs a
TIMEobject using aDATETIMEobject.
Return Data Type
TIME
Example
SELECT
TIME(15, 30, 00) as time_hms,
TIME(TIMESTAMP "2008-12-25 15:30:00+08", "America/Los_Angeles") as time_tstz;
+----------+-----------+
| time_hms | time_tstz |
+----------+-----------+
| 15:30:00 | 23:30:00 |
+----------+-----------+
SELECT TIME(DATETIME "2008-12-25 15:30:00.000000") AS time_dt;
+----------+
| time_dt |
+----------+
| 15:30:00 |
+----------+
EXTRACT
EXTRACT(part FROM time_expression)
Description
Returns a value that corresponds to the specified part from
a supplied time_expression.
Allowed part values are:
MICROSECONDMILLISECONDSECONDMINUTEHOUR
Returned values truncate lower order time periods. For example, when extracting
seconds, EXTRACT truncates the millisecond and microsecond values.
Return Data Type
INT64
Example
In the following example, EXTRACT returns a value corresponding to the HOUR
time part.
SELECT EXTRACT(HOUR FROM TIME "15:30:00") as hour;
+------------------+
| hour |
+------------------+
| 15 |
+------------------+
TIME_ADD
TIME_ADD(time_expression, INTERVAL int64_expression part)
Description
Adds int64_expression units of part to the TIME object.
TIME_ADD supports the following values for part:
MICROSECONDMILLISECONDSECONDMINUTEHOUR
This function automatically adjusts when values fall outside of the 00:00:00 to
24:00:00 boundary. For example, if you add an hour to 23:30:00, the returned
value is 00:30:00.
Return Data Types
TIME
Example
SELECT
TIME "15:30:00" as original_time,
TIME_ADD(TIME "15:30:00", INTERVAL 10 MINUTE) as later;
+-----------------------------+------------------------+
| original_time | later |
+-----------------------------+------------------------+
| 15:30:00 | 15:40:00 |
+-----------------------------+------------------------+
TIME_SUB
TIME_SUB(time_expression, INTERVAL int64_expression part)
Description
Subtracts int64_expression units of part from the TIME object.
TIME_SUB supports the following values for part:
MICROSECONDMILLISECONDSECONDMINUTEHOUR
This function automatically adjusts when values fall outside of the 00:00:00 to
24:00:00 boundary. For example, if you subtract an hour from 00:30:00, the
returned value is 23:30:00.
Return Data Type
TIME
Example
SELECT
TIME "15:30:00" as original_date,
TIME_SUB(TIME "15:30:00", INTERVAL 10 MINUTE) as earlier;
+-----------------------------+------------------------+
| original_date | earlier |
+-----------------------------+------------------------+
| 15:30:00 | 15:20:00 |
+-----------------------------+------------------------+
TIME_DIFF
TIME_DIFF(time_expression_a, time_expression_b, part)
Description
Returns the number of whole specified part intervals between two
TIME objects (time_expression_a - time_expression_b). If the first
TIME is earlier than the second one, the output is negative. Throws an error
if the computation overflows the result type, such as if the difference in
microseconds
between the two TIME objects would overflow an
INT64 value.
TIME_DIFF supports the following values for part:
MICROSECONDMILLISECONDSECONDMINUTEHOUR
Return Data Type
INT64
Example
SELECT
TIME "15:30:00" as first_time,
TIME "14:35:00" as second_time,
TIME_DIFF(TIME "15:30:00", TIME "14:35:00", MINUTE) as difference;
+----------------------------+------------------------+------------------------+
| first_time | second_time | difference |
+----------------------------+------------------------+------------------------+
| 15:30:00 | 14:35:00 | 55 |
+----------------------------+------------------------+------------------------+
TIME_TRUNC
TIME_TRUNC(time_expression, part)
Description
Truncates a TIME object to the granularity of part.
TIME_TRUNC supports the following values for part:
MICROSECONDMILLISECONDSECONDMINUTEHOUR
Return Data Type
TIME
Example
SELECT
TIME "15:30:00" as original,
TIME_TRUNC(TIME "15:30:00", HOUR) as truncated;
+----------------------------+------------------------+
| original | truncated |
+----------------------------+------------------------+
| 15:30:00 | 15:00:00 |
+----------------------------+------------------------+
FORMAT_TIME
FORMAT_TIME(format_string, time_object)
Description
Formats a TIME object according to the specified format_string. See
Supported Format Elements For TIME
for a list of format elements that this function supports.
Return Data Type
STRING
Example
SELECT FORMAT_TIME("%R", TIME "15:30:00") as formatted_time;
+----------------+
| formatted_time |
+----------------+
| 15:30 |
+----------------+
PARSE_TIME
PARSE_TIME(format_string, time_string)
Description
Converts a string representation of time to a
TIME object.
format_string contains the format elements
that define how time_string is formatted. Each element in
time_string must have a corresponding element in format_string. The
location of each element in format_string must match the location of
each element in time_string.
-- This works because elements on both sides match.
SELECT PARSE_TIME("%I:%M:%S", "07:30:00")
-- This doesn't work because the seconds element is in different locations.
SELECT PARSE_TIME("%S:%I:%M", "07:30:00")
-- This doesn't work because one of the seconds elements is missing.
SELECT PARSE_TIME("%I:%M", "07:30:00")
-- This works because %T can find all matching elements in time_string.
SELECT PARSE_TIME("%T", "07:30:00")
The format string fully supports most format elements except for %P.
When using PARSE_TIME, keep the following in mind:
- Unspecified fields. Any unspecified field is initialized from
00:00:00.0. For instance, ifsecondsis unspecified then it defaults to00, and so on. - Whitespace. One or more consecutive white spaces in the format string
matches zero or more consecutive white spaces in the
TIMEstring. In addition, leading and trailing white spaces in theTIMEstring are always allowed, even if they are not in the format string. - Format precedence. When two (or more) format elements have overlapping information, the last one generally overrides any earlier ones.
Return Data Type
TIME
Example
SELECT PARSE_TIME("%H", "15") as parsed_time;
+-------------+
| parsed_time |
+-------------+
| 15:00:00 |
+-------------+
SELECT PARSE_TIME('%I:%M:%S %p', '2:23:38 PM') AS parsed_time
+-------------+
| parsed_time |
+-------------+
| 14:23:38 |
+-------------+
Supported format elements for TIME
Unless otherwise noted, TIME functions that use format strings support the
following elements:
| Format element | Description | Example |
| %H | The hour (24-hour clock) as a decimal number (00-23). | 21 |
| %I | The hour (12-hour clock) as a decimal number (01-12). | 09 |
| %k | The hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a space. | 21 |
| %l | The hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a space. | 9 |
| %M | The minute as a decimal number (00-59). | 47 |
| %n | A newline character. | |
| %P | Either am or pm. | pm |
| %p | Either AM or PM. | PM |
| %R | The time in the format %H:%M. | 21:47 |
| %r | The 12-hour clock time using AM/PM notation. | 09:47:00 PM |
| %S | The second as a decimal number (00-60). | 00 |
| %T | The time in the format %H:%M:%S. | 21:47:00 |
| %t | A tab character. | |
| %X | The time representation in HH:MM:SS format. | 21:47:00 |
| %% | A single % character. | % |
| %E#S | Seconds with # digits of fractional precision. | 00.000 |
| %E*S | Seconds with full fractional precision (a literal '*'). | 00 |
Timestamp functions
BigQuery supports the following TIMESTAMP functions.
NOTE: These functions return a runtime error if overflow occurs; result values are bounded by the defined date and timestamp min/max values.
CURRENT_TIMESTAMP
CURRENT_TIMESTAMP()
Description
CURRENT_TIMESTAMP() produces a TIMESTAMP value that is continuous,
non-ambiguous, has exactly 60 seconds per minute and does not repeat values over
the leap second. Parentheses are optional.
This function handles leap seconds by smearing them across a window of 20 hours around the inserted leap second.
Supported Input Types
Not applicable
Result Data Type
TIMESTAMP
Examples
SELECT CURRENT_TIMESTAMP() as now;
+--------------------------------+
| now |
+--------------------------------+
| 2020-06-02 23:57:12.120174 UTC |
+--------------------------------+
When a column named current_timestamp is present, the column name and the
function call without parentheses are ambiguous. To ensure the function call,
add parentheses; to ensure the column name, qualify it with its
range variable. For example, the
following query will select the function in the now column and the table
column in the current_timestamp column.
WITH t AS (SELECT 'column value' AS `current_timestamp`)
SELECT current_timestamp() AS now, t.current_timestamp FROM t;
+--------------------------------+-------------------+
| now | current_timestamp |
+--------------------------------+-------------------+
| 2020-06-02 23:57:12.120174 UTC | column value |
+--------------------------------+-------------------+
EXTRACT
EXTRACT(part FROM timestamp_expression [AT TIME ZONE timezone])
Description
Returns a value that corresponds to the specified part from
a supplied timestamp_expression. This function supports an optional
timezone parameter. See
Time zone definitions for information
on how to specify a time zone.
Allowed part values are:
MICROSECONDMILLISECONDSECONDMINUTEHOURDAYOFWEEKDAYDAYOFYEARWEEK: Returns the week number of the date in the range [0, 53]. Weeks begin with Sunday, and dates prior to the first Sunday of the year are in week 0.WEEK(<WEEKDAY>): Returns the week number oftimestamp_expressionin the range [0, 53]. Weeks begin onWEEKDAY.datetimes prior to the firstWEEKDAYof the year are in week 0. Valid values forWEEKDAYareSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK: Returns the ISO 8601 week number of thedatetime_expression.ISOWEEKs begin on Monday. Return values are in the range [1, 53]. The firstISOWEEKof each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year.MONTHQUARTERYEARISOYEAR: Returns the ISO 8601 week-numbering year, which is the Gregorian calendar year containing the Thursday of the week to whichdate_expressionbelongs.DATEDATETIMETIME
Returned values truncate lower order time periods. For example, when extracting
seconds, EXTRACT truncates the millisecond and microsecond values.
Return Data Type
INT64, except when:
partisDATE, returns aDATEobject.partisDATETIME, returns aDATETIMEobject.partisTIME, returns aTIMEobject.
Examples
In the following example, EXTRACT returns a value corresponding to the DAY
time part.
WITH Input AS (SELECT TIMESTAMP("2008-12-25 05:30:00+00") AS timestamp_value)
SELECT
EXTRACT(DAY FROM timestamp_value AT TIME ZONE "UTC") AS the_day_utc,
EXTRACT(DAY FROM timestamp_value AT TIME ZONE "America/Los_Angeles") AS the_day_california
FROM Input
+-------------+--------------------+
| the_day_utc | the_day_california |
+-------------+--------------------+
| 25 | 24 |
+-------------+--------------------+
In the following example, EXTRACT returns values corresponding to different
time parts from a column of timestamps.
WITH Timestamps AS (
SELECT TIMESTAMP("2005-01-03 12:34:56+00") AS timestamp_value UNION ALL
SELECT TIMESTAMP("2007-12-31 12:00:00+00") UNION ALL
SELECT TIMESTAMP("2009-01-01 12:00:00+00") UNION ALL
SELECT TIMESTAMP("2009-12-31 12:00:00+00") UNION ALL
SELECT TIMESTAMP("2017-01-02 12:00:00+00") UNION ALL
SELECT TIMESTAMP("2017-05-26 12:00:00+00")
)
SELECT
timestamp_value,
EXTRACT(ISOYEAR FROM timestamp_value) AS isoyear,
EXTRACT(ISOWEEK FROM timestamp_value) AS isoweek,
EXTRACT(YEAR FROM timestamp_value) AS year,
EXTRACT(WEEK FROM timestamp_value) AS week
FROM Timestamps
ORDER BY timestamp_value;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+---------+---------+------+------+
| timestamp_value | isoyear | isoweek | year | week |
+-------------------------+---------+---------+------+------+
| 2005-01-03 12:34:56 UTC | 2005 | 1 | 2005 | 1 |
| 2007-12-31 12:00:00 UTC | 2008 | 1 | 2007 | 52 |
| 2009-01-01 12:00:00 UTC | 2009 | 1 | 2009 | 0 |
| 2009-12-31 12:00:00 UTC | 2009 | 53 | 2009 | 52 |
| 2017-01-02 12:00:00 UTC | 2017 | 1 | 2017 | 1 |
| 2017-05-26 12:00:00 UTC | 2017 | 21 | 2017 | 21 |
+-------------------------+---------+---------+------+------+
In the following example, timestamp_expression falls on a Monday. EXTRACT
calculates the first column using weeks that begin on Sunday, and it calculates
the second column using weeks that begin on Monday.
WITH table AS (SELECT TIMESTAMP("2017-11-05 00:00:00+00") AS timestamp_value)
SELECT
timestamp_value,
EXTRACT(WEEK(SUNDAY) FROM timestamp_value) AS week_sunday,
EXTRACT(WEEK(MONDAY) FROM timestamp_value) AS week_monday
FROM table;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+-------------+---------------+
| timestamp_value | week_sunday | week_monday |
+-------------------------+-------------+---------------+
| 2017-11-05 00:00:00 UTC | 45 | 44 |
+-------------------------+-------------+---------------+
STRING
STRING(timestamp_expression[, timezone])
Description
Converts a timestamp_expression to a STRING data type. Supports an optional
parameter to specify a time zone. See
Time zone definitions for information
on how to specify a time zone.
Return Data Type
STRING
Example
SELECT STRING(TIMESTAMP "2008-12-25 15:30:00+00", "UTC") AS string;
+-------------------------------+
| string |
+-------------------------------+
| 2008-12-25 15:30:00+00 |
+-------------------------------+
TIMESTAMP
TIMESTAMP(string_expression[, timezone])
TIMESTAMP(date_expression[, timezone])
TIMESTAMP(datetime_expression[, timezone])
Description
string_expression[, timezone]: Converts a STRING expression to a TIMESTAMP data type.string_expressionmust include a timestamp literal. Ifstring_expressionincludes a timezone in the timestamp literal, do not include an explicittimezoneargument.date_expression[, timezone]: Converts a DATE object to a TIMESTAMP data type.datetime_expression[, timezone]: Converts a DATETIME object to a TIMESTAMP data type.
This function supports an optional parameter to specify a time zone. If no time zone is specified, the default time zone, UTC, is used.
Return Data Type
TIMESTAMP
Examples
SELECT TIMESTAMP("2008-12-25 15:30:00+00") AS timestamp_str;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+
| timestamp_str |
+-------------------------+
| 2008-12-25 15:30:00 UTC |
+-------------------------+
SELECT TIMESTAMP("2008-12-25 15:30:00", "America/Los_Angeles") AS timestamp_str;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+
| timestamp_str |
+-------------------------+
| 2008-12-25 23:30:00 UTC |
+-------------------------+
SELECT TIMESTAMP("2008-12-25 15:30:00 UTC") AS timestamp_str;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+
| timestamp_str |
+-------------------------+
| 2008-12-25 15:30:00 UTC |
+-------------------------+
SELECT TIMESTAMP(DATETIME "2008-12-25 15:30:00") AS timestamp_datetime;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+
| timestamp_datetime |
+-------------------------+
| 2008-12-25 15:30:00 UTC |
+-------------------------+
SELECT TIMESTAMP(DATE "2008-12-25") AS timestamp_date;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+
| timestamp_date |
+-------------------------+
| 2008-12-25 00:00:00 UTC |
+-------------------------+
TIMESTAMP_ADD
TIMESTAMP_ADD(timestamp_expression, INTERVAL int64_expression date_part)
Description
Adds int64_expression units of date_part to the timestamp, independent of
any time zone.
TIMESTAMP_ADD supports the following values for date_part:
MICROSECONDMILLISECONDSECONDMINUTEHOUR. Equivalent to 60MINUTEs.DAY. Equivalent to 24HOURs.
Return Data Types
TIMESTAMP
Example
SELECT
TIMESTAMP("2008-12-25 15:30:00+00") AS original,
TIMESTAMP_ADD(TIMESTAMP "2008-12-25 15:30:00+00", INTERVAL 10 MINUTE) AS later;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+-------------------------+
| original | later |
+-------------------------+-------------------------+
| 2008-12-25 15:30:00 UTC | 2008-12-25 15:40:00 UTC |
+-------------------------+-------------------------+
TIMESTAMP_SUB
TIMESTAMP_SUB(timestamp_expression, INTERVAL int64_expression date_part)
Description
Subtracts int64_expression units of date_part from the timestamp,
independent of any time zone.
TIMESTAMP_SUB supports the following values for date_part:
MICROSECONDMILLISECONDSECONDMINUTEHOUR. Equivalent to 60MINUTEs.DAY. Equivalent to 24HOURs.
Return Data Type
TIMESTAMP
Example
SELECT
TIMESTAMP("2008-12-25 15:30:00+00") AS original,
TIMESTAMP_SUB(TIMESTAMP "2008-12-25 15:30:00+00", INTERVAL 10 MINUTE) AS earlier;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+-------------------------+
| original | earlier |
+-------------------------+-------------------------+
| 2008-12-25 15:30:00 UTC | 2008-12-25 15:20:00 UTC |
+-------------------------+-------------------------+
TIMESTAMP_DIFF
TIMESTAMP_DIFF(timestamp_expression_a, timestamp_expression_b, date_part)
Description
Returns the number of whole specified date_part intervals between two
TIMESTAMP objects (timestamp_expression_a - timestamp_expression_b). If the first TIMESTAMP is earlier than the second one,
the output is negative. Throws an error if the computation overflows the
result type, such as if the difference in
microseconds
between the two TIMESTAMP objects would overflow an INT64 value.
TIMESTAMP_DIFF supports the following values for date_part:
MICROSECONDMILLISECONDSECONDMINUTEHOUR. Equivalent to 60MINUTEs.DAY. Equivalent to 24HOURs.
Return Data Type
INT64
Example
SELECT
TIMESTAMP("2010-07-07 10:20:00+00") AS later_timestamp,
TIMESTAMP("2008-12-25 15:30:00+00") AS earlier_timestamp,
TIMESTAMP_DIFF(TIMESTAMP "2010-07-07 10:20:00+00", TIMESTAMP "2008-12-25 15:30:00+00", HOUR) AS hours;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+-------------------------+-------+
| later_timestamp | earlier_timestamp | hours |
+-------------------------+-------------------------+-------+
| 2010-07-07 10:20:00 UTC | 2008-12-25 15:30:00 UTC | 13410 |
+-------------------------+-------------------------+-------+
In the following example, the first timestamp occurs before the second timestamp, resulting in a negative output.
SELECT TIMESTAMP_DIFF(TIMESTAMP "2018-08-14", TIMESTAMP "2018-10-14", DAY);
+---------------+
| negative_diff |
+---------------+
| -61 |
+---------------+
In this example, the result is 0 because only the number of whole specified
HOUR intervals are included.
SELECT TIMESTAMP_DIFF("2001-02-01 01:00:00", "2001-02-01 00:00:01", HOUR)
+---------------+
| negative_diff |
+---------------+
| 0 |
+---------------+
TIMESTAMP_TRUNC
TIMESTAMP_TRUNC(timestamp_expression, date_part[, timezone])
Description
Truncates a timestamp to the granularity of date_part.
TIMESTAMP_TRUNC supports the following values for date_part:
MICROSECONDMILLISECONDSECONDMINUTEHOURDAYWEEKWEEK(<WEEKDAY>):Truncatestimestamp_expressionto the preceding week boundary, where weeks begin onWEEKDAY. Valid values forWEEKDAYareSUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY, andSATURDAY.ISOWEEK: Truncatestimestamp_expressionto the preceding ISO 8601 week boundary.ISOWEEKs begin on Monday. The firstISOWEEKof each ISO year contains the first Thursday of the corresponding Gregorian calendar year. Anydate_expressionearlier than this will truncate to the preceding Monday.MONTHQUARTERYEARISOYEAR: Truncatestimestamp_expressionto the preceding ISO 8601 week-numbering year boundary. The ISO year boundary is the Monday of the first week whose Thursday belongs to the corresponding Gregorian calendar year.
TIMESTAMP_TRUNC function supports an optional timezone parameter. This
parameter applies to the following date_parts:
MINUTEHOURDAYWEEKWEEK(<WEEKDAY>)ISOWEEKMONTHQUARTERYEARISOYEAR
Use this parameter if you want to use a time zone other than the default time zone, UTC, as part of the truncate operation.
When truncating a TIMESTAMP to MINUTE
orHOUR, TIMESTAMP_TRUNC determines the civil time of the
TIMESTAMP in the specified (or default) time zone
and subtracts the minutes and seconds (when truncating to HOUR) or the seconds
(when truncating to MINUTE) from that TIMESTAMP.
While this provides intuitive results in most cases, the result is
non-intuitive near daylight savings transitions that are not hour aligned.
Return Data Type
TIMESTAMP
Examples
SELECT
TIMESTAMP_TRUNC(TIMESTAMP "2008-12-25 15:30:00+00", DAY, "UTC") AS utc,
TIMESTAMP_TRUNC(TIMESTAMP "2008-12-25 15:30:00+00", DAY, "America/Los_Angeles") AS la;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+-----------------=-------+
| utc | la |
+-------------------------+-------------------------+
| 2008-12-25 00:00:00 UTC | 2008-12-25 08:00:00 UTC |
+-------------------------+-------------------------+
In the following example, timestamp_expression has a time zone offset of +12.
The first column shows the timestamp_expression in UTC time. The second
column shows the output of TIMESTAMP_TRUNC using weeks that start on Monday.
Because the timestamp_expression falls on a Sunday in UTC, TIMESTAMP_TRUNC
truncates it to the preceding Monday. The third column shows the same function
with the optional Time zone definition
argument 'Pacific/Auckland'. Here the function truncates the
timestamp_expression using New Zealand Daylight Time, where it falls on a
Monday.
SELECT
timestamp_value AS timestamp_value,
TIMESTAMP_TRUNC(timestamp_value, WEEK(MONDAY), "UTC") AS utc_truncated,
TIMESTAMP_TRUNC(timestamp_value, WEEK(MONDAY), "Pacific/Auckland") AS nzdt_truncated
FROM (SELECT TIMESTAMP("2017-11-06 00:00:00+12") AS timestamp_value);
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+-------------------------+-------------------------+
| timestamp_value | utc_truncated | nzdt_truncated |
+-------------------------+-------------------------+-------------------------+
| 2017-11-05 12:00:00 UTC | 2017-10-30 00:00:00 UTC | 2017-11-05 11:00:00 UTC |
+-------------------------+-------------------------+-------------------------+
In the following example, the original timestamp_expression is in the
Gregorian calendar year 2015. However, TIMESTAMP_TRUNC with the ISOYEAR date
part truncates the timestamp_expression to the beginning of the ISO year, not
the Gregorian calendar year. The first Thursday of the 2015 calendar year was
2015-01-01, so the ISO year 2015 begins on the preceding Monday, 2014-12-29.
Therefore the ISO year boundary preceding the timestamp_expression
2015-06-15 00:00:00+00 is 2014-12-29.
SELECT
TIMESTAMP_TRUNC("2015-06-15 00:00:00+00", ISOYEAR) AS isoyear_boundary,
EXTRACT(ISOYEAR FROM TIMESTAMP "2015-06-15 00:00:00+00") AS isoyear_number;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+----------------+
| isoyear_boundary | isoyear_number |
+-------------------------+----------------+
| 2014-12-29 00:00:00 UTC | 2015 |
+-------------------------+----------------+
FORMAT_TIMESTAMP
FORMAT_TIMESTAMP(format_string, timestamp[, timezone])
Description
Formats a timestamp according to the specified format_string.
See Supported Format Elements For TIMESTAMP for a list of format elements that this function supports.
Return Data Type
STRING
Example
SELECT FORMAT_TIMESTAMP("%c", TIMESTAMP "2008-12-25 15:30:00+00", "UTC") AS formatted;
+--------------------------+
| formatted |
+--------------------------+
| Thu Dec 25 15:30:00 2008 |
+--------------------------+
SELECT FORMAT_TIMESTAMP("%b-%d-%Y", TIMESTAMP "2008-12-25 15:30:00+00") AS formatted;
+-------------+
| formatted |
+-------------+
| Dec-25-2008 |
+-------------+
SELECT FORMAT_TIMESTAMP("%b %Y", TIMESTAMP "2008-12-25 15:30:00+00")
AS formatted;
+-------------+
| formatted |
+-------------+
| Dec 2008 |
+-------------+
PARSE_TIMESTAMP
PARSE_TIMESTAMP(format_string, timestamp_string[, timezone])
Description
Converts a string representation of a timestamp to a
TIMESTAMP object.
format_string contains the format elements
that define how timestamp_string is formatted. Each element in
timestamp_string must have a corresponding element in format_string. The
location of each element in format_string must match the location of
each element in timestamp_string.
-- This works because elements on both sides match.
SELECT PARSE_TIMESTAMP("%a %b %e %I:%M:%S %Y", "Thu Dec 25 07:30:00 2008")
-- This doesn't work because the year element is in different locations.
SELECT PARSE_TIMESTAMP("%a %b %e %Y %I:%M:%S", "Thu Dec 25 07:30:00 2008")
-- This doesn't work because one of the year elements is missing.
SELECT PARSE_TIMESTAMP("%a %b %e %I:%M:%S", "Thu Dec 25 07:30:00 2008")
-- This works because %c can find all matching elements in timestamp_string.
SELECT PARSE_TIMESTAMP("%c", "Thu Dec 25 07:30:00 2008")
The format string fully
supports most format elements, except for
%Q, %a, %A, %g,
%G, %j, %P, %u, %U, %V, %w, and %W.
When using PARSE_TIMESTAMP, keep the following in mind:
- Unspecified fields. Any unspecified field is initialized from
1970-01-01 00:00:00.0. This initialization value uses the time zone specified by the function's time zone argument, if present. If not, the initialization value uses the default time zone, UTC. For instance, if the year is unspecified then it defaults to1970, and so on. - Case insensitive names. Names, such as
Monday,February, and so on, are case insensitive. - Whitespace. One or more consecutive white spaces in the format string matches zero or more consecutive white spaces in the timestamp string. In addition, leading and trailing white spaces in the timestamp string are always allowed, even if they are not in the format string.
- Format precedence. When two (or more) format elements have overlapping
information (for example both
%Fand%Yaffect the year), the last one generally overrides any earlier ones, with some exceptions (see the descriptions of%s,%C, and%y).
Return Data Type
TIMESTAMP
Example
SELECT PARSE_TIMESTAMP("%c", "Thu Dec 25 07:30:00 2008") AS parsed;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+
| parsed |
+-------------------------+
| 2008-12-25 07:30:00 UTC |
+-------------------------+
TIMESTAMP_SECONDS
TIMESTAMP_SECONDS(int64_expression)
Description
Interprets int64_expression as the number of seconds since 1970-01-01 00:00:00
UTC.
Return Data Type
TIMESTAMP
Example
SELECT TIMESTAMP_SECONDS(1230219000) AS timestamp_value;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+
| timestamp_value |
+-------------------------+
| 2008-12-25 15:30:00 UTC |
+-------------------------+
TIMESTAMP_MILLIS
TIMESTAMP_MILLIS(int64_expression)
Description
Interprets int64_expression as the number of milliseconds since 1970-01-01
00:00:00 UTC.
Return Data Type
TIMESTAMP
Example
SELECT TIMESTAMP_MILLIS(1230219000000) AS timestamp_value;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+
| timestamp_value |
+-------------------------+
| 2008-12-25 15:30:00 UTC |
+-------------------------+
TIMESTAMP_MICROS
TIMESTAMP_MICROS(int64_expression)
Description
Interprets int64_expression as the number of microseconds since 1970-01-01
00:00:00 UTC.
Return Data Type
TIMESTAMP
Example
SELECT TIMESTAMP_MICROS(1230219000000000) AS timestamp_value;
-- Results may differ, depending upon the environment and time zone where this query was executed.
+-------------------------+
| timestamp_value |
+-------------------------+
| 2008-12-25 15:30:00 UTC |
+-------------------------+
UNIX_SECONDS
UNIX_SECONDS(timestamp_expression)
Description
Returns the number of seconds since 1970-01-01 00:00:00 UTC. Truncates higher levels of precision.
Return Data Type
INT64
Example
SELECT UNIX_SECONDS(TIMESTAMP "2008-12-25 15:30:00+00") AS seconds;
+------------+
| seconds |
+------------+
| 1230219000 |
+------------+
UNIX_MILLIS
UNIX_MILLIS(timestamp_expression)
Description
Returns the number of milliseconds since 1970-01-01 00:00:00 UTC. Truncates higher levels of precision.
Return Data Type
INT64
Example
SELECT UNIX_MILLIS(TIMESTAMP "2008-12-25 15:30:00+00") AS millis;
+---------------+
| millis |
+---------------+
| 1230219000000 |
+---------------+
UNIX_MICROS
UNIX_MICROS(timestamp_expression)
Description
Returns the number of microseconds since 1970-01-01 00:00:00 UTC. Truncates higher levels of precision.
Return Data Type
INT64
Example
SELECT UNIX_MICROS(TIMESTAMP "2008-12-25 15:30:00+00") AS micros;
+------------------+
| micros |
+------------------+
| 1230219000000000 |
+------------------+
Supported format elements for TIMESTAMP
Unless otherwise noted, TIMESTAMP functions that use format strings support the following elements:
| Format element | Description | Example |
| %A | The full weekday name. | Wednesday |
| %a | The abbreviated weekday name. | Wed |
| %B | The full month name. | January |
| %b or %h | The abbreviated month name. | Jan |
| %C | The century (a year divided by 100 and truncated to an integer) as a decimal number (00-99). | 20 |
| %c | The date and time representation in the format %a %b %e %T %Y. | Wed Jan 20 16:47:00 2021 |
| %D | The date in the format %m/%d/%y. | 01/20/21 |
| %d | The day of the month as a decimal number (01-31). | 20 |
| %e | The day of month as a decimal number (1-31); single digits are preceded by a space. | 20 |
| %F | The date in the format %Y-%m-%d. | 2021-01-20 |
| %G | The ISO 8601 year with century as a decimal number. Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %G and %Y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge. | 2021 |
| %g | The ISO 8601 year without century as a decimal number (00-99). Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %g and %y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge. | 21 |
| %H | The hour (24-hour clock) as a decimal number (00-23). | 16 |
| %I | The hour (12-hour clock) as a decimal number (01-12). | 04 |
| %j | The day of the year as a decimal number (001-366). | 020 |
| %k | The hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a space. | 16 |
| %l | The hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a space. | 11 |
| %M | The minute as a decimal number (00-59). | 47 |
| %m | The month as a decimal number (01-12). | 01 |
| %n | A newline character. | |
| %P | Either am or pm. | am |
| %p | Either AM or PM. | AM |
| %Q | The quarter as a decimal number (1-4). | 1 |
| %R | The time in the format %H:%M. | 16:47 |
| %r | The 12-hour clock time using AM/PM notation. | 04:47:00 PM |
| %S | The second as a decimal number (00-60). | 00 |
| %s | The number of seconds since 1970-01-01 00:00:00 UTC. Always overrides all other format elements, independent of where %s appears in the string. If multiple %s elements appear, then the last one takes precedence. | 1611179220 |
| %T | The time in the format %H:%M:%S. | 16:47:00 |
| %t | A tab character. | |
| %U | The week number of the year (Sunday as the first day of the week) as a decimal number (00-53). | 03 |
| %u | The weekday (Monday as the first day of the week) as a decimal number (1-7). | 3 |
| %V | The ISO 8601 week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is week 53 of the previous year, and the next week is week 1. | 03 |
| %W | The week number of the year (Monday as the first day of the week) as a decimal number (00-53). | 03 |
| %w | The weekday (Sunday as the first day of the week) as a decimal number (0-6). | 3 |
| %X | The time representation in HH:MM:SS format. | 16:47:00 |
| %x | The date representation in MM/DD/YY format. | 01/20/21 |
| %Y | The year with century as a decimal number. | 2021 |
| %y | The year without century as a decimal number (00-99), with an optional leading zero. Can be mixed with %C. If %C is not specified, years 00-68 are 2000s, while years 69-99 are 1900s. | 21 |
| %Z | The time zone name. | UTC-5 |
| %z | The offset from the Prime Meridian in the format +HHMM or -HHMM as appropriate, with positive values representing locations east of Greenwich. | -0500 |
| %% | A single % character. | % |
| %Ez | RFC 3339-compatible numeric time zone (+HH:MM or -HH:MM). | -05:00 |
| %E#S | Seconds with # digits of fractional precision. | 00.000 |
| %E*S | Seconds with full fractional precision (a literal '*'). | 00 |
| %E4Y | Four-character years (0001 ... 9999). Note that %Y produces as many characters as it takes to fully render the year. | 2021 |
Time zone definitions
Certain date and timestamp functions allow you to override the default time zone
and specify a different one. You can specify a time zone by either supplying
the time zone name (for example, America/Los_Angeles)
or time zone offset from UTC (for example, -08).
If you choose to use a time zone offset, use this format:
(+|-)H[H][:M[M]]
The following timestamps are equivalent because the time zone offset
for America/Los_Angeles is -08 for the specified date and time.
SELECT UNIX_MILLIS(TIMESTAMP "2008-12-25 15:30:00 America/Los_Angeles") as millis;
SELECT UNIX_MILLIS(TIMESTAMP "2008-12-25 15:30:00-08:00") as millis;
Geography functions
The geography functions operate on or generate BigQuery
GEOGRAPHY values. The signature of any geography
function starts with ST_. BigQuery supports the following functions
that can be used to analyze geographical data, determine spatial relationships
between geographical features, and construct or manipulate
GEOGRAPHYs.
All BigQuery geography functions return NULL if any input argument
is NULL.
Categories
The geography functions are grouped into the following categories based on their behavior:
| Category | Functions | Description |
| Constructors |
ST_GEOGPOINTST_MAKELINEST_MAKEPOLYGONST_MAKEPOLYGONORIENTED
|
Functions that build new geography values from coordinates or existing geographies. |
| Parsers |
ST_GEOGFROMST_GEOGFROMGEOJSONST_GEOGFROMTEXTST_GEOGFROMWKBST_GEOGPOINTFROMGEOHASH |
Functions that create geographies from an external format such as WKT and GeoJSON. |
| Formatters |
ST_ASBINARYST_ASGEOJSONST_ASTEXTST_GEOHASH
|
Functions that export geographies to an external format such as WKT. |
| Transformations |
ST_BOUNDARYST_CENTROIDST_CENTROID_AGG (Aggregate)ST_CLOSESTPOINTST_CONVEXHULLST_DIFFERENCEST_DUMPST_EXTERIORRINGST_INTERSECTIONST_SIMPLIFYST_SNAPTOGRIDST_UNIONST_UNION_AGG (Aggregate) |
Functions that generate a new geography based on input. |
| Accessors |
ST_DIMENSIONST_DUMPST_ENDPOINTST_ISCOLLECTIONST_ISEMPTYST_NPOINTSST_NUMPOINTSST_POINTNST_STARTPOINTST_XST_Y |
Functions that provide access to properties of a geography without side-effects. |
| Predicates |
ST_CONTAINSST_COVEREDBYST_COVERSST_DISJOINTST_DWITHINST_EQUALSST_INTERSECTSST_INTERSECTSBOXST_TOUCHESST_WITHIN |
Functions that return TRUE or
FALSE for some spatial
relationship between two
geographies or some property of
a geography. These functions
are commonly used in filter
clauses.
|
| Measures |
ST_AREAST_DISTANCEST_LENGTHST_MAXDISTANCEST_PERIMETER |
Functions that compute measurements of one or more geographies. |
| Clustering |
ST_CLUSTERDBSCAN
|
Functions that perform clustering on geographies. |
ST_AREA
ST_AREA(geography_expression[, use_spheroid])
Description
Returns the area in square meters covered by the polygons in the input
GEOGRAPHY.
If geography_expression is a point or a line, returns zero. If
geography_expression is a collection, returns the area of the polygons in the
collection; if the collection does not contain polygons, returns zero.
The optional use_spheroid parameter determines how this function measures
distance. If use_spheroid is FALSE, the function measures distance on the
surface of a perfect sphere.
The use_spheroid parameter currently only supports
the value FALSE. The default value of use_spheroid is FALSE.
Return type
FLOAT64
ST_ASBINARY
ST_ASBINARY(geography_expression)
Description
Returns the WKB representation of an input
GEOGRAPHY.
See ST_GEOGFROMWKB to construct a
GEOGRAPHY from WKB.
Return type
BYTES
ST_ASGEOJSON
ST_ASGEOJSON(geography_expression)
Description
Returns the RFC 7946 compliant GeoJSON
representation of the input GEOGRAPHY.
A BigQuery GEOGRAPHY has spherical
geodesic edges, whereas a GeoJSON Geometry object explicitly has planar edges.
To convert between these two types of edges, BigQuery adds additional
points to the line where necessary so that the resulting sequence of edges
remains within 10 meters of the original edge.
See ST_GEOGFROMGEOJSON to construct a
GEOGRAPHY from GeoJSON.
Return type
STRING
ST_ASTEXT
ST_ASTEXT(geography_expression)
Description
Returns the WKT representation of an input
GEOGRAPHY.
See ST_GEOGFROMTEXT to construct a
GEOGRAPHY from WKT.
Return type
STRING
ST_BOUNDARY
ST_BOUNDARY(geography_expression)
Description
Returns a single GEOGRAPHY that contains the union
of the boundaries of each component in the given input
GEOGRAPHY.
The boundary of each component of a GEOGRAPHY is
defined as follows:
- The boundary of a point is empty.
- The boundary of a linestring consists of the endpoints of the linestring.
- The boundary of a polygon consists of the linestrings that form the polygon shell and each of the polygon's holes.
Return type
GEOGRAPHY
ST_CENTROID
ST_CENTROID(geography_expression)
Description
Returns the centroid of the input GEOGRAPHY as a
single point GEOGRAPHY.
The centroid of a GEOGRAPHY is the weighted
average of the centroids of the highest-dimensional components in the
GEOGRAPHY. The centroid for components in each
dimension is defined as follows:
- The centroid of points is the arithmetic mean of the input coordinates.
- The centroid of linestrings is the centroid of all the edges weighted by length. The centroid of each edge is the geodesic midpoint of the edge.
- The centroid of a polygon is its center of mass.
If the input GEOGRAPHY is empty, an empty
GEOGRAPHY is returned.
Constraints
In the unlikely event that the centroid of a
GEOGRAPHY cannot be defined by a single point on
the surface of the Earth, a deterministic but otherwise arbitrary point is
returned. This can only happen if the centroid is exactly at the center of the
Earth, such as the centroid for a pair of antipodal points, and the likelihood
of this happening is vanishingly small.
Return type
GEOGRAPHY
ST_CENTROID_AGG
ST_CENTROID_AGG(geography)
Description
Computes the centroid of the set of input GEOGRAPHYs
as a single point GEOGRAPHY.
The centroid over the set of input GEOGRAPHYs is
the weighted average of the centroid of each individual
GEOGRAPHY. Only the
GEOGRAPHYs with the highest dimension present in the
input contribute to the centroid of the entire set. For example, if
the input contains both GEOGRAPHYs with lines and
GEOGRAPHYs with only points, ST_CENTROID_AGG
returns the weighted average of the
GEOGRAPHYs with lines, since those have maximal
dimension. In this example, ST_CENTROID_AGG ignores
GEOGRAPHYs with only points when calculating the
aggregate centroid.
ST_CENTROID_AGG ignores NULL input GEOGRAPHY
values.
See ST_CENTROID for the non-aggregate version of
ST_CENTROID_AGG and the definition of centroid for an individual
GEOGRAPHY value.
Return type
GEOGRAPHY
Example
The following queries compute the aggregate centroid over a set of
GEOGRAPHY values. The input to the first query
contains only points, and therefore each value contribute to the aggregate
centroid. Also notice that ST_CENTROID_AGG is not equivalent to calling
ST_CENTROID on the result of ST_UNION_AGG; duplicates are removed by the
union, unlike ST_CENTROID_AGG. The input to the second query has mixed
dimensions, and only values with the highest dimension in the set, the lines,
affect the aggregate centroid.
SELECT ST_CENTROID_AGG(points) AS st_centroid_agg,
ST_CENTROID(ST_UNION_AGG(points)) AS centroid_of_union
FROM UNNEST([ST_GEOGPOINT(1, 5),
ST_GEOGPOINT(1, 2),
ST_GEOGPOINT(1, -1),
ST_GEOGPOINT(1, -1)]) points;
+---------------------------+-------------------+
| st_centroid_agg | centroid_of_union |
+---------------------------+-------------------+
| POINT(1 1.24961422620969) | POINT(1 2) |
+---------------------------+-------------------+
SELECT ST_CENTROID_AGG(points) AS st_centroid_agg
FROM UNNEST([ST_GEOGPOINT(50, 26),
ST_GEOGPOINT(34, 33.3),
ST_GEOGFROMTEXT('LINESTRING(0 -1, 0 1)')
ST_GEOGFROMTEXT('LINESTRING(0 1, 0 3)')]) points;
+-----------------+
| st_centroid_agg |
+-----------------+
| POINT(0 1) |
+-----------------+
ST_CLOSESTPOINT
ST_CLOSESTPOINT(geography_1, geography_2[, use_spheroid])
Description
Returns a GEOGRAPHY containing a point on
geography_1 with the smallest possible distance to geography_2. This implies
that the distance between the point returned by ST_CLOSESTPOINT and
geography_2 is less than or equal to the distance between any other point on
geography_1 and geography_2.
If either of the input GEOGRAPHYs is empty,
ST_CLOSESTPOINT returns NULL.
The optional use_spheroid parameter determines how this function measures
distance. If use_spheroid is FALSE, the function measures distance on the
surface of a perfect sphere.
The use_spheroid parameter currently only supports
the value FALSE. The default value of use_spheroid is FALSE.
Return type
GEOGRAPHY
ST_CLUSTERDBSCAN
ST_CLUSTERDBSCAN(geography_column, epsilon, minimum_geographies) OVER (...)
Performs DBSCAN clustering on a column of geographies. Returns a 0-based cluster number.
Input parameters
geography_column: A column ofGEOGRAPHYs that is clustered.epsilon: The epsilon that specifies the radius, measured in meters, around a core value. Non-negativeFLOAT64value.minimum_geographies: Specifies the minimum number of geographies in a single cluster. Only dense input forms a cluster, otherwise it is classified as noise. Non-negativeINT64value.OVER: Specifies a window. See Analytic Functions. Usage of theOVERclause withST_CLUSTERDBSCANis compatible with the following:PARTITION BY: Optional.ORDER BY: Optional.window_frame_clause: Disallowed.
Geography types and the DBSCAN algorithm
The DBSCAN algorithm identifies high-density clusters of data and marks outliers
in low-density areas of noise. Geographies passed in through geography_column
are classified in one of three ways by the DBSCAN algorithm:
- Core value: A geography is a core value if it is within
epsilondistance ofminimum_geographiesgeographies, including itself. The core value starts a new cluster, or is added to the same cluster as a core value withinepsilondistance. Core values are grouped in a cluster together with all other core and border values that are withinepsilondistance. - Border value: A geography is a border value if it is within epsilon distance
of a core value. It is added to the same cluster as a core value within
epsilondistance. A border value may be withinepsilondistance of more than one cluster. In this case, it may be arbitrarily assigned to either cluster and the function will produce the same result in subsequent calls. - Noise: A geography is noise if it is neither a core nor a border value.
Noise values are assigned to a
NULLcluster. An emptyGEOGRAPHYis always classified as noise.
Constraints
- The argument
minimum_geographiesis a non-negativeINT64andepsilonis a non-negativeFLOAT64. - An empty geography cannot join any cluster.
- Multiple clustering assignments could be possible for a border value. If a
geography is a border value,
ST_CLUSTERDBSCANwill assign it to an arbitrary valid cluster.
Return type
INT64 for each geography in the geography column.
Examples
This example performs DBSCAN clustering with a radius of 100,000 meters with a
minimum_geographies argument of 1. The geographies being analyzed are a
mixture of points, lines, and polygons.
WITH Geos as
(SELECT 1 as row_id, st_geogfromtext('point empty') as geo UNION ALL
SELECT 2, st_geogfromtext('multipoint(1 1, 2 2, 4 4, 5 2)') UNION ALL
SELECT 3, st_geogfromtext('point(14 15)') UNION ALL
SELECT 4, st_geogfromtext('linestring(40 1, 42 34, 44 39)') UNION ALL
SELECT 5, st_geogfromtext('polygon((40 2, 40 1, 41 2, 40 2))'))
SELECT row_id, geo, ST_CLUSTERDBSCAN(geo, 1e5, 1) OVER () AS cluster_num FROM
Geos ORDER BY row_id
+--------+-----------------------------------+-------------+
| row_id | geo | cluster_num |
+--------+-----------------------------------+-------------+
| 1 | GEOMETRYCOLLECTION EMPTY | NULL |
| 2 | MULTIPOINT(1 1, 2 2, 5 2, 4 4) | 0 |
| 3 | POINT(14 15) | 1 |
| 4 | LINESTRING(40 1, 42 34, 44 39) | 2 |
| 5 | POLYGON((40 2, 40 1, 41 2, 40 2)) | 2 |
+--------+-----------------------------------+-------------+
ST_CONTAINS
ST_CONTAINS(geography_1, geography_2)
Description
Returns TRUE if no point of geography_2 is outside geography_1, and
the interiors intersect; returns FALSE otherwise.
NOTE: A GEOGRAPHY does not contain its own
boundary. Compare with ST_COVERS.
Return type
BOOL
Example
The following query tests whether the polygon POLYGON((1 1, 20 1, 10 20, 1 1))
contains each of the three points (0, 0), (1, 1), and (10, 10), which lie
on the exterior, the boundary, and the interior of the polygon respectively.
SELECT
ST_GEOGPOINT(i, i) AS p,
ST_CONTAINS(ST_GEOGFROMTEXT('POLYGON((1 1, 20 1, 10 20, 1 1))'),
ST_GEOGPOINT(i, i)) AS `contains`
FROM UNNEST([0, 1, 10]) AS i;
+--------------+----------+
| p | contains |
+--------------+----------+
| POINT(0 0) | false |
| POINT(1 1) | false |
| POINT(10 10) | true |
+--------------+----------+
ST_CONVEXHULL
ST_CONVEXHULL(geography_expression)
Description
Returns the convex hull for the input GEOGRAPHY.
The convex hull is the smallest convex GEOGRAPHY
that covers the input. A GEOGRAPHY is convex if
for every pair of points in the GEOGRAPHY,
the geodesic edge connecting the points are also contained in the same
GEOGRAPHY.
In most cases, the convex hull consists of a single polygon. Notable edge cases include the following:
- The convex hull of a single point is also a point.
- The convex hull of two or more collinear points is a linestring as long as that linestring is convex.
- If the input
GEOGRAPHYspans more than a hemisphere, the convex hull is the full globe. This includes any input that contains a pair of antipodal points. ST_CONVEXHULLreturnsNULLif the input is eitherNULLor the emptyGEOGRAPHY.
Return type
GEOGRAPHY
Examples
The convex hull returned by ST_CONVEXHULL can be a point, linestring, or a
polygon, depending on the input.
WITH Geographies AS
(SELECT ST_GEOGFROMTEXT('POINT(1 1)') AS g UNION ALL
SELECT ST_GEOGFROMTEXT('LINESTRING(1 1, 2 2)') AS g UNION ALL
SELECT ST_GEOGFROMTEXT('MULTIPOINT(2 11, 4 12, 0 15, 1 9, 1 12)') AS g)
SELECT
g AS input_geography,
ST_CONVEXHULL(g) AS convex_hull
FROM Geographies;
+-----------------------------------------+--------------------------------------------------------+
| input_geography | convex_hull |
+-----------------------------------------+--------------------------------------------------------+
| POINT(1 1) | POINT(0.999999999999943 1) |
| LINESTRING(1 1, 2 2) | LINESTRING(2 2, 1.49988573656168 1.5000570914792, 1 1) |
| MULTIPOINT(1 9, 4 12, 2 11, 1 12, 0 15) | POLYGON((1 9, 4 12, 0 15, 1 9)) |
+-----------------------------------------+--------------------------------------------------------+
ST_COVEREDBY
ST_COVEREDBY(geography_1, geography_2)
Description
Returns FALSE if geography_1 or geography_2 is empty. Returns TRUE if no
points of geography_1 lie in the exterior of geography_2.
Given two GEOGRAPHYs a and b,
ST_COVEREDBY(a, b) returns the same result as
ST_COVERS(b, a). Note the opposite order of arguments.
Return type
BOOL
ST_COVERS
ST_COVERS(geography_1, geography_2)
Description
Returns FALSE if geography_1 or geography_2 is empty.
Returns TRUE if no points of geography_2 lie in the exterior of geography_1.
Return type
BOOL
Example
The following query tests whether the polygon POLYGON((1 1, 20 1, 10 20, 1 1))
covers each of the three points (0, 0), (1, 1), and (10, 10), which lie
on the exterior, the boundary, and the interior of the polygon respectively.
SELECT
ST_GEOGPOINT(i, i) AS p,
ST_COVERS(ST_GEOGFROMTEXT('POLYGON((1 1, 20 1, 10 20, 1 1))'),
ST_GEOGPOINT(i, i)) AS `covers`
FROM UNNEST([0, 1, 10]) AS i;
+--------------+--------+
| p | covers |
+--------------+--------+
| POINT(0 0) | false |
| POINT(1 1) | true |
| POINT(10 10) | true |
+--------------+--------+
ST_DIFFERENCE
ST_DIFFERENCE(geography_1, geography_2)
Description
Returns a GEOGRAPHY that represents the point set
difference of geography_1 and geography_2. Therefore, the result consists of
the part of geography_1 that does not intersect with geography_2.
If geometry_1 is completely contained in geometry_2, then ST_DIFFERENCE
returns an empty GEOGRAPHY.
Constraints
The underlying geometric objects that a BigQuery
GEOGRAPHY represents correspond to a closed point
set. Therefore, ST_DIFFERENCE is the closure of the point set difference of
geography_1 and geography_2. This implies that if geography_1 and
geography_2 intersect, then a portion of the boundary of geography_2 could
be in the difference.
Return type
GEOGRAPHY
Example
The following query illustrates the diffence between geog1, a larger polygon
POLYGON((0 0, 10 0, 10 10, 0 0)) and geog1, a smaller polygon
POLYGON((4 2, 6 2, 8 6, 4 2)) that intersects with geog1. The result is
geog1 with a hole where geog2 intersects with it.
SELECT
ST_DIFFERENCE(
ST_GEOGFROMTEXT("POLYGON((0 0, 10 0, 10 10, 0 0))"),
ST_GEOGFROMTEXT("POLYGON((4 2, 6 2, 8 6, 4 2))")
);
+--------------------------------------------------------+
| difference_of_geog1_and_geog2 |
+--------------------------------------------------------+
| POLYGON((0 0, 10 0, 10 10, 0 0), (8 6, 6 2, 4 2, 8 6)) |
+--------------------------------------------------------+
ST_DIMENSION
ST_DIMENSION(geography_expression)
Description
Returns the dimension of the highest-dimensional element in the input
GEOGRAPHY.
The dimension of each possible element is as follows:
- The dimension of a point is
0. - The dimension of a linestring is
1. - The dimension of a polygon is
2.
If the input GEOGRAPHY is empty, ST_DIMENSION
returns -1.
Return type
INT64
ST_DISJOINT
ST_DISJOINT(geography_1, geography_2)
Description
Returns TRUE if the intersection of geography_1 and geography_2 is empty,
that is, no point in geography_1 also appears in geography_2.
ST_DISJOINT is the logical negation of ST_INTERSECTS.
Return type
BOOL
ST_DISTANCE
ST_DISTANCE(geography_1, geography_2[, use_spheroid])
Description
Returns the shortest distance in meters between two non-empty
GEOGRAPHYs.
If either of the input GEOGRAPHYs is empty,
ST_DISTANCE returns NULL.
The optional use_spheroid parameter determines how this function measures
distance. If use_spheroid is FALSE, the function measures distance on the
surface of a perfect sphere.
The use_spheroid parameter currently only supports
the value FALSE. The default value of use_spheroid is FALSE.
Return type
FLOAT64
ST_DUMP
ST_DUMP(geography[, dimension])
Description
Returns an ARRAY of simple
GEOGRAPHYs where each element is a component of
the input GEOGRAPHY. A simple
GEOGRAPHY consists of a single point, linestring,
or polygon. If the input GEOGRAPHY is simple, the
result is a single element. When the input
GEOGRAPHY is a collection, ST_DUMP returns an
ARRAY with one simple
GEOGRAPHY for each component in the collection.
If dimension is provided, the function only returns
GEOGRAPHYs of the corresponding dimension. A
dimension of -1 is equivalent to omitting dimension.
Return Type
ARRAY
Examples
The following example shows how ST_DUMP returns the simple geographies within
a complex geography.
WITH example AS (
SELECT ST_GeogFromText('POINT(0 0)') AS geography
UNION ALL
SELECT ST_GeogFromText('MULTIPOINT(0 0, 1 1)') AS geography
UNION ALL
SELECT ST_GeogFromText('GEOMETRYCOLLECTION(POINT(0 0), LINESTRING(1 2, 2 1))'))
SELECT
geography AS original_geography,
ST_DUMP(geography) AS dumped_geographies
FROM example
+-------------------------------------+-------------------------------+
| original_geographies | dumped_geographies |
+-------------------------------------+-------------------------------+
| POINT(0 0) | [POINT(0 0)] |
+-------------------------------------+-------------------------------+
| MULTIPOINT(0 0, 1 1) | [POINT(0 0), POINT(1 1)] |
+-------------------------------------+-------------------------------+
| GEOMETRYCOLLECTION(POINT(0 0), | [POINT(0 0), |
| LINESTRING(1 2, 2 1)) | LINESTRING(1 2, 2 1)] |
+-------------------------------------+-------------------------------+
The following example shows how ST_DUMP with the dimension argument only
returns simple geographies of the given dimension.
WITH example AS (
SELECT ST_GeogFromText('GEOMETRYCOLLECTION(POINT(0 0), LINESTRING(1 2, 2 1))') AS geography)
SELECT
geography AS original_geography,
ST_DUMP(geography, 1) AS dumped_geographies
FROM example
+-------------------------------------+------------------------------+
| original_geographies | dumped_geographies |
+-------------------------------------+------------------------------+
| GEOMETRYCOLLECTION(POINT(0 0), | [LINESTRING(1 2, 2 1)] |
| LINESTRING(1 2, 2 1)) | |
+-------------------------------------+------------------------------+
ST_DWITHIN
ST_DWITHIN(geography_1, geography_2, distance[, use_spheroid])
Description
Returns TRUE if the distance between at least one point in geography_1 and
one point in geography_2 is less than or equal to the distance given by the
distance argument; otherwise, returns FALSE. If either input
GEOGRAPHY is empty, ST_DWithin returns FALSE. The
given distance is in meters on the surface of the Earth.
The optional use_spheroid parameter determines how this function measures
distance. If use_spheroid is FALSE, the function measures distance on the
surface of a perfect sphere.
The use_spheroid parameter currently only supports
the value FALSE. The default value of use_spheroid is FALSE.
Return type
BOOL
ST_ENDPOINT
ST_ENDPOINT(linestring_geography)
Description
Returns the last point of a linestring geography as a point geography. Returns
an error if the input is not a linestring or if the input is empty. Use the
SAFE prefix to obtain NULL for invalid input instead of an error.
Return Type
Point GEOGRAPHY
Example
SELECT ST_EndPoint(ST_GeogFromText('linestring(1 1, 2 1, 3 2, 3 3)')) last
+--------------+
| last |
+--------------+
| POINT(3 3) |
+--------------+
ST_EQUALS
ST_EQUALS(geography_1, geography_2)
Description
Returns TRUE if geography_1 and geography_2 represent the same
GEOGRAPHY value. More precisely, this means that
one of the following conditions holds:
+ ST_COVERS(geography_1, geography_2) = TRUE and ST_COVERS(geography_2,
geography_1) = TRUE
+ Both geography_1 and geography_2 are empty.
Therefore, two GEOGRAPHYs may be equal even if the
ordering of points or vertices differ, as long as they still represent the same
geometric structure.
Constraints
ST_EQUALS is not guaranteed to be a transitive function.
Return type
BOOL
ST_EXTERIORRING
ST_EXTERIORRING(polygon_geography)
Description
Returns a linestring geography that corresponds to the outermost ring of a polygon geography.
- If the input geography is a polygon, gets the outermost ring of the polygon geography and returns the corresponding linestring.
- If the input is the full
GEOGRAPHY, returns an empty geography. - Returns an error if the input is not a single polygon.
Use the SAFE prefix to return NULL for invalid input instead of an error.
Return type
- Linestring
GEOGRAPHY - Empty
GEOGRAPHY
Examples
WITH geo as
(SELECT ST_GEOGFROMTEXT('POLYGON((0 0, 1 4, 2 2, 0 0))') AS g UNION ALL
SELECT ST_GEOGFROMTEXT('''POLYGON((1 1, 1 10, 5 10, 5 1, 1 1),
(2 2, 3 4, 2 4, 2 2))''') as g)
SELECT ST_EXTERIORRING(g) AS ring FROM geo;
+---------------------------------------+
| ring |
+---------------------------------------+
| LINESTRING(2 2, 1 4, 0 0, 2 2) |
| LINESTRING(5 1, 5 10, 1 10, 1 1, 5 1) |
+---------------------------------------+
ST_GEOGFROM
ST_GEOGFROM(expression)
Description
Converts an expression for a STRING or BYTES value into a
GEOGRAPHY value.
If expression represents a STRING value, it must be a valid
GEOGRAPHY representation in one of the following formats:
- WKT format. To learn more about this format and the requirements to use it, see ST_GEOGFROMTEXT.
- WKB in hexadecimal text format. To learn more about this format and the requirements to use it, see ST_GEOGFROMWKB.
- GeoJSON format. To learn more about this format and the requirements to use it, see ST_GEOGFROMGEOJSON.
If expression represents a BYTES value, it must be a valid GEOGRAPHY
binary expression in WKB format. To learn more about this format and the
requirements to use it, see ST_GEOGFROMWKB.
If expression is NULL, the output is NULL.
Return type
GEOGRAPHY
Examples
This takes a WKT-formatted string and returns a GEOGRAPHY polygon:
SELECT ST_GEOGFROM('Polygon((0 0, 0 2, 2 2, 2 0, 0 0))') AS WKT_format
+------------------------------------+
| WKT_format |
+------------------------------------+
| POLYGON((2 0, 2 2, 0 2, 0 0, 2 0)) |
+------------------------------------+
This takes a WKB-formatted hexadecimal-encoded string and returns a GEOGRAPHY point:
SELECT ST_GEOGFROM(FROM_HEX('010100000000000000000000400000000000001040')) AS WKB_format
+----------------+
| WKB_format |
+----------------+
| POINT(2 4) |
+----------------+
This takes WKB-formatted bytes and returns a GEOGRAPHY point:
SELECT ST_GEOGFROM('010100000000000000000000400000000000001040')-AS WKB_format
+----------------+
| WKB_format |
+----------------+
| POINT(2 4) |
+----------------+
This takes a GEOJSON-formatted string and returns a GEOGRAPHY polygon:
SELECT ST_GEOGFROM(
'{ "type": "Polygon", "coordinates": [ [ [2, 0], [2, 2], [1, 2], [0, 2], [0, 0], [2, 0] ] ] }'
) AS GEOJSON_format
+-----------------------------------------+
| GEOJSON_format |
+-----------------------------------------+
| POLYGON((2 0, 2 2, 1 2, 0 2, 0 0, 2 0)) |
+-----------------------------------------+
ST_GEOGFROMGEOJSON
ST_GEOGFROMGEOJSON(geojson_string [, make_valid => constant_expression])
Description
Returns a GEOGRAPHY value that corresponds to the
input GeoJSON representation.
ST_GEOGFROMGEOJSON accepts input that is RFC 7946
compliant.
If the parameter make_valid is set to TRUE, the function attempts to repair
polygons that don't conform to Open Geospatial Consortium semantics.
This parameter uses named argument syntax, and should be specified using
make_valid => argument_value syntax.
A BigQuery GEOGRAPHY has spherical
geodesic edges, whereas a GeoJSON Geometry object explicitly has planar edges.
To convert between these two types of edges, BigQuery adds additional
points to the line where necessary so that the resulting sequence of edges
remains within 10 meters of the original edge.
See ST_ASGEOJSON to format a
GEOGRAPHY as GeoJSON.
Constraints
The input is subject to the following constraints:
ST_GEOGFROMGEOJSONonly accepts JSON geometry fragments and cannot be used to ingest a whole JSON document.- The input JSON fragment must consist of a GeoJSON geometry type, which
includes
Point,MultiPoint,LineString,MultiLineString,Polygon,MultiPolygon, andGeometryCollection. Any other GeoJSON type such asFeatureorFeatureCollectionwill result in an error. - A position in the
coordinatesmember of a GeoJSON geometry type must consist of exactly two elements. The first is the longitude and the second is the latitude. Therefore,ST_GEOGFROMGEOJSONdoes not support the optional third element for a position in thecoordinatesmember.
Return type
GEOGRAPHY
ST_GEOGFROMTEXT
Signature 1
ST_GEOGFROMTEXT(wkt_string[, oriented])
Description
Returns a GEOGRAPHY value that corresponds to the
input WKT representation.
This function supports an optional parameter of type
BOOL, oriented. If this parameter is set to
TRUE, any polygons in the input are assumed to be oriented as follows:
if someone walks along the boundary of the polygon in the order of
the input vertices, the interior of the polygon is on the left. This allows
WKT to represent polygons larger than a hemisphere. If oriented is FALSE or
omitted, this function returns the polygon with the smaller area.
See also ST_MAKEPOLYGONORIENTED which is similar
to ST_GEOGFROMTEXT with oriented=TRUE.
To format GEOGRAPHY as WKT, use
ST_ASTEXT.
Constraints
- All input edges are assumed to be spherical geodesics, and not planar
straight lines. For reading data in a planar projection, consider using
ST_GEOGFROMGEOJSON. For more information on the differences between spherical geodesics and planar lines, see Coordinate systems and edges. - The function does not support three-dimensional geometries that have a
Zsuffix, nor does it support linear referencing system geometries with anMsuffix. - The function only supports geometry primitives and multipart geometries. In particular it supports only Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, and GeometryCollection.
Return type
GEOGRAPHY
Example
The following query reads the WKT string POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))
both as a non-oriented polygon and as an oriented polygon, and checks whether
each result contains the point (1, 1).
WITH polygon AS (SELECT 'Polygon((0 0, 0 2, 2 2, 2 0, 0 0))' AS p)
SELECT
ST_CONTAINS(ST_GEOGFROMTEXT(p), ST_GEOGPOINT(1, 1)) AS fromtext_default,
ST_CONTAINS(ST_GEOGFROMTEXT(p, FALSE), ST_GEOGPOINT(1, 1)) AS non_oriented,
ST_CONTAINS(ST_GEOGFROMTEXT(p, TRUE), ST_GEOGPOINT(1, 1)) AS oriented
FROM polygon;
+-------------------+---------------+-----------+
| fromtext_default | non_oriented | oriented |
+-------------------+---------------+-----------+
| true | true | false |
+-------------------+---------------+-----------+
Signature 2
ST_GEOGFROMTEXT(wkt_string[, oriented => boolean_constant_1]
[, planar => boolean_constant_2] [, make_valid => boolean_constant_3])
Description
Returns a GEOGRAPHY value that corresponds to the
input WKT representation.
This function supports three optional parameters of type
BOOL: oriented, planar, and make_valid.
This signature uses named arguments syntax, and the parameters should be
specified using parameter_name => parameter_value syntax, in any order.
If the oriented parameter is set to
TRUE, any polygons in the input are assumed to be oriented as follows:
if someone walks along the boundary of the polygon in the order of
the input vertices, the interior of the polygon is on the left. This allows
WKT to represent polygons larger than a hemisphere. If oriented is FALSE or
omitted, this function returns the polygon with the smaller area.
See also ST_MAKEPOLYGONORIENTED which is similar
to ST_GEOGFROMTEXT with oriented=TRUE.
If the parameter planar is set to TRUE, the edges of the line strings and
polygons are assumed to use planar map semantics, rather than BigQuery
default spherical geodesics semantics. For more information
on the differences between spherical geodesics and planar lines, see
Coordinate systems and edges.
If the parameter make_valid is set to TRUE, the function attempts to repair
polygons that don't conform to Open Geospatial Consortium semantics.
To format GEOGRAPHY as WKT, use
ST_ASTEXT.
Constraints
- All input edges are assumed to be spherical geodesics by default, and not
planar straight lines. For reading data in a planar projection,
pass
planar => TRUEargument, or consider usingST_GEOGFROMGEOJSON. For more information on the differences between spherical geodesics and planar lines, see Coordinate systems and edges. - The function does not support three-dimensional geometries that have a
Zsuffix, nor does it support linear referencing system geometries with anMsuffix. - The function only supports geometry primitives and multipart geometries. In particular it supports only Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, and GeometryCollection.
orientedandplanarcannot be equal toTRUEat the same time.orientedandmake_validcannot be equal toTRUEat the same time.
Example
The following query reads the WKT string POLYGON((0 0, 0 2, 2 2, 0 2, 0 0))
both as a non-oriented polygon and as an oriented polygon, and checks whether
each result contains the point (1, 1).
WITH polygon AS (SELECT 'Polygon((0 0, 0 2, 2 2, 2 0, 0 0))' AS p)
SELECT
ST_CONTAINS(ST_GEOGFROMTEXT(p), ST_GEOGPOINT(1, 1)) AS fromtext_default,
ST_CONTAINS(ST_GEOGFROMTEXT(p, oriented => FALSE), ST_GEOGPOINT(1, 1)) AS non_oriented,
ST_CONTAINS(ST_GEOGFROMTEXT(p, oriented => TRUE), ST_GEOGPOINT(1, 1)) AS oriented
FROM polygon;
+-------------------+---------------+-----------+
| fromtext_default | non_oriented | oriented |
+-------------------+---------------+-----------+
| true | true | false |
+-------------------+---------------+-----------+
The following query converts a WKT string with an invalid polygon to
GEOGRAPHY. The WKT string violates two properties
of a valid polygon - the loop describing the polygon is not closed, and it
contains self-intersection. With the make_valid option, ST_GeogFromText
successfully converts it to a multipolygon shape.
WITH data AS (
SELECT 'polygon((0 -1, 2 1, 2 -1, 0 1))' wkt)
SELECT
SAFE.ST_GeogFromText(wkt) as geom,
SAFE.ST_GeogFromText(wkt, make_valid => TRUE) as valid_geom
FROM data
+------+-----------------------------------------------------------------+
| geom | valid_geom |
+------+-----------------------------------------------------------------+
| NULL | MULTIPOLYGON(((0 -1, 1 0, 0 1, 0 -1)), ((1 0, 2 -1, 2 1, 1 0))) |
+------+-----------------------------------------------------------------+
ST_GEOGFROMWKB
ST_GEOGFROMWKB(wkb_bytes_expression)
ST_GEOGFROMWKB(wkb_hex_string_expression)
Description
Converts an expression for a hexadecimal-text STRING or BYTES
value into a GEOGRAPHY value. The expression must be in
WKB format.
To format GEOGRAPHY as WKB, use
ST_ASBINARY.
Constraints
All input edges are assumed to be spherical geodesics, and not planar straight
lines. For reading data in a planar projection, consider using
ST_GEOGFROMGEOJSON.
Return type
GEOGRAPHY
ST_GEOGPOINT
ST_GEOGPOINT(longitude, latitude)
Description
Creates a GEOGRAPHY with a single point.
ST_GEOGPOINT creates a point from the specified
FLOAT64 longitude and latitude parameters and
returns that point in a GEOGRAPHY value.
Constraints
- Latitudes must be in the range [-90, 90]. Latitudes outside this range will result in an error.
- Longitudes outside the range [-180, 180] are allowed;
ST_GEOGPOINTuses the input longitude modulo 360 to obtain a longitude within [-180, 180].
Return type
GEOGRAPHY
ST_GEOGPOINTFROMGEOHASH
ST_GEOGPOINTFROMGEOHASH(geohash)
Description
Returns a GEOGRAPHY value that corresponds to a
point in the middle of a bounding box defined in the GeoHash.
Return type
GEOGRAPHY
ST_GEOHASH
ST_GEOHASH(geography_expression, maxchars)
Description
Returns a GeoHash representation of geography_expression. The
resulting GeoHash will contain at most maxchars characters. Fewer characters
corresponds to lower precision (or, described differently, to a bigger
bounding box).
ST_GeoHash over an empty GEOGRAPHY object returns NULL.
Constraints
- Only
GEOGRAPHYobjects representing single points are supported. - The maximum value of
maxcharsis 20.
Return type
STRING
Example
Returns a GeoHash of the Seattle Center with 10 characters of precision.
SELECT ST_GEOHASH(ST_GEOGPOINT(-122.35, 47.62), 10) geohash
+--------------+
| geohash |
+--------------+
| c22yzugqw7 |
+--------------+
ST_INTERSECTION
ST_INTERSECTION(geography_1, geography_2)
Description
Returns a GEOGRAPHY that represents the point set
intersection of the two input GEOGRAPHYs. Thus,
every point in the intersection appears in both geography_1 and geography_2.
If the two input GEOGRAPHYs are disjoint, that is,
there are no points that appear in both input geometry_1 and geometry_2,
then an empty GEOGRAPHY is returned.
See ST_INTERSECTS, ST_DISJOINT for related predicate functions.
Return type
GEOGRAPHY
ST_INTERSECTS
ST_INTERSECTS(geography_1, geography_2)
Description
Returns TRUE if the point set intersection of geography_1 and geography_2
is non-empty. Thus, this function returns TRUE if there is at least one point
that appears in both input GEOGRAPHYs.
If ST_INTERSECTS returns TRUE, it implies that ST_DISJOINT
returns FALSE.
Return type
BOOL
ST_INTERSECTSBOX
ST_INTERSECTSBOX(geography, lng1, lat1, lng2, lat2)
Description
Returns TRUE if geography intersects the rectangle between [lng1, lng2]
and [lat1, lat2]. The edges of the rectangle follow constant lines of
longitude and latitude. lng1 and lng2 specify the westmost and eastmost
constant longitude lines that bound the rectangle, and lat1 and lat2 specify
the minimum and maximum constant latitude lines that bound the rectangle.
Specify all longitude and latitude arguments in degrees.
Constraints
The input arguments are subject to the following constraints:
- Latitudes should be in the
[-90, 90]degree range. - Longitudes should be in the
[-180, 180]degree range.
Return type
BOOL
Example
SELECT p, st_intersectsbox(p, -90, 0, 90, 20) AS box1,
st_intersectsbox(p, 90, 0, -90, 20) AS box2
FROM UNNEST([st_geogpoint(10, 10), st_geogpoint(170, 10),
st_geogpoint(30, 30)]) p
+----------------+--------------+--------------+
| p | box1 | box2 |
+----------------+--------------+--------------+
| POINT(10 10) | true | false |
+----------------+--------------+--------------+
| POINT(170 10) | false | true |
+----------------+--------------+--------------+
| POINT(30 30) | false | false |
+----------------+--------------+--------------+
ST_ISCOLLECTION
ST_ISCOLLECTION(geography_expression)
Description
Returns TRUE if the total number of points, linestrings, and polygons is
greater than one.
An empty GEOGRAPHY is not a collection.
Return type
BOOL
ST_ISEMPTY
ST_ISEMPTY(geography_expression)
Description
Returns TRUE if the given GEOGRAPHY is empty;
that is, the GEOGRAPHY does not contain any points,
lines, or polygons.
NOTE: A BigQuery empty GEOGRAPHY is not
associated with a particular geometry shape. For example, the results of
expressions ST_GEOGFROMTEXT('POINT EMPTY') and
ST_GEOGFROMTEXT('GEOMETRYCOLLECTION EMPTY') are identical.
Return type
BOOL
ST_LENGTH
ST_LENGTH(geography_expression[, use_spheroid])
Description
Returns the total length in meters of the lines in the input
GEOGRAPHY.
If geography_expression is a point or a polygon, returns zero. If
geography_expression is a collection, returns the length of the lines in the
collection; if the collection does not contain lines, returns zero.
The optional use_spheroid parameter determines how this function measures
distance. If use_spheroid is FALSE, the function measures distance on the
surface of a perfect sphere.
The use_spheroid parameter currently only supports
the value FALSE. The default value of use_spheroid is FALSE.
Return type
FLOAT64
ST_MAKELINE
ST_MAKELINE(geography_1, geography_2)
ST_MAKELINE(array_of_geography)
Description
Creates a GEOGRAPHY with a single linestring by
concatenating the point or line vertices of each of the input
GEOGRAPHYs in the order they are given.
ST_MAKELINE comes in two variants. For the first variant, input must be two
GEOGRAPHYs. For the second, input must be an
ARRAY of type
GEOGRAPHY. In either variant, each input
GEOGRAPHY must consist of one of the following
values:
- Exactly one point.
- Exactly one linestring.
For the first variant of ST_MAKELINE, if either input
GEOGRAPHY is NULL, ST_MAKELINE returns NULL.
For the second variant, if input ARRAY or any
element in the input ARRAY is NULL, ST_MAKELINE
returns NULL.
Constraints
Every edge must span strictly less than 180 degrees.
NOTE: BigQuery's snapping process may discard sufficiently short edges
and snap the two endpoints together. For instance, if two input
GEOGRAPHYs each contain a point and the two points
are separated by a distance less than the snap radius, the points will be
snapped together. In such a case the result will be a
GEOGRAPHY with exactly one point.
Return type
GEOGRAPHY
ST_MAKEPOLYGON
ST_MAKEPOLYGON(geography_expression[, array_of_geography])
Description
Creates a GEOGRAPHY containing a single polygon
from linestring inputs, where each input linestring is used to construct a
polygon ring.
ST_MAKEPOLYGON comes in two variants. For the first variant, the input
linestring is provided by a single GEOGRAPHY
containing exactly one linestring. For the second variant, the input consists of
a single GEOGRAPHY and an array of
GEOGRAPHYs, each containing exactly one linestring.
The first GEOGRAPHY in either variant is used to
construct the polygon shell. Additional
GEOGRAPHYs provided in the input
ARRAY specify a polygon hole. For every input
GEOGRAPHY containing
exactly one linestring, the following must be true:
- The linestring must consist of at least three distinct vertices.
- The linestring must be closed: that is, the first and last vertex have to be the same. If the first and last vertex differ, the function constructs a final edge from the first vertex to the last.
For the first variant of ST_MAKEPOLYGON, if either input
GEOGRAPHY is NULL, ST_MAKEPOLYGON returns
NULL. For the second variant, if input ARRAY
or any element in the ARRAY is NULL,
ST_MAKEPOLYGON returns NULL.
NOTE: ST_MAKEPOLYGON accepts an empty GEOGRAPHY
as input. ST_MAKEPOLYGON interprets an empty
GEOGRAPHY as having an empty linestring, which will
create a full loop: that is, a polygon that covers the entire Earth.
Constraints
Together, the input rings must form a valid polygon:
- The polygon shell must cover each of the polygon holes.
- There can be only one polygon shell (which has to be the first input ring). This implies that polygon holes cannot be nested.
- Polygon rings may only intersect in a vertex on the boundary of both rings.
Every edge must span strictly less than 180 degrees.
Each polygon ring divides the sphere into two regions. The first input linesting
to ST_MAKEPOLYGON forms the polygon shell, and the interior is chosen to be
the smaller of the two regions. Each subsequent input linestring specifies a
polygon hole, so the interior of the polygon is already well-defined. In order
to define a polygon shell such that the interior of the polygon is the larger of
the two regions, see ST_MAKEPOLYGONORIENTED.
NOTE: BigQuery's snapping process may discard sufficiently short edges
and snap the two endpoints together. Hence, when vertices are snapped together,
it is possible that a polygon hole that is sufficiently small may disappear, or
the output GEOGRAPHY may contain only a line or a
point.
Return type
GEOGRAPHY
ST_MAKEPOLYGONORIENTED
ST_MAKEPOLYGONORIENTED(array_of_geography)
Description
Like ST_MAKEPOLYGON, but the vertex ordering of each input linestring
determines the orientation of each polygon ring. The orientation of a polygon
ring defines the interior of the polygon as follows: if someone walks along the
boundary of the polygon in the order of the input vertices, the interior of the
polygon is on the left. This applies for each polygon ring provided.
This variant of the polygon constructor is more flexible since
ST_MAKEPOLYGONORIENTED can construct a polygon such that the interior is on
either side of the polygon ring. However, proper orientation of polygon rings is
critical in order to construct the desired polygon.
If the input ARRAY or any element in the
ARRAY is NULL, ST_MAKEPOLYGONORIENTED
returns NULL.
NOTE: The input argument for ST_MAKEPOLYGONORIENTED may contain an empty
GEOGRAPHY. ST_MAKEPOLYGONORIENTED
interprets an empty GEOGRAPHY as having an empty
linestring, which will create a full loop: that is, a polygon that covers the
entire Earth.
Constraints
Together, the input rings must form a valid polygon:
- The polygon shell must cover each of the polygon holes.
- There must be only one polygon shell, which must to be the first input ring. This implies that polygon holes cannot be nested.
- Polygon rings may only intersect in a vertex on the boundary of both rings.
Every edge must span strictly less than 180 degrees.
ST_MAKEPOLYGONORIENTED relies on the ordering of the input vertices of each
linestring to determine the orientation of the polygon. This applies to the
polygon shell and any polygon holes. ST_MAKEPOLYGONORIENTED expects all
polygon holes to have the opposite orientation of the shell. See
ST_MAKEPOLYGON for an alternate polygon constructor, and
other constraints on building a valid polygon.
NOTE: Due to BigQuery's snapping process, edges with a sufficiently
short length will be discarded and the two endpoints will be snapped to a single
point. Therefore, it is possible that vertices in a linestring may be snapped
together such that one or more edge disappears. Hence, it is possible that a
polygon hole that is sufficiently small may disappear, or the resulting
GEOGRAPHY may contain only a line or a point.
Return type
GEOGRAPHY
ST_MAXDISTANCE
ST_MAXDISTANCE(geography_1, geography_2[, use_spheroid])
Returns the longest distance in meters between two non-empty
GEOGRAPHYs; that is, the distance between two
vertices where the first vertex is in the first
GEOGRAPHY, and the second vertex is in the second
GEOGRAPHY. If geography_1 and geography_2 are the
same GEOGRAPHY, the function returns the distance
between the two most distant vertices in that
GEOGRAPHY.
If either of the input GEOGRAPHYs is empty,
ST_MAXDISTANCE returns NULL.
The optional use_spheroid parameter determines how this function measures
distance. If use_spheroid is FALSE, the function measures distance on the
surface of a perfect sphere.
The use_spheroid parameter currently only supports
the value FALSE. The default value of use_spheroid is FALSE.
Return type
FLOAT64
ST_NPOINTS
ST_NPOINTS(geography_expression)
Description
An alias of ST_NUMPOINTS.
ST_NUMPOINTS
ST_NUMPOINTS(geography_expression)
Description
Returns the number of vertices in the input
GEOGRAPHY. This includes the number of points, the
number of linestring vertices, and the number of polygon vertices.
NOTE: The first and last vertex of a polygon ring are counted as distinct vertices.
Return type
INT64
ST_PERIMETER
ST_PERIMETER(geography_expression[, use_spheroid])
Description
Returns the length in meters of the boundary of the polygons in the input
GEOGRAPHY.
If geography_expression is a point or a line, returns zero. If
geography_expression is a collection, returns the perimeter of the polygons
in the collection; if the collection does not contain polygons, returns zero.
The optional use_spheroid parameter determines how this function measures
distance. If use_spheroid is FALSE, the function measures distance on the
surface of a perfect sphere.
The use_spheroid parameter currently only supports
the value FALSE. The default value of use_spheroid is FALSE.
Return type
FLOAT64
ST_POINTN
ST_POINTN(linestring_geography, index)
Description
Returns the Nth point of a linestring geography as a point geography, where N is
the index. The index is 1-based. Negative values are counted backwards from the
end of the linestring, so that -1 is the last point. Returns an error if the
input is not a linestring, if the input is empty, or if there is no vertex at
the given index. Use the SAFE prefix to obtain NULL for invalid input
instead of an error.
Return Type
Point GEOGRAPHY
Example
The following example uses ST_POINTN, ST_STARTPOINT and
ST_ENDPOINT to extract points from a linestring.
WITH linestring AS (
SELECT ST_GeogFromText('linestring(1 1, 2 1, 3 2, 3 3)') g
)
SELECT ST_PointN(g, 1) AS first, ST_PointN(g, -1) AS last,
ST_PointN(g, 2) AS second, ST_PointN(g, -2) AS second_to_last
FROM linestring;
+--------------+--------------+--------------+----------------+
| first | last | second | second_to_last |
+--------------+--------------+--------------+----------------+
| POINT(1 1) | POINT(3 3) | POINT(2 1) | POINT(3 2) |
+--------------+--------------+--------------+----------------+
ST_SIMPLIFY
ST_SIMPLIFY(geography, tolerance_meters)
Description
Returns a simplified version of geography, the given input
GEOGRAPHY. The input
GEOGRAPHY is simplified by replacing nearly straight
chains of short edges with a single long edge. The input geography will not
change by more than the tolerance specified by tolerance_meters. Thus,
simplified edges are guaranteed to pass within tolerance_meters of the
original positions of all vertices that were removed from that edge. The given
tolerance_meters is in meters on the surface of the Earth.
Note that ST_SIMPLIFY preserves topological relationships, which means that
no new crossing edges will be created and the output will be valid. For a large
enough tolerance, adjacent shapes may collapse into a single object, or a shape
could be simplified to a shape with a smaller dimension.
Constraints
For ST_SIMPLIFY to have any effect, tolerance_meters must be non-zero.
ST_SIMPLIFY returns an error if the tolerance specified by tolerance_meters
is one of the following:
- A negative tolerance.
- Greater than ~7800 kilometers.
Return type
GEOGRAPHY
Examples
The following example shows how ST_SIMPLIFY simplifies the input line
GEOGRAPHY by removing intermediate vertices.
WITH example AS
(SELECT ST_GEOGFROMTEXT('LINESTRING(0 0, 0.05 0, 0.1 0, 0.15 0, 2 0)') AS line)
SELECT
line AS original_line,
ST_SIMPLIFY(line, 1) AS simplified_line
FROM example;
+---------------------------------------------+----------------------+
| original_line | simplified_line |
+---------------------------------------------+----------------------+
| LINESTRING(0 0, 0.05 0, 0.1 0, 0.15 0, 2 0) | LINESTRING(0 0, 2 0) |
+---------------------------------------------+----------------------+
The following example illustrates how the result of ST_SIMPLIFY can have a
lower dimension than the original shape.
WITH example AS
(SELECT
ST_GEOGFROMTEXT('POLYGON((0 0, 0.1 0, 0.1 0.1, 0 0))') AS polygon,
t AS tolerance
FROM UNNEST([1000, 10000, 100000]) AS t)
SELECT
polygon AS original_triangle,
tolerance AS tolerance_meters,
ST_SIMPLIFY(polygon, tolerance) AS simplified_result
FROM example
+-------------------------------------+------------------+-------------------------------------+
| original_triangle | tolerance_meters | simplified_result |
+-------------------------------------+------------------+-------------------------------------+
| POLYGON((0 0, 0.1 0, 0.1 0.1, 0 0)) | 1000 | POLYGON((0 0, 0.1 0, 0.1 0.1, 0 0)) |
| POLYGON((0 0, 0.1 0, 0.1 0.1, 0 0)) | 10000 | LINESTRING(0 0, 0.1 0.1) |
| POLYGON((0 0, 0.1 0, 0.1 0.1, 0 0)) | 100000 | POINT(0 0) |
+-------------------------------------+------------------+-------------------------------------+
ST_SNAPTOGRID
ST_SNAPTOGRID(geography_expression, grid_size)
Description
Returns the input GEOGRAPHY, where each vertex has
been snapped to a longitude/latitude grid. The grid size is determined by the
grid_size parameter which is given in degrees.
Constraints
Arbitrary grid sizes are not supported. The grid_size parameter is rounded so
that it is of the form \(10^n\), where \(-10 < n < 0\).
Return type
GEOGRAPHY
ST_STARTPOINT
ST_STARTPOINT(linestring_geography)
Description
Returns the first point of a linestring geography as a point geography. Returns
an error if the input is not a linestring or if the input is empty. Use the
SAFE prefix to obtain NULL for invalid input instead of an error.
Return Type
Point GEOGRAPHY
Example
SELECT ST_StartPoint(ST_GeogFromText('linestring(1 1, 2 1, 3 2, 3 3)')) first
+--------------+
| first |
+--------------+
| POINT(1 1) |
+--------------+
ST_TOUCHES
ST_TOUCHES(geography_1, geography_2)
Description
Returns TRUE provided the following two conditions are satisfied:
geography_1intersectsgeography_2.- The interior of
geography_1and the interior ofgeography_2are disjoint.
Return type
BOOL
ST_UNION
ST_UNION(geography_1, geography_2)
ST_UNION(array_of_geography)
Description
Returns a GEOGRAPHY that represents the point set
union of all input GEOGRAPHYs.
ST_UNION comes in two variants. For the first variant, input must be two
GEOGRAPHYs. For the second, the input is an
ARRAY of type GEOGRAPHY.
For the first variant of ST_UNION, if an input
GEOGRAPHY is NULL, ST_UNION returns NULL.
For the second variant, if the input ARRAY value
is NULL, ST_UNION returns NULL.
For a non-NULL input ARRAY, the union is computed
and NULL elements are ignored so that they do not affect the output.
See ST_UNION_AGG for the aggregate version of ST_UNION.
Return type
GEOGRAPHY
ST_UNION_AGG
ST_UNION_AGG(geography)
Description
Returns a GEOGRAPHY that represents the point set
union of all input GEOGRAPHYs.
ST_UNION_AGG ignores NULL input GEOGRAPHY values.
See ST_UNION for the non-aggregate version of ST_UNION_AGG.
Return type
GEOGRAPHY
ST_WITHIN
ST_WITHIN(geography_1, geography_2)
Description
Returns TRUE if no point of geography_1 is outside of geography_2 and
the interiors of geography_1 and geography_2 intersect.
Given two geographies a and b, ST_WITHIN(a, b) returns the same result
as ST_CONTAINS(b, a). Note the opposite order of arguments.
Return type
BOOL
ST_X
ST_X(geography_expression)
Description
Returns the longitude in degrees of the single-point input
GEOGRAPHY.
For any input GEOGRAPHY that is not a single point,
including an empty GEOGRAPHY, ST_X returns an
error. Use the SAFE. prefix to obtain NULL.
Return type
FLOAT64
Example
The following example uses ST_X and ST_Y to extract coordinates from
single-point geographies.
WITH points AS
(SELECT ST_GEOGPOINT(i, i + 1) AS p FROM UNNEST([0, 5, 12]) AS i)
SELECT
p,
ST_X(p) as longitude,
ST_Y(p) as latitude
FROM points;
+--------------+-----------+----------+
| p | longitude | latitude |
+--------------+-----------+----------+
| POINT(0 1) | 0.0 | 1.0 |
| POINT(5 6) | 5.0 | 6.0 |
| POINT(12 13) | 12.0 | 13.0 |
+--------------+-----------+----------+
ST_Y
ST_Y(geography_expression)
Description
Returns the latitude in degrees of the single-point input
GEOGRAPHY.
For any input GEOGRAPHY that is not a single point,
including an empty GEOGRAPHY, ST_Y returns an
error. Use the SAFE. prefix to return NULL instead.
Return type
FLOAT64
Example
See ST_X for example usage.
Security functions
BigQuery supports the following security functions.
SESSION_USER
SESSION_USER()
Description
Returns the email address of the user that is running the query.
Return Data Type
STRING
Example
SELECT SESSION_USER() as user;
+----------------------+
| user |
+----------------------+
| jdoe@example.com |
+----------------------+
UUID functions
BigQuery supports the following universally unique identifier (UUID) functions.
GENERATE_UUID
GENERATE_UUID()
Description
Returns a random universally unique identifier (UUID) as a STRING.
The returned STRING consists of 32 hexadecimal
digits in five groups separated by hyphens in the form 8-4-4-4-12. The
hexadecimal digits represent 122 random bits and 6 fixed bits, in compliance
with RFC 4122 section 4.4.
The returned STRING is lowercase.
Return Data Type
STRING
Example
The following query generates a random UUID.
SELECT GENERATE_UUID() AS uuid;
The above query generates a result like the following:
+--------------------------------------+
| uuid |
+--------------------------------------+
| 4192bff0-e1e0-43ce-a4db-912808c32493 |
+--------------------------------------+
Net functions
NET.IP_FROM_STRING
NET.IP_FROM_STRING(addr_str)
Description
Converts an IPv4 or IPv6 address from text (STRING) format to binary (BYTES) format in network byte order.
This function supports the following formats for addr_str:
- IPv4: Dotted-quad format. For example,
10.1.2.3. - IPv6: Colon-separated format. For example,
1234:5678:90ab:cdef:1234:5678:90ab:cdef. For more examples, see the IP Version 6 Addressing Architecture.
This function does not support CIDR notation, such as 10.1.2.3/32.
If this function receives a NULL input, it returns NULL. If the input is
considered invalid, an OUT_OF_RANGE error occurs.
Return Data Type
BYTES
Example
SELECT
addr_str, FORMAT("%T", NET.IP_FROM_STRING(addr_str)) AS ip_from_string
FROM UNNEST([
'48.49.50.51',
'::1',
'3031:3233:3435:3637:3839:4041:4243:4445',
'::ffff:192.0.2.128'
]) AS addr_str;
| addr_str | ip_from_string |
|---|---|
| 48.49.50.51 | b"0123" |
| ::1 | b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" |
| 3031:3233:3435:3637:3839:4041:4243:4445 | b"0123456789@ABCDE" |
| ::ffff:192.0.2.128 | b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\x00\x02\x80" |
NET.SAFE_IP_FROM_STRING
NET.SAFE_IP_FROM_STRING(addr_str)
Description
Similar to NET.IP_FROM_STRING, but returns NULL
instead of throwing an error if the input is invalid.
Return Data Type
BYTES
Example
SELECT
addr_str,
FORMAT("%T", NET.SAFE_IP_FROM_STRING(addr_str)) AS safe_ip_from_string
FROM UNNEST([
'48.49.50.51',
'::1',
'3031:3233:3435:3637:3839:4041:4243:4445',
'::ffff:192.0.2.128',
'48.49.50.51/32',
'48.49.50',
'::wxyz'
]) AS addr_str;
| addr_str | safe_ip_from_string |
|---|---|
| 48.49.50.51 | b"0123" |
| ::1 | b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" |
| 3031:3233:3435:3637:3839:4041:4243:4445 | b"0123456789@ABCDE" |
| ::ffff:192.0.2.128 | b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\x00\x02\x80" |
| 48.49.50.51/32 | NULL |
| 48.49.50 | NULL |
| ::wxyz | NULL |
NET.IP_TO_STRING
NET.IP_TO_STRING(addr_bin)
Description Converts an IPv4 or IPv6 address from binary (BYTES) format in network byte order to text (STRING) format.
If the input is 4 bytes, this function returns an IPv4 address as a STRING. If the input is 16 bytes, it returns an IPv6 address as a STRING.
If this function receives a NULL input, it returns NULL. If the input has
a length different from 4 or 16, an OUT_OF_RANGE error occurs.
Return Data Type
STRING
Example
SELECT FORMAT("%T", x) AS addr_bin, NET.IP_TO_STRING(x) AS ip_to_string
FROM UNNEST([
b"0123",
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
b"0123456789@ABCDE",
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\x00\x02\x80"
]) AS x;
| addr_bin | ip_to_string |
|---|---|
| b"0123" | 48.49.50.51 |
| b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" | ::1 |
| b"0123456789@ABCDE" | 3031:3233:3435:3637:3839:4041:4243:4445 |
| b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\x00\x02\x80" | ::ffff:192.0.2.128 |
NET.IP_NET_MASK
NET.IP_NET_MASK(num_output_bytes, prefix_length)
Description
Returns a network mask: a byte sequence with length equal to num_output_bytes,
where the first prefix_length bits are set to 1 and the other bits are set to
0. num_output_bytes and prefix_length are INT64.
This function throws an error if num_output_bytes is not 4 (for IPv4) or 16
(for IPv6). It also throws an error if prefix_length is negative or greater
than 8 * num_output_bytes.
Return Data Type
BYTES
Example
SELECT x, y, FORMAT("%T", NET.IP_NET_MASK(x, y)) AS ip_net_mask
FROM UNNEST([
STRUCT(4 as x, 0 as y),
(4, 20),
(4, 32),
(16, 0),
(16, 1),
(16, 128)
]);
| x | y | ip_net_mask |
|---|---|---|
| 4 | 0 | b"\x00\x00\x00\x00" |
| 4 | 20 | b"\xff\xff\xf0\x00" |
| 4 | 32 | b"\xff\xff\xff\xff" |
| 16 | 0 | b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 16 | 1 | b"\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 16 | 128 | b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" |
NET.IP_TRUNC
NET.IP_TRUNC(addr_bin, prefix_length)
Description
Takes addr_bin, an IPv4 or IPv6 address in binary (BYTES) format in network
byte order, and returns a subnet address in the same format. The result has the
same length as addr_bin, where the first prefix_length bits are equal to
those in addr_bin and the remaining bits are 0.
This function throws an error if LENGTH(addr_bin) is not 4 or 16, or if
prefix_len is negative or greater than LENGTH(addr_bin) * 8.
Return Data Type
BYTES
Example
SELECT
FORMAT("%T", x) as addr_bin, prefix_length,
FORMAT("%T", NET.IP_TRUNC(x, prefix_length)) AS ip_trunc
FROM UNNEST([
STRUCT(b"\xAA\xBB\xCC\xDD" as x, 0 as prefix_length),
(b"\xAA\xBB\xCC\xDD", 11), (b"\xAA\xBB\xCC\xDD", 12),
(b"\xAA\xBB\xCC\xDD", 24), (b"\xAA\xBB\xCC\xDD", 32),
(b'0123456789@ABCDE', 80)
]);
| addr_bin | prefix_length | ip_trunc |
|---|---|---|
| b"\xaa\xbb\xcc\xdd" | 0 | b"\x00\x00\x00\x00" |
| b"\xaa\xbb\xcc\xdd" | 11 | b"\xaa\xa0\x00\x00" |
| b"\xaa\xbb\xcc\xdd" | 12 | b"\xaa\xb0\x00\x00" |
| b"\xaa\xbb\xcc\xdd" | 24 | b"\xaa\xbb\xcc\x00" |
| b"\xaa\xbb\xcc\xdd" | 32 | b"\xaa\xbb\xcc\xdd" |
| b"0123456789@ABCDE" | 80 | b"0123456789\x00\x00\x00\x00\x00\x00" |
NET.IPV4_FROM_INT64
NET.IPV4_FROM_INT64(integer_value)
Description
Converts an IPv4 address from integer format to binary (BYTES) format in network
byte order. In the integer input, the least significant bit of the IP address is
stored in the least significant bit of the integer, regardless of host or client
architecture. For example, 1 means 0.0.0.1, and 0x1FF means 0.0.1.255.
This function checks that either all the most significant 32 bits are 0, or all
the most significant 33 bits are 1 (sign-extended from a 32-bit integer).
In other words, the input should be in the range [-0x80000000, 0xFFFFFFFF];
otherwise, this function throws an error.
This function does not support IPv6.
Return Data Type
BYTES
Example
SELECT x, x_hex, FORMAT("%T", NET.IPV4_FROM_INT64(x)) AS ipv4_from_int64
FROM (
SELECT CAST(x_hex AS INT64) x, x_hex
FROM UNNEST(["0x0", "0xABCDEF", "0xFFFFFFFF", "-0x1", "-0x2"]) AS x_hex
);
| x | x_hex | ipv4_from_int64 |
|---|---|---|
| 0 | 0x0 | b"\x00\x00\x00\x00" |
| 11259375 | 0xABCDEF | b"\x00\xab\xcd\xef" |
| 4294967295 | 0xFFFFFFFF | b"\xff\xff\xff\xff" |
| -1 | -0x1 | b"\xff\xff\xff\xff" |
| -2 | -0x2 | b"\xff\xff\xff\xfe" |
NET.IPV4_TO_INT64
NET.IPV4_TO_INT64(addr_bin)
Description
Converts an IPv4 address from binary (BYTES) format in network byte order to
integer format. In the integer output, the least significant bit of the IP
address is stored in the least significant bit of the integer, regardless of
host or client architecture. For example, 1 means 0.0.0.1, and 0x1FF means
0.0.1.255. The output is in the range [0, 0xFFFFFFFF].
If the input length is not 4, this function throws an error.
This function does not support IPv6.
Return Data Type
INT64
Example
SELECT
FORMAT("%T", x) AS addr_bin,
FORMAT("0x%X", NET.IPV4_TO_INT64(x)) AS ipv4_to_int64
FROM
UNNEST([b"\x00\x00\x00\x00", b"\x00\xab\xcd\xef", b"\xff\xff\xff\xff"]) AS x;
| addr_bin | ipv4_to_int64 |
|---|---|
| b"\x00\x00\x00\x00" | 0x0 |
| b"\x00\xab\xcd\xef" | 0xABCDEF |
| b"\xff\xff\xff\xff" | 0xFFFFFFFF |
NET.HOST
NET.HOST(url)
Description
Takes a URL as a STRING and returns the host as a STRING. For best results, URL values should comply with the format as defined by RFC 3986. If the URL value does not comply with RFC 3986 formatting, this function makes a best effort to parse the input and return a relevant result. If the function cannot parse the input, it returns NULL.
Note: The function does not perform any normalization.
Return Data Type
STRING
Example
SELECT
FORMAT("%T", input) AS input,
description,
FORMAT("%T", NET.HOST(input)) AS host,
FORMAT("%T", NET.PUBLIC_SUFFIX(input)) AS suffix,
FORMAT("%T", NET.REG_DOMAIN(input)) AS domain
FROM (
SELECT "" AS input, "invalid input" AS description
UNION ALL SELECT "http://abc.xyz", "standard URL"
UNION ALL SELECT "//user:password@a.b:80/path?query",
"standard URL with relative scheme, port, path and query, but no public suffix"
UNION ALL SELECT "https://[::1]:80", "standard URL with IPv6 host"
UNION ALL SELECT "http://例子.卷筒纸.中国", "standard URL with internationalized domain name"
UNION ALL SELECT " www.Example.Co.UK ",
"non-standard URL with spaces, upper case letters, and without scheme"
UNION ALL SELECT "mailto:?to=&subject=&body=", "URI rather than URL--unsupported"
);
| input | description | host | suffix | domain |
|---|---|---|---|---|
| "" | invalid input | NULL | NULL | NULL |
| "http://abc.xyz" | standard URL | "abc.xyz" | "xyz" | "abc.xyz" |
| "//user:password@a.b:80/path?query" | standard URL with relative scheme, port, path and query, but no public suffix | "a.b" | NULL | NULL |
| "https://[::1]:80" | standard URL with IPv6 host | "[::1]" | NULL | NULL |
| "http://例子.卷筒纸.中国" | standard URL with internationalized domain name | "例子.卷筒纸.中国" | "中国" | "卷筒纸.中国" |
| " www.Example.Co.UK " | non-standard URL with spaces, upper case letters, and without scheme | "www.Example.Co.UK" | "Co.UK" | "Example.Co.UK" |
| "mailto:?to=&subject=&body=" | URI rather than URL--unsupported | "mailto" | NULL | NULL |
NET.PUBLIC_SUFFIX
NET.PUBLIC_SUFFIX(url)
Description
Takes a URL as a STRING and returns the public suffix (such as com, org,
or net) as a STRING. A public suffix is an ICANN domain registered at
publicsuffix.org. For best results, URL values
should comply with the format as defined by
RFC 3986. If the URL value does not comply
with RFC 3986 formatting, this function makes a best effort to parse the input
and return a relevant result.
This function returns NULL if any of the following is true:
- It cannot parse the host from the input;
- The parsed host contains adjacent dots in the middle (not leading or trailing);
- The parsed host does not contain any public suffix.
Before looking up the public suffix, this function temporarily normalizes the host by converting upper case English letters to lower case and encoding all non-ASCII characters with Punycode. The function then returns the public suffix as part of the original host instead of the normalized host.
Note: The function does not perform Unicode normalization.
Note: The public suffix data at publicsuffix.org also contains private domains. This function ignores the private domains.
Note: The public suffix data may change over time. Consequently, input that produces a NULL result now may produce a non-NULL value in the future.
Return Data Type
STRING
Example
SELECT
FORMAT("%T", input) AS input,
description,
FORMAT("%T", NET.HOST(input)) AS host,
FORMAT("%T", NET.PUBLIC_SUFFIX(input)) AS suffix,
FORMAT("%T", NET.REG_DOMAIN(input)) AS domain
FROM (
SELECT "" AS input, "invalid input" AS description
UNION ALL SELECT "http://abc.xyz", "standard URL"
UNION ALL SELECT "//user:password@a.b:80/path?query",
"standard URL with relative scheme, port, path and query, but no public suffix"
UNION ALL SELECT "https://[::1]:80", "standard URL with IPv6 host"
UNION ALL SELECT "http://例子.卷筒纸.中国", "standard URL with internationalized domain name"
UNION ALL SELECT " www.Example.Co.UK ",
"non-standard URL with spaces, upper case letters, and without scheme"
UNION ALL SELECT "mailto:?to=&subject=&body=", "URI rather than URL--unsupported"
);
| input | description | host | suffix | domain |
|---|---|---|---|---|
| "" | invalid input | NULL | NULL | NULL |
| "http://abc.xyz" | standard URL | "abc.xyz" | "xyz" | "abc.xyz" |
| "//user:password@a.b:80/path?query" | standard URL with relative scheme, port, path and query, but no public suffix | "a.b" | NULL | NULL |
| "https://[::1]:80" | standard URL with IPv6 host | "[::1]" | NULL | NULL |
| "http://例子.卷筒纸.中国" | standard URL with internationalized domain name | "例子.卷筒纸.中国" | "中国" | "卷筒纸.中国" |
| " www.Example.Co.UK " | non-standard URL with spaces, upper case letters, and without scheme | "www.Example.Co.UK" | "Co.UK" | "Example.Co.UK |
| "mailto:?to=&subject=&body=" | URI rather than URL--unsupported | "mailto" | NULL | NULL |
NET.REG_DOMAIN
NET.REG_DOMAIN(url)
Description
Takes a URL as a STRING and returns the registered or registerable domain (the public suffix plus one preceding label), as a STRING. For best results, URL values should comply with the format as defined by RFC 3986. If the URL value does not comply with RFC 3986 formatting, this function makes a best effort to parse the input and return a relevant result.
This function returns NULL if any of the following is true:
- It cannot parse the host from the input;
- The parsed host contains adjacent dots in the middle (not leading or trailing);
- The parsed host does not contain any public suffix;
- The parsed host contains only a public suffix without any preceding label.
Before looking up the public suffix, this function temporarily normalizes the host by converting upper case English letters to lowercase and encoding all non-ASCII characters with Punycode. The function then returns the registered or registerable domain as part of the original host instead of the normalized host.
Note: The function does not perform Unicode normalization.
Note: The public suffix data at publicsuffix.org also contains private domains. This function does not treat a private domain as a public suffix. For example, if "us.com" is a private domain in the public suffix data, NET.REG_DOMAIN("foo.us.com") returns "us.com" (the public suffix "com" plus the preceding label "us") rather than "foo.us.com" (the private domain "us.com" plus the preceding label "foo").
Note: The public suffix data may change over time. Consequently, input that produces a NULL result now may produce a non-NULL value in the future.
Return Data Type
STRING
Example
SELECT
FORMAT("%T", input) AS input,
description,
FORMAT("%T", NET.HOST(input)) AS host,
FORMAT("%T", NET.PUBLIC_SUFFIX(input)) AS suffix,
FORMAT("%T", NET.REG_DOMAIN(input)) AS domain
FROM (
SELECT "" AS input, "invalid input" AS description
UNION ALL SELECT "http://abc.xyz", "standard URL"
UNION ALL SELECT "//user:password@a.b:80/path?query",
"standard URL with relative scheme, port, path and query, but no public suffix"
UNION ALL SELECT "https://[::1]:80", "standard URL with IPv6 host"
UNION ALL SELECT "http://例子.卷筒纸.中国", "standard URL with internationalized domain name"
UNION ALL SELECT " www.Example.Co.UK ",
"non-standard URL with spaces, upper case letters, and without scheme"
UNION ALL SELECT "mailto:?to=&subject=&body=", "URI rather than URL--unsupported"
);
| input | description | host | suffix | domain |
|---|---|---|---|---|
| "" | invalid input | NULL | NULL | NULL |
| "http://abc.xyz" | standard URL | "abc.xyz" | "xyz" | "abc.xyz" |
| "//user:password@a.b:80/path?query" | standard URL with relative scheme, port, path and query, but no public suffix | "a.b" | NULL | NULL |
| "https://[::1]:80" | standard URL with IPv6 host | "[::1]" | NULL | NULL |
| "http://例子.卷筒纸.中国" | standard URL with internationalized domain name | "例子.卷筒纸.中国" | "中国" | "卷筒纸.中国" |
| " www.Example.Co.UK " | non-standard URL with spaces, upper case letters, and without scheme | "www.Example.Co.UK" | "Co.UK" | "Example.Co.UK" |
| "mailto:?to=&subject=&body=" | URI rather than URL--unsupported | "mailto" | NULL | NULL |
Operators
Operators are represented by special characters or keywords; they do not use function call syntax. An operator manipulates any number of data inputs, also called operands, and returns a result.
Common conventions:
- Unless otherwise specified, all operators return
NULLwhen one of the operands isNULL. - All operators will throw an error if the computation result overflows.
- For all floating point operations,
+/-infandNaNmay only be returned if one of the operands is+/-inforNaN. In other cases, an error is returned.
Operator precedence
The following table lists all BigQuery operators from highest to lowest precedence, i.e. the order in which they will be evaluated within a statement.
| Order of Precedence | Operator | Input Data Types | Name | Operator Arity |
|---|---|---|---|---|
| 1 | . | STRUCT |
Field access operator | Binary |
| Array subscript operator | ARRAY | Array position. Must be used with OFFSET or ORDINAL—see Array Functions . | Binary | |
| 2 | + | All numeric types | Unary plus | Unary |
| - | All numeric types | Unary minus | Unary | |
| ~ | Integer or BYTES | Bitwise not | Unary | |
| 3 | * | All numeric types | Multiplication | Binary |
| / | All numeric types | Division | Binary | |
| || | STRING, BYTES, or ARRAY<T> | Concatenation operator | Binary | |
| 4 | + | All numeric types DATE and INT64 |
Addition | Binary |
| - | All numeric types DATE and INT64 |
Subtraction | Binary | |
| 5 | << | Integer or BYTES | Bitwise left-shift | Binary |
| >> | Integer or BYTES | Bitwise right-shift | Binary | |
| 6 | & | Integer or BYTES | Bitwise and | Binary |
| 7 | ^ | Integer or BYTES | Bitwise xor | Binary |
| 8 | | | Integer or BYTES | Bitwise or | Binary |
| 9 (Comparison Operators) | = | Any comparable type. See Data Types for a complete list. | Equal | Binary |
| < | Any comparable type. See Data Types for a complete list. | Less than | Binary | |
| > | Any comparable type. See Data Types for a complete list. | Greater than | Binary | |
| <= | Any comparable type. See Data Types for a complete list. | Less than or equal to | Binary | |
| >= | Any comparable type. See Data Types for a complete list. | Greater than or equal to | Binary | |
| !=, <> | Any comparable type. See Data Types for a complete list. | Not equal | Binary | |
| [NOT] LIKE | STRING and byte | Value does [not] match the pattern specified | Binary | |
| [NOT] BETWEEN | Any comparable types. See Data Types for a complete list. | Value is [not] within the range specified | Binary | |
| [NOT] IN | Any comparable types. See Data Types for a complete list. | Value is [not] in the set of values specified | Binary | |
IS [NOT] NULL |
All | Value is [not] NULL |
Unary | |
| IS [NOT] TRUE | BOOL | Value is [not] TRUE. | Unary | |
| IS [NOT] FALSE | BOOL | Value is [not] FALSE. | Unary | |
| 10 | NOT | BOOL | Logical NOT | Unary |
| 11 | AND | BOOL | Logical AND | Binary |
| 12 | OR | BOOL | Logical OR | Binary |
Operators with the same precedence are left associative. This means that those operators are grouped together starting from the left and moving right. For example, the expression:
x AND y AND z
is interpreted as
( ( x AND y ) AND z )
The expression:
x * y / z
is interpreted as:
( ( x * y ) / z )
All comparison operators have the same priority, but comparison operators are not associative. Therefore, parentheses are required in order to resolve ambiguity. For example:
(x < y) IS FALSE
Field access operator
expression.fieldname[. ...]
Description
Gets the value of a field. Alternatively known as the dot operator. Can be
used to access nested fields. For example, expression.fieldname1.fieldname2.
Input types
STRUCT
Return type
- For
STRUCT: SQL data type offieldname. If a field is not found in the struct, an error is thrown.
Array subscript operator
array_expression [position_keyword (array_element_id)]
Description
Get a value in an array at a specific location. Supported by some array functions.
Input types
position_keyword:OFFSETorORDINAL. To learn more, see OFFSET and ORDINALarray_element_id: An integer that represents an index in the array.
Return type
Type T stored at the index in an array.
Arithmetic operators
All arithmetic operators accept input of numeric type T, and the result type has type T unless otherwise indicated in the description below:
| Name | Syntax |
|---|---|
| Addition | X + Y |
| Subtraction | X - Y |
| Multiplication | X * Y |
| Division | X / Y |
| Unary Plus | + X |
| Unary Minus | - X |
NOTE: Divide by zero operations return an error. To return a different result, consider the IEEE_DIVIDE or SAFE_DIVIDE functions.
Result types for Addition, Subtraction and Multiplication:
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| INT64 | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
| FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
Result types for Division:
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| INT64 | FLOAT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
| NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
| BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
| FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
Result types for Unary Plus:
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
Result types for Unary Minus:
| INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
|---|---|---|---|---|
| OUTPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
Date arithmetics operators
Operators '+' and '-' can be used for arithmetic operations on dates.
date_expression + int64_expression
int64_expression + date_expression
date_expression - int64_expression
Description
Adds or subtracts int64_expression days to or from date_expression. This is
equivalent to DATE_ADD or DATE_SUB functions, when interval is expressed in
days.
Return Data Type
DATE
Example
SELECT DATE "2020-09-22" + 1 AS day_later, DATE "2020-09-22" - 7 AS week_ago
+------------+------------+
| day_later | week_ago |
+------------+------------+
| 2020-09-23 | 2020-09-15 |
+------------+------------+
Bitwise operators
All bitwise operators return the same type and the same length as the first operand.
| Name | Syntax | Input Data Type | Description |
|---|---|---|---|
| Bitwise not | ~ X | Integer or BYTES | Performs logical negation on each bit, forming the ones' complement of the given binary value. |
| Bitwise or | X | Y | X: Integer or BYTES
Y: Same type as X |
Takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of the corresponding bits. This operator throws an error if X and Y are BYTES of different lengths. |
| Bitwise xor | X ^ Y | X: Integer or BYTES
Y: Same type as X |
Takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of the corresponding bits. This operator throws an error if X and Y are BYTES of different lengths. |
| Bitwise and | X & Y | X: Integer or BYTES
Y: Same type as X |
Takes two bit patterns of equal length and performs the logical AND operation on each pair of the corresponding bits. This operator throws an error if X and Y are BYTES of different lengths. |
| Left shift | X << Y | X: Integer or BYTES
Y: INT64 |
Shifts the first operand X to the left. This operator returns 0 or a byte sequence of b'\x00' if the second operand Y is greater than or equal to the bit length of the first operand X (for example, 64 if X has the type INT64). This operator throws an error if Y is negative. |
| Right shift | X >> Y | X: Integer or BYTES
Y: INT64 |
Shifts the first operand X to the right. This operator does not do sign bit extension with a signed type (i.e. it fills vacant bits on the left with 0). This operator returns 0 or a byte sequence of b'\x00' if the second operand Y is greater than or equal to the bit length of the first operand X (for example, 64 if X has the type INT64). This operator throws an error if Y is negative. |
Logical operators
BigQuery supports the AND, OR, and NOT logical operators.
Logical operators allow only BOOL or NULL input
and use three-valued logic
to produce a result. The result can be TRUE, FALSE, or NULL:
| x | y | x AND y | x OR y |
|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE | TRUE |
| TRUE | NULL | NULL | TRUE |
| FALSE | TRUE | FALSE | TRUE |
| FALSE | FALSE | FALSE | FALSE |
| FALSE | NULL | FALSE | NULL |
| NULL | TRUE | NULL | TRUE |
| NULL | FALSE | FALSE | NULL |
| NULL | NULL | NULL | NULL |
| x | NOT x |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
| NULL | NULL |
Examples
The examples in this section reference a table called entry_table:
+-------+
| entry |
+-------+
| a |
| b |
| c |
| NULL |
+-------+
SELECT 'a' FROM entry_table WHERE entry = 'a'
-- a => 'a' = 'a' => TRUE
-- b => 'b' = 'a' => FALSE
-- NULL => NULL = 'a' => NULL
+-------+
| entry |
+-------+
| a |
+-------+
SELECT entry FROM entry_table WHERE NOT (entry = 'a')
-- a => NOT('a' = 'a') => NOT(TRUE) => FALSE
-- b => NOT('b' = 'a') => NOT(FALSE) => TRUE
-- NULL => NOT(NULL = 'a') => NOT(NULL) => NULL
+-------+
| entry |
+-------+
| b |
| c |
+-------+
SELECT entry FROM entry_table WHERE entry IS NULL
-- a => 'a' IS NULL => FALSE
-- b => 'b' IS NULL => FALSE
-- NULL => NULL IS NULL => TRUE
+-------+
| entry |
+-------+
| NULL |
+-------+
Comparison operators
Comparisons always return BOOL. Comparisons generally require both operands to be of the same type. If operands are of different types, and if BigQuery can convert the values of those types to a common type without loss of precision, BigQuery will generally coerce them to that common type for the comparison; BigQuery will generally coerce literals to the type of non-literals, where present. Comparable data types are defined in Data Types.
STRUCTs support only 4 comparison operators: equal (=), not equal (!= and <>), and IN.
The following rules apply when comparing these data types:
- FLOAT64:
All comparisons with NaN return FALSE,
except for
!=and<>, which return TRUE. - BOOL: FALSE is less than TRUE.
- STRING: Strings are compared codepoint-by-codepoint, which means that canonically equivalent strings are only guaranteed to compare as equal if they have been normalized first.
NULL: The convention holds here: any operation with aNULLinput returnsNULL.
| Name | Syntax | Description |
|---|---|---|
| Less Than | X < Y | Returns TRUE if X is less than Y. |
| Less Than or Equal To | X <= Y | Returns TRUE if X is less than or equal to Y. |
| Greater Than | X > Y | Returns TRUE if X is greater than Y. |
| Greater Than or Equal To | X >= Y | Returns TRUE if X is greater than or equal to Y. |
| Equal | X = Y | Returns TRUE if X is equal to Y. |
| Not Equal | X != Y X <> Y |
Returns TRUE if X is not equal to Y. |
| BETWEEN | X [NOT] BETWEEN Y AND Z | Returns TRUE if X is [not] within the range specified. The result of "X BETWEEN Y AND Z" is equivalent to "Y <= X AND X <= Z" but X is evaluated only once in the former. |
| LIKE | X [NOT] LIKE Y | Checks if the STRING in the first operand X
matches a pattern specified by the second operand Y. Expressions can contain
these characters:
|
| IN | Multiple - see below | Returns FALSE if the right operand is empty. Returns NULL if the left
operand is NULL. Returns TRUE or NULL, never FALSE, if the right operand
contains NULL. Arguments on either side of IN are general expressions. Neither
operand is required to be a literal, although using a literal on the right is
most common. X is evaluated only once. |
When testing values that have a STRUCT data type for
equality, it's possible that one or more fields are NULL. In such cases:
- If all non-NULL field values are equal, the comparison returns NULL.
- If any non-NULL field values are not equal, the comparison returns false.
The following table demonstrates how STRUCT data
types are compared when they have fields that are NULL valued.
| Struct1 | Struct2 | Struct1 = Struct2 |
|---|---|---|
STRUCT(1, NULL) |
STRUCT(1, NULL) |
NULL |
STRUCT(1, NULL) |
STRUCT(2, NULL) |
FALSE |
STRUCT(1,2) |
STRUCT(1, NULL) |
NULL |
IN operators
The IN operator supports the following syntaxes:
x [NOT] IN (y, z, ... ) # Requires at least one element
x [NOT] IN (<subquery>)
x [NOT] IN UNNEST(<array expression>) # analysis error if the expression
# does not return an ARRAY type.
Arguments on either side of the IN operator are general expressions.
It is common to use literals on the right side expression; however, this is not
required.
The semantics of:
x IN (y, z, ...)
are defined as equivalent to:
(x = y) OR (x = z) OR ...
and the subquery and array forms are defined similarly.
x NOT IN ...
is equivalent to:
NOT(x IN ...)
The UNNEST form treats an array scan like UNNEST in the
FROM clause:
x [NOT] IN UNNEST(<array expression>)
This form is often used with ARRAY parameters. For example:
x IN UNNEST(@array_parameter)
Note: A NULL ARRAY will be treated equivalently to an empty ARRAY.
See the Arrays topic for more information on how to use this syntax.
When using the IN operator, the following semantics apply:
INwith an empty right side expression is always FALSEINwith aNULLleft side expression and a non-empty right side expression is alwaysNULLINwith aNULLin theIN-list can only return TRUE orNULL, never FALSENULL IN (NULL)returnsNULLIN UNNEST(<NULL array>)returns FALSE (notNULL)NOT INwith aNULLin theIN-list can only return FALSE orNULL, never TRUE
IN can be used with multi-part keys by using the struct constructor syntax.
For example:
(Key1, Key2) IN ( (12,34), (56,78) )
(Key1, Key2) IN ( SELECT (table.a, table.b) FROM table )
See the Struct Type section of the Data Types topic for more information on this syntax.
IS operators
IS operators return TRUE or FALSE for the condition they are testing. They never
return NULL, even for NULL inputs, unlike the IS_INF and IS_NAN functions
defined in Mathematical Functions. If NOT is present,
the output BOOL value is inverted.
| Function Syntax | Input Data Type | Result Data Type | Description |
|---|---|---|---|
X IS [NOT] NULL |
Any value type | BOOL | Returns TRUE if the operand X evaluates to NULL, and returns FALSE
otherwise. |
X IS [NOT] TRUE |
BOOL | BOOL | Returns TRUE if the BOOL operand evaluates to TRUE. Returns FALSE otherwise. |
X IS [NOT] FALSE |
BOOL | BOOL | Returns TRUE if the BOOL operand evaluates to FALSE. Returns FALSE otherwise. |
Concatenation operator
The concatenation operator combines multiple values into one.
| Function Syntax | Input Data Type | Result Data Type |
|---|---|---|
STRING || STRING [ || ... ] |
STRING | STRING |
BYTES || BYTES [ || ... ] |
BYTES | STRING |
ARRAY<T> || ARRAY<T> [ || ... ] |
ARRAY<T> | ARRAY<T> |
Conditional expressions
Conditional expressions impose constraints on the evaluation order of their inputs. In essence, they are evaluated left to right, with short-circuiting, and only evaluate the output value that was chosen. In contrast, all inputs to regular functions are evaluated before calling the function. Short-circuiting in conditional expressions can be exploited for error handling or performance tuning.
CASE expr
CASE expr
WHEN expr_to_match THEN result
[ ... ]
[ ELSE else_result ]
END
Description
Compares expr to expr_to_match of each successive WHEN clause and returns
the first result where this comparison returns true. The remaining WHEN
clauses and else_result are not evaluated. If the expr = expr_to_match
comparison returns false or NULL for all WHEN clauses, returns else_result
if present; if not present, returns NULL.
expr and expr_to_match can be any type. They must be implicitly
coercible to a common supertype; equality comparisons are
done on coerced values. There may be multiple result types. result and
else_result expressions must be coercible to a common supertype.
Return Data Type
Supertype of result[, ...] and else_result.
Example
WITH Numbers AS
(SELECT 90 as A, 2 as B UNION ALL
SELECT 50, 8 UNION ALL
SELECT 60, 6 UNION ALL
SELECT 50, 10)
SELECT A, B,
CASE A
WHEN 90 THEN 'red'
WHEN 50 THEN 'blue'
ELSE 'green'
END
AS result
FROM Numbers
+------------------+
| A | B | result |
+------------------+
| 90 | 2 | red |
| 50 | 8 | blue |
| 60 | 6 | green |
| 50 | 10 | blue |
+------------------+
CASE
CASE
WHEN condition THEN result
[ ... ]
[ ELSE else_result ]
END
Description
Evaluates the condition of each successive WHEN clause and returns the
first result where the condition is true; any remaining WHEN clauses
and else_result are not evaluated. If all conditions are false or NULL,
returns else_result if present; if not present, returns NULL.
condition must be a boolean expression. There may be multiple result types.
result and else_result expressions must be implicitly coercible to a
common supertype.
Return Data Type
Supertype of result[, ...] and else_result.
Example
WITH Numbers AS
(SELECT 90 as A, 2 as B UNION ALL
SELECT 50, 6 UNION ALL
SELECT 20, 10)
SELECT A, B,
CASE
WHEN A > 60 THEN 'red'
WHEN A > 30 THEN 'blue'
ELSE 'green'
END
AS result
FROM Numbers
+------------------+
| A | B | result |
+------------------+
| 90 | 2 | red |
| 50 | 6 | blue |
| 20 | 10 | green |
+------------------+
COALESCE
COALESCE(expr[, ...])
Description
Returns the value of the first non-null expression. The remaining expressions are not evaluated. An input expression can be any type. There may be multiple input expression types. All input expressions must be implicitly coercible to a common supertype.
Return Data Type
Supertype of expr[, ...].
Examples
SELECT COALESCE('A', 'B', 'C') as result
+--------+
| result |
+--------+
| A |
+--------+
SELECT COALESCE(NULL, 'B', 'C') as result
+--------+
| result |
+--------+
| B |
+--------+
IF
IF(expr, true_result, else_result)
Description
If expr is true, returns true_result, else returns else_result.
else_result is not evaluated if expr is true. true_result is not
evaluated if expr is false or NULL.
expr must be a boolean expression. true_result and else_result
must be coercible to a common supertype.
Return Data Type
Supertype of true_result and else_result.
Example
WITH Numbers AS
(SELECT 10 as A, 20 as B UNION ALL
SELECT 50, 30 UNION ALL
SELECT 60, 60)
SELECT
A, B,
IF( A<B, 'true', 'false') as result
FROM Numbers
+------------------+
| A | B | result |
+------------------+
| 10 | 20 | true |
| 50 | 30 | false |
| 60 | 60 | false |
+------------------+
IFNULL
IFNULL(expr, null_result)
Description
If expr is NULL, return null_result. Otherwise, return expr. If expr
is not NULL, null_result is not evaluated.
expr and null_result can be any type and must be implicitly coercible to
a common supertype. Synonym for
COALESCE(expr, null_result).
Return Data Type
Supertype of expr or null_result.
Examples
SELECT IFNULL(NULL, 0) as result
+--------+
| result |
+--------+
| 0 |
+--------+
SELECT IFNULL(10, 0) as result
+--------+
| result |
+--------+
| 10 |
+--------+
NULLIF
NULLIF(expr, expr_to_match)
Description
Returns NULL if expr = expr_to_match is true, otherwise
returns expr.
expr and expr_to_match must be implicitly coercible to a
common supertype, and must be comparable.
Return Data Type
Supertype of expr and expr_to_match.
Example
SELECT NULLIF(0, 0) as result
+--------+
| result |
+--------+
| NULL |
+--------+
SELECT NULLIF(10, 0) as result
+--------+
| result |
+--------+
| 10 |
+--------+
Expression subqueries
There are four types of expression subqueries, i.e. subqueries that are used as
expressions. Expression subqueries return NULL or a single value, as opposed to
a column or table, and must be surrounded by parentheses. For a fuller
discussion of subqueries, see
Subqueries.
| Type of Subquery | Result Data Type | Description |
|---|---|---|
| Scalar | Any type T | A subquery in parentheses inside an expression (e.g. in the
SELECT list or WHERE clause) is interpreted as a
scalar subquery. The SELECT list in a scalar subquery must have
exactly one field. If the subquery returns exactly one row, that single value is
the scalar subquery result. If the subquery returns zero rows, the scalar
subquery value is NULL. If the subquery returns more than one row, the query
fails with a runtime error. When the subquery is written with SELECT AS
STRUCT , it can include multiple
columns, and the returned value is the constructed STRUCT. Selecting
multiple columns without using SELECT AS is an error. |
| ARRAY | ARRAY | Can use SELECT AS STRUCT to
build arrays of structs, and conversely, selecting multiple columns
without using SELECT AS is an error. Returns an empty ARRAY if the
subquery returns zero rows. Never returns a NULL ARRAY. |
| IN | BOOL | Occurs in an expression following the IN operator. The subquery must produce
a single column whose type is equality-compatible with the expression on the
left side of the IN operator. Returns FALSE if the subquery returns zero rows.
x IN () is equivalent to x IN (value, value, ...)
See the IN operator in
Comparison Operators
for full semantics. |
| EXISTS | BOOL | Returns TRUE if the subquery produced one or more rows. Returns FALSE if the
subquery produces zero rows. Never returns NULL. Unlike all other expression
subqueries, there are no rules about the column list. Any number of columns may
be selected and it will not affect the query result. |
Examples
The following examples of expression subqueries assume that t.int_array has
type ARRAY<INT64>.
| Type | Subquery | Result Data Type | Notes |
|---|---|---|---|
| Scalar | (SELECT COUNT(*) FROM t.int_array) |
INT64 | |
(SELECT DISTINCT i FROM t.int_array i) |
INT64, possibly runtime error | ||
(SELECT i FROM t.int_array i WHERE i=5) |
INT64, possibly runtime error | ||
(SELECT ARRAY_AGG(i) FROM t.int_array i) |
ARRAY | Uses the ARRAY_AGG aggregation function to return an ARRAY. | |
(SELECT 'xxx' a) |
STRING | ||
(SELECT 'xxx' a, 123 b) |
Error | Returns an error because there is more than one column | |
(SELECT AS STRUCT 'xxx' a, 123 b) |
STRUCT | ||
(SELECT AS STRUCT 'xxx' a) |
STRUCT | ||
| ARRAY | ARRAY(SELECT COUNT(*) FROM t.int_array) |
ARRAY of size 1 | |
ARRAY(SELECT x FROM t) |
ARRAY | ||
ARRAY(SELECT 5 a, COUNT(*) b FROM t.int_array) |
Error | Returns an error because there is more than one column | |
ARRAY(SELECT AS STRUCT 5 a, COUNT(*) b FROM t.int_array) |
ARRAY | ||
ARRAY(SELECT AS STRUCT i FROM t.int_array i) |
ARRAY | Makes an ARRAY of one-field STRUCTs | |
ARRAY(SELECT AS STRUCT 1 x, 2, 3 x) |
ARRAY | Returns an ARRAY of STRUCTs with anonymous or duplicate fields. | |
ARRAY(SELECT AS TypeName SUM(x) a, SUM(y) b, SUM(z) c from t) |
array<TypeName> | Selecting into a named type. Assume TypeName is a STRUCT type with fields a,b,c. | |
| STRUCT | (SELECT AS STRUCT 1 x, 2, 3 x) |
STRUCT | Constructs a STRUCT with anonymous or duplicate fields. |
| EXISTS | EXISTS(SELECT x,y,z FROM table WHERE y=z) |
BOOL | |
NOT EXISTS(SELECT x,y,z FROM table WHERE y=z) |
BOOL | ||
| IN | x IN (SELECT y FROM table WHERE z) |
BOOL | |
x NOT IN (SELECT y FROM table WHERE z) |
BOOL |
Debugging functions
BigQuery supports the following debugging functions.
ERROR
ERROR(error_message)
Description
Returns an error. The error_message argument is a STRING.
BigQuery treats ERROR in the same way as any expression that may
result in an error: there is no special guarantee of evaluation order.
Return Data Type
BigQuery infers the return type in context.
Examples
In the following example, the query returns an error message if the value of the row does not match one of two defined values.
SELECT
CASE
WHEN value = 'foo' THEN 'Value is foo.'
WHEN value = 'bar' THEN 'Value is bar.'
ELSE ERROR(concat('Found unexpected value: ', value))
END AS new_value
FROM (
SELECT 'foo' AS value UNION ALL
SELECT 'bar' AS value UNION ALL
SELECT 'baz' AS value);
Found unexpected value: baz
In the following example, BigQuery may evaluate the ERROR function
before or after the x > 0WHERE clause conditions and
there are no special guarantees for the ERROR function.
SELECT *
FROM (SELECT -1 AS x)
WHERE x > 0 AND ERROR('Example error');
In the next example, the WHERE clause evaluates an IF condition, which
ensures that BigQuery only evaluates the ERROR function if the
condition fails.
SELECT *
FROM (SELECT -1 AS x)
WHERE IF(x > 0, true, ERROR(FORMAT('Error: x must be positive but is %t', x)));'
Error: x must be positive but is -1