This topic is a compilation of functions, operators, and conditional expressions.
To learn more about how to call functions, function call rules,
the SAFE
prefix, and special types of arguments,
see Function calls.
OPERATORS AND CONDITIONALS
Operators in GoogleSQL
GoogleSQL for Spanner supports 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
NULL
when one of the operands isNULL
.  All operators will throw an error if the computation result overflows.
 For all floating point operations,
+/inf
andNaN
may only be returned if one of the operands is+/inf
orNaN
. In other cases, an error is returned.
Operator precedence
The following table lists all GoogleSQL 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  Field access operator  JSON STRUCT 
Field access operator  Binary 
Array subscript operator  ARRAY 
Array position. Must be used with ORDINAL —see
Array Functions
. 
Binary  
JSON subscript operator  JSON 
Field name or array position in JSON.  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  Addition  Binary 
 
All numeric types  Subtraction  Binary  
5  << 
Integer or BYTES 
Bitwise leftshift  Binary 
>> 
Integer or BYTES 
Bitwise rightshift  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 BYTES 
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 values:
STRUCT
JSON
Return type
 For
STRUCT
: SQL data type offieldname
. If a field is not found in the struct, an error is thrown.  For
JSON
:JSON
. If a field is not found in a JSON value, a SQLNULL
is returned.
Example
In the following example, the expression is t.customer
and the
field access operations are .address
and .country
. An operation is an
application of an operator (.
) to specific operands (in this case,
address
and country
, or more specifically, t.customer
and address
,
for the first operation, and t.customer.address
and country
for the
second operation).
WITH orders AS (
SELECT STRUCT(STRUCT('Yonge Street' AS street, 'Canada' AS country) AS address) AS customer
)
SELECT t.customer.address.country FROM orders AS t;
++
 country 
++
 Canada 
++
Array subscript operator
array_expression[array_subscript_specifier]
array_subscript_specifier:
position_keyword(index)
position_keyword:
{ OFFSET  SAFE_OFFSET  ORDINAL  SAFE_ORDINAL }
Description
Gets a value from an array at a specific position.
Input values:
array_expression
: The input array.position_keyword(index)
: Determines where the index for the array should start and how outofrange indexes are handled. The index is an integer that represents a specific position in the array.OFFSET(index)
: The index starts at zero. Produces an error if the index is out of range.SAFE_OFFSET(index)
: The index starts at zero. ReturnsNULL
if the index is out of range.ORDINAL(index)
: The index starts at one. Produces an error if the index is out of range.SAFE_ORDINAL(index)
: The index starts at one. ReturnsNULL
if the index is out of range.
Return type
T
where array_expression
is ARRAY<T>
.
Examples
In following query, the array subscript operator is used to return values at
specific position in item_array
. This query also shows what happens when you
reference an index (6
) in an array that is out of range. If the SAFE
prefix
is included, NULL
is returned, otherwise an error is produced.
WITH Items AS (SELECT ["coffee", "tea", "milk"] AS item_array)
SELECT
item_array,
item_array[OFFSET(0)] AS item_offset,
item_array[ORDINAL(1)] AS item_ordinal,
item_array[SAFE_OFFSET(6)] AS item_safe_offset
FROM Items
+++++
 item_array  item_offset  item_ordinal  item_safe_offset 
+++++
 [coffee, tea, milk]  coffee  coffee  NULL 
+++++
When you reference an index that is out of range in an array, and a positional
keyword that begins with SAFE
is not included, an error is produced.
For example:
WITH Items AS (SELECT ["coffee", "tea", "milk"] AS item_array)
SELECT
item_array[OFFSET(6)] AS item_offset
FROM Items
 Error. Array index 6 is out of bounds.
JSON subscript operator
json_expression[array_element_id]
json_expression[field_name]
Description
Gets a value of an array element or field in a JSON expression. Can be used to access nested data.
Input values:
JSON expression
: TheJSON
expression that contains an array element or field to return.[array_element_id]
: AnINT64
expression that represents a zerobased index in the array. If a negative value is entered, or the value is greater than or equal to the size of the array, or the JSON expression doesn't represent a JSON array, a SQLNULL
is returned.[field_name]
: ASTRING
expression that represents the name of a field in JSON. If the field name is not found, or the JSON expression is not a JSON object, a SQLNULL
is returned.
Return type
JSON
Example
In the following example:
json_value
is a JSON expression..class
is a JSON field access..students
is a JSON field access.[0]
is a JSON subscript expression with an element offset that accesses the zeroth element of an array in the JSON value.['name']
is a JSON subscript expression with a field name that accesses a field.
SELECT json_value.class.students[0]['name'] AS first_student
FROM
UNNEST(
[
JSON '{"class" : {"students" : [{"name" : "Jane"}]}}',
JSON '{"class" : {"students" : []}}',
JSON '{"class" : {"students" : [{"name" : "John"}, {"name": "Jamie"}]}}'])
AS json_value;
++
 first_student 
++
 "Jane" 
 NULL 
 "John" 
++
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  FLOAT64 

INT64  INT64  NUMERIC  FLOAT64 
NUMERIC  NUMERIC  NUMERIC  FLOAT64 
FLOAT64  FLOAT64  FLOAT64  FLOAT64 
Result types for Division:
INPUT  INT64  NUMERIC  FLOAT64 

INT64  FLOAT64  NUMERIC  FLOAT64 
NUMERIC  NUMERIC  NUMERIC  FLOAT64 
FLOAT64  FLOAT64  FLOAT64  FLOAT64 
Result types for Unary Plus:
INPUT  INT64  NUMERIC  FLOAT64 

OUTPUT  INT64  NUMERIC  FLOAT64 
Result types for Unary Minus:
INPUT  INT64  NUMERIC  FLOAT64 

OUTPUT  INT64  NUMERIC  FLOAT64 
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
GoogleSQL supports the AND
, OR
, and NOT
logical operators.
Logical operators allow only BOOL
or NULL
input
and use threevalued 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 GoogleSQL can convert the values of those types to a
common type without loss of precision, GoogleSQL will generally coerce
them to that common type for the comparison; GoogleSQL will generally
coerce literals to the type of nonliterals, where
present. Comparable data types are defined in
Data Types.
Structs support only these comparison operators: equal
(=
), not equal (!=
and <>
), and IN
.
The comparison operators in this section cannot be used to compare
JSON
GoogleSQL literals with other JSON
GoogleSQL literals.
If you need to compare values inside of JSON
, convert the values to
SQL values first. For more information, see JSON
functions.
The following rules apply when comparing these data types:
FLOAT64
: All comparisons withNaN
returnFALSE
, except for!=
and<>
, which returnTRUE
.BOOL
:FALSE
is less thanTRUE
.STRING
: Strings are compared codepointbycodepoint, 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 aNULL
input 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 
LIKE 
X [NOT] LIKE Y 
See the `LIKE` operator for details. 
IN 
Multiple  See the `IN` operator for details. 
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 returnsNULL
.  If any non
NULL
field values are not equal, the comparison returnsFALSE
.
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 
EXISTS
operator
EXISTS ( subquery )
Description
Returns TRUE
if the subquery produces one or more rows. Returns FALSE
if
the subquery produces zero rows. Never returns NULL
. To learn more about
how you can use a subquery with EXISTS
,
see EXISTS
subqueries.
Examples
In this example, the EXISTS
operator returns FALSE
because there are no
rows in Words
where the direction is south
:
WITH Words AS (
SELECT 'Intend' as value, 'east' as direction UNION ALL
SELECT 'Secure', 'north' UNION ALL
SELECT 'Clarity', 'west'
)
SELECT EXISTS ( SELECT value FROM Words WHERE direction = 'south' ) as result;
++
 result 
++
 FALSE 
++
IN
operator
The IN
operator supports the following syntax:
search_value [NOT] IN value_set
value_set:
{
(expression[, ...])
 (subquery)
 UNNEST(array_expression)
}
Description
Checks for an equal value in a set of values.
Semantic rules apply, but in general, IN
returns TRUE
if an equal value is found, FALSE
if an equal value is excluded, otherwise
NULL
. NOT IN
returns FALSE
if an equal value is found, TRUE
if an
equal value is excluded, otherwise NULL
.
search_value
: The expression that is compared to a set of values.value_set
: One or more values to compare to a search value.(expression[, ...])
: A list of expressions.(subquery)
: A subquery that returns a single column. The values in that column are the set of values. If no rows are produced, the set of values is empty.UNNEST(array_expression)
: An UNNEST operator that returns a column of values from an array expression. This is equivalent to:IN (SELECT element FROM UNNEST(array_expression) AS element)
This operator generally supports collation,
however, [NOT] IN UNNEST
does not support collation.
Semantic rules
When using the IN
operator, the following semantics apply in this order:
 Returns
FALSE
ifvalue_set
is empty.  Returns
NULL
ifsearch_value
isNULL
.  Returns
TRUE
ifvalue_set
contains a value equal tosearch_value
.  Returns
NULL
ifvalue_set
contains aNULL
.  Returns
FALSE
.
When using the NOT IN
operator, the following semantics apply in this order:
 Returns
TRUE
ifvalue_set
is empty.  Returns
NULL
ifsearch_value
isNULL
.  Returns
FALSE
ifvalue_set
contains a value equal tosearch_value
.  Returns
NULL
ifvalue_set
contains aNULL
.  Returns
TRUE
.
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)
See the Arrays topic for more information on how to use this syntax.
IN
can be used with multipart 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 topic for more information.
Return Data Type
BOOL
Examples
You can use these WITH
clauses to emulate temporary tables for
Words
and Items
in the following examples:
WITH Words AS (
SELECT 'Intend' as value UNION ALL
SELECT 'Secure' UNION ALL
SELECT 'Clarity' UNION ALL
SELECT 'Peace' UNION ALL
SELECT 'Intend'
)
SELECT * FROM Words;
++
 value 
++
 Intend 
 Secure 
 Clarity 
 Peace 
 Intend 
++
WITH
Items AS (
SELECT STRUCT('blue' AS color, 'round' AS shape) AS info UNION ALL
SELECT STRUCT('blue', 'square') UNION ALL
SELECT STRUCT('red', 'round')
)
SELECT * FROM Items;
++
 info 
++
 {blue color, round shape} 
 {blue color, square shape} 
 {red color, round shape} 
++
Example with IN
and an expression:
SELECT * FROM Words WHERE value IN ('Intend', 'Secure');
++
 value 
++
 Intend 
 Secure 
 Intend 
++
Example with NOT IN
and an expression:
SELECT * FROM Words WHERE value NOT IN ('Intend');
++
 value 
++
 Secure 
 Clarity 
 Peace 
++
Example with IN
, a scalar subquery, and an expression:
SELECT * FROM Words WHERE value IN ((SELECT 'Intend'), 'Clarity');
++
 value 
++
 Intend 
 Clarity 
 Intend 
++
Example with IN
and an UNNEST
operation:
SELECT * FROM Words WHERE value IN UNNEST(['Secure', 'Clarity']);
++
 value 
++
 Secure 
 Clarity 
++
Example with IN
and a struct:
SELECT
(SELECT AS STRUCT Items.info) as item
FROM
Items
WHERE (info.shape, info.color) IN (('round', 'blue'));
++
 item 
++
 { {blue color, round shape} info } 
++
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 TRUE 
BOOL 
BOOL 
Evaluates to TRUE if X evaluates to
TRUE .
Otherwise, evaluates to FALSE .

X IS NOT TRUE 
BOOL 
BOOL 
Evaluates to FALSE if X evaluates to
TRUE .
Otherwise, evaluates to TRUE .

X IS FALSE 
BOOL 
BOOL 
Evaluates to TRUE if X evaluates to
FALSE .
Otherwise, evaluates to FALSE .

X IS NOT FALSE 
BOOL 
BOOL 
Evaluates to FALSE if X evaluates to
FALSE .
Otherwise, evaluates to TRUE .

X IS NULL 
Any value type  BOOL 
Evaluates to TRUE if X evaluates to
NULL .
Otherwise evaluates to FALSE .

X IS NOT NULL 
Any value type  BOOL 
Evaluates to FALSE if X evaluates to
NULL .
Otherwise evaluates to TRUE .

X IS UNKNOWN 
BOOL 
BOOL 
Evaluates to TRUE if X evaluates to
NULL .
Otherwise evaluates to FALSE .

X IS NOT UNKNOWN 
BOOL 
BOOL 
Evaluates to FALSE if X evaluates to
NULL .
Otherwise, evaluates to TRUE .

LIKE
operator
expression_1 IS [NOT] LIKE expression_2
Description
IS LIKE
returns TRUE
if the string in the first operand expression_1
matches a pattern specified by the second operand expression_2
,
otherwise returns FALSE
.
IS NOT LIKE
returns TRUE
if the string in the first operand expression_1
does not match a pattern specified by the second operand expression_2
,
otherwise returns FALSE
.
Expressions can contain these characters:
 A percent sign (
%
) matches any number of characters or bytes.  An underscore (
_
) matches a single character or byte.  You can escape
\
,_
, or%
using two backslashes. For example,\\%
. If you are using raw strings, only a single backslash is required. For example,r'\%'
.
Return type
BOOL
Examples
The following examples illustrate how you can check to see if the string in the first operand matches a pattern specified by the second operand.
 Returns TRUE
SELECT 'apple' LIKE 'a%';
 Returns FALSE
SELECT '%a' LIKE 'apple';
 Returns FALSE
SELECT 'apple' NOT LIKE 'a%';
 Returns TRUE
SELECT '%a' NOT LIKE 'apple';
 Produces an error
SELECT NULL LIKE 'a%';
 Produces an error
SELECT 'apple' LIKE NULL;
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 in GoogleSQL
GoogleSQL for Spanner supports conditional expressions. Conditional expressions impose constraints on the evaluation order of their inputs. In essence, they are evaluated left to right, with shortcircuiting, and only evaluate the output value that was chosen. In contrast, all inputs to regular functions are evaluated before calling the function. Shortcircuiting 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 evaluates to TRUE
. The remaining WHEN
clauses and else_result
aren't evaluated.
If the expr = expr_to_match
comparison evaluates to FALSE
or NULL
for all
WHEN
clauses, returns the evaluation of else_result
if present; if
else_result
isn't present, then returns NULL
.
Consistent with equality comparisons elsewhere, if both
expr
and expr_to_match
are NULL
, then expr = expr_to_match
evaluates to
NULL
, which returns else_result
. If a CASE statement needs to distinguish a
NULL
value, then the alternate CASE syntax should be used.
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 evaluates to TRUE
; any remaining WHEN
clauses and else_result
aren't evaluated.
If all conditions evaluate to FALSE
or NULL
, returns evaluation of
else_result
if present; if else_result
isn't present, then returns NULL
.
For additional rules on how values are evaluated, see the threevalued logic table in Logical operators.
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 B = 6 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 nonNULL
expression. The remaining
expressions aren't 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
evaluates to TRUE
, returns true_result
, else returns the
evaluation for else_result
. else_result
isn't evaluated if expr
evaluates
to TRUE
. true_result
isn't evaluated if expr
evaluates to 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
evaluates to NULL
, returns null_result
. Otherwise, returns
expr
. If expr
doesn't evaluate to NULL
, null_result
isn't 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
evaluates to 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 
++
FUNCTIONS
Aggregate functions in GoogleSQL
GoogleSQL for Spanner supports the following general aggregate functions. To learn about the syntax for aggregate function calls, see Aggregate function calls.
ANY_VALUE
ANY_VALUE(
expression
[ HAVING { MAX  MIN } expression2 ]
)
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 IGNORE NULLS
is specified;
rows for which expression
is NULL
are not considered and won't be
selected.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
Any
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 
++
WITH
Store AS (
SELECT 20 AS sold, "apples" AS fruit
UNION ALL
SELECT 30 AS sold, "pears" AS fruit
UNION ALL
SELECT 30 AS sold, "bananas" AS fruit
UNION ALL
SELECT 10 AS sold, "oranges" AS fruit
)
SELECT ANY_VALUE(fruit HAVING MAX sold) AS a_highest_selling_fruit FROM Store;
++
 a_highest_selling_fruit 
++
 pears 
++
WITH
Store AS (
SELECT 20 AS sold, "apples" AS fruit
UNION ALL
SELECT 30 AS sold, "pears" AS fruit
UNION ALL
SELECT 30 AS sold, "bananas" AS fruit
UNION ALL
SELECT 10 AS sold, "oranges" AS fruit
)
SELECT ANY_VALUE(fruit HAVING MIN sold) AS a_lowest_selling_fruit FROM Store;
++
 a_lowest_selling_fruit 
++
 oranges 
++
ARRAY_AGG
ARRAY_AGG(
[ DISTINCT ]
expression
[ { IGNORE  RESPECT } NULLS ]
[ HAVING { MAX  MIN } expression2 ]
)
Description
Returns an ARRAY of expression
values.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
All data types except ARRAY.
Returned Data Types
ARRAY
If there are zero input rows, this function returns NULL
.
Examples
SELECT 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 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 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] 
++
WITH vals AS
(
SELECT 1 x, 'a' y UNION ALL
SELECT 1 x, 'b' y UNION ALL
SELECT 2 x, 'a' y UNION ALL
SELECT 2 x, 'c' y
)
SELECT x, ARRAY_AGG(y) as array_agg
FROM vals
GROUP BY x;
++
 x  array_agg 
++
 1  [a, b] 
 2  [a, c] 
++
ARRAY_CONCAT_AGG
ARRAY_CONCAT_AGG(
expression
[ HAVING { MAX  MIN } expression2 ]
)
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
nonNULL
input arrays. Returns NULL
if there are zero input rows or
expression
evaluates to NULL
for all rows.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
ARRAY
Returned Data Types
ARRAY
Examples
SELECT 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] 
++
AVG
AVG(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
Returns the average of nonNULL
values in an aggregated group.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Caveats:
 If the aggregated group is empty or the argument is
NULL
for all rows in the group, returnsNULL
.  If the argument is
NaN
for any row in the group, returnsNaN
.  If the argument is
[+]Infinity
for any row in the group, returns either[+]Infinity
orNaN
.  If there is numeric overflow, produces an error.
 If a floatingpoint type is returned, the result is nondeterministic, which means you might receive a different result each time you use this function.
Supported Argument Types
 Any numeric input type
Returned Data Types
INPUT  INT64  NUMERIC  FLOAT64 

OUTPUT  FLOAT64  NUMERIC  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 
++
BIT_AND
BIT_AND(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
Performs a bitwise AND operation on expression
and returns the result.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
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(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
Performs a bitwise OR operation on expression
and returns the result.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
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
[ HAVING { MAX  MIN } expression2 ]
)
Description
Performs a bitwise XOR operation on expression
and returns the result.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
 INT64
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(*)
2.
COUNT(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
 Returns the number of rows in the input.
 Returns the number of rows with
expression
evaluated to any value other thanNULL
.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
expression
can be any data type. If
DISTINCT
is present, expression
can only be a data type that is
groupable.
Return Data Types
INT64
Examples
You can use the COUNT
function to return the number of rows in a table or the
number of distinct values of an expression. For example:
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 COUNT(*) AS count_star, COUNT(x) AS count_x
FROM UNNEST([1, 4, NULL, 4, 5]) AS x;
+++
 count_star  count_x 
+++
 5  4 
+++
If you want to count the number of distinct values of an expression for which a certain condition is satisfied, this is one recipe that you can use:
COUNT(DISTINCT IF(condition, expression, NULL))
Here, IF
will return the value of expression
if condition
is TRUE
, or
NULL
otherwise. The surrounding COUNT(DISTINCT ...)
will ignore the NULL
values, so it will count only the distinct values of expression
for which
condition
is TRUE
.
For example, to count the number of distinct positive values of x
:
SELECT COUNT(DISTINCT IF(x > 0, x, NULL)) AS distinct_positive
FROM UNNEST([1, 2, 4, 1, 5, 4, 1, 3, 6, 1]) AS x;
++
 distinct_positive 
++
 3 
++
Or to count the number of distinct dates on which a certain kind of event occurred:
WITH Events AS (
SELECT DATE '20210101' AS event_date, 'SUCCESS' AS event_type
UNION ALL
SELECT DATE '20210102' AS event_date, 'SUCCESS' AS event_type
UNION ALL
SELECT DATE '20210102' AS event_date, 'FAILURE' AS event_type
UNION ALL
SELECT DATE '20210103' AS event_date, 'SUCCESS' AS event_type
UNION ALL
SELECT DATE '20210104' AS event_date, 'FAILURE' AS event_type
UNION ALL
SELECT DATE '20210104' AS event_date, 'FAILURE' AS event_type
)
SELECT
COUNT(DISTINCT IF(event_type = 'FAILURE', event_date, NULL))
AS distinct_dates_with_failures
FROM Events;
++
 distinct_dates_with_failures 
++
 2 
++
COUNTIF
COUNTIF(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
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.
Since expression
must be a BOOL
, the form
COUNTIF(DISTINCT ...)
is generally not useful: there is only one distinct
value of TRUE
. So COUNTIF(DISTINCT ...)
will return 1 if expression
evaluates to TRUE
for one or more input rows, or 0 otherwise.
Usually when someone wants to combine COUNTIF
and DISTINCT
, they
want to count the number of distinct values of an expression for which a certain
condition is satisfied. One recipe to achieve this is the following:
COUNT(DISTINCT IF(condition, expression, NULL))
Note that this uses COUNT
, not COUNTIF
; the IF
part has been moved inside.
To learn more, see the examples for COUNT
.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
BOOL
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 
+++
LOGICAL_AND
LOGICAL_AND(
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
Returns the logical AND of all nonNULL
expressions. Returns NULL
if there
are zero input rows or expression
evaluates to NULL
for all rows.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
BOOL
Return Data Types
BOOL
Examples
LOGICAL_AND
returns FALSE
because not all of the values in the array are
less than 3.
SELECT LOGICAL_AND(x < 3) AS logical_and FROM UNNEST([1, 2, 4]) AS x;
++
 logical_and 
++
 FALSE 
++
LOGICAL_OR
LOGICAL_OR(
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
Returns the logical OR of all nonNULL
expressions. Returns NULL
if there
are zero input rows or expression
evaluates to NULL
for all rows.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
BOOL
Return Data Types
BOOL
Examples
LOGICAL_OR
returns TRUE
because at least one of the values in the array is
less than 3.
SELECT LOGICAL_OR(x < 3) AS logical_or FROM UNNEST([1, 2, 4]) AS x;
++
 logical_or 
++
 TRUE 
++
MAX
MAX(
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
Returns the maximum nonNULL
value in an aggregated group.
Caveats:
 If the aggregated group is empty or the argument is
NULL
for all rows in the group, returnsNULL
.  If the argument is
NaN
for any row in the group, returnsNaN
.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
Any orderable data type except for ARRAY
.
Return Data Types
The data type of the input values.
Examples
SELECT MAX(x) AS max
FROM UNNEST([8, 37, 55, 4]) AS x;
++
 max 
++
 55 
++
MIN
MIN(
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
Returns the minimum nonNULL
value in an aggregated group.
Caveats:
 If the aggregated group is empty or the argument is
NULL
for all rows in the group, returnsNULL
.  If the argument is
NaN
for any row in the group, returnsNaN
.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
Any orderable data type except for ARRAY
.
Return Data Types
The data type of the input values.
Examples
SELECT MIN(x) AS min
FROM UNNEST([8, 37, 4, 55]) AS x;
++
 min 
++
 4 
++
STRING_AGG
STRING_AGG(
[ DISTINCT ]
expression [, delimiter]
[ HAVING { MAX  MIN } expression2 ]
)
Description
Returns a value (either STRING
or BYTES
) obtained by concatenating
nonNULL
values. Returns NULL
if there are zero input rows or expression
evaluates to NULL
for all rows.
If a delimiter
is specified, concatenated values are separated by that
delimiter; otherwise, a comma is used as a delimiter.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Supported Argument Types
Either STRING
or BYTES
.
Return Data Types
Either STRING
or 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 
++
SUM
SUM(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
Returns the sum of nonNULL
values in an aggregated group.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Caveats:
 If the aggregated group is empty or the argument is
NULL
for all rows in the group, returnsNULL
.  If the argument is
NaN
for any row in the group, returnsNaN
.  If the argument is
[+]Infinity
for any row in the group, returns either[+]Infinity
orNaN
.  If there is numeric overflow, produces an error.
 If a floatingpoint type is returned, the result is nondeterministic, which means you might receive a different result each time you use this function.
Supported Argument Types
 Any supported numeric data type
Return Data Types
INPUT  INT64  NUMERIC  FLOAT64 

OUTPUT  INT64  NUMERIC  FLOAT64 
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 SUM(x) AS sum
FROM UNNEST([]) AS x;
++
 sum 
++
 NULL 
++
Statistical aggregate functions in GoogleSQL
GoogleSQL for Spanner supports statistical aggregate functions. To learn about the syntax for aggregate function calls, see Aggregate function calls.
STDDEV_SAMP
STDDEV_SAMP(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
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
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 nonNULL inputs, this function returns NULL.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Return Data Type
FLOAT64
STDDEV
STDDEV(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
An alias of STDDEV_SAMP.
VAR_SAMP
VAR_SAMP(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
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
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 nonNULL inputs, this function returns NULL.
To learn more about the optional arguments in this function and how to use them, see Aggregate function calls.
Return Data Type
FLOAT64
VARIANCE
VARIANCE(
[ DISTINCT ]
expression
[ HAVING { MAX  MIN } expression2 ]
)
Description
An alias of VAR_SAMP.
Conversion functions in GoogleSQL
GoogleSQL for Spanner supports 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
CAST(expression AS typename)
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 GoogleSQL 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 UTF8 results in a
runtime error.
Examples
The following query results in "true"
if x
is 1
, "false"
for any other
nonNULL
value, and NULL
if x
is NULL
.
CAST(x=1 AS STRING)
CAST AS ARRAY
CAST(expression AS ARRAY<element_type>)
Description
GoogleSQL 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 BOOL
CAST(expression AS BOOL)
Description
GoogleSQL supports casting to BOOL
. The
expression
parameter can represent an expression for these data types:
INT64
BOOL
STRING
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 a boolean.A string is caseinsensitive when converting to a boolean. 
CAST AS BYTES
CAST(expression AS BYTES)
Description
GoogleSQL supports casting to BYTES
. The
expression
parameter can represent an expression for these data types:
BYTES
STRING
Conversion rules
From  To  Rule(s) when casting x 

STRING 
BYTES 
Strings are cast to bytes using UTF8 encoding. For example, the string "©", when cast to bytes, would become a 2byte sequence with the hex values C2 and A9. 
CAST AS DATE
CAST(expression AS DATE)
Description
GoogleSQL supports casting to DATE
. The expression
parameter can represent an expression for these data types:
STRING
TIMESTAMP
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 FLOAT64
CAST(expression AS FLOAT64)
Description
GoogleSQL supports casting to floating point types.
The expression
parameter can represent an expression for these data types:
INT64
FLOAT64
NUMERIC
STRING
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.

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 caseinsensitive.

CAST AS INT64
CAST(expression AS INT64)
Description
GoogleSQL supports casting to integer types.
The expression
parameter can represent an expression for these data types:
INT64
FLOAT64
NUMERIC
BOOL
STRING
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
GoogleSQL supports casting to NUMERIC
. The
expression
parameter can represent an expression for these data types:
INT64
FLOAT64
NUMERIC
STRING
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 returns 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)
Description
GoogleSQL supports casting to STRING
. The
expression
parameter can represent an expression for these data types:
INT64
FLOAT64
NUMERIC
BOOL
BYTES
DATE
TIMESTAMP
STRING
Conversion rules
From  To  Rule(s) when casting x 

FLOAT64  STRING 
Returns an approximate string representation. A returned
NaN or 0 will not be signed. 
BOOL 
STRING 
Returns "true" if x is TRUE ,
"false" otherwise. 
BYTES 
STRING 
Returns x interpreted as a UTF8 string.For example, the bytes literal b'\xc2\xa9' , when cast to a string,
is interpreted as UTF8 and becomes the unicode character "©".An error occurs if x is not valid UTF8. 
DATE 
STRING 
Casting from a date type to a string is independent of time zone and is
of the form YYYYMMDD .

TIMESTAMP 
STRING 
When casting from timestamp types to string, the timestamp is interpreted using the default time zone, America/Los_Angeles. 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 
++
 20210309 
++
CAST AS STRUCT
CAST(expression AS STRUCT)
Description
GoogleSQL 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 TIMESTAMP
CAST(expression AS TIMESTAMP)
Description
GoogleSQL supports casting to TIMESTAMP
. The
expression
parameter can represent an expression for these data types:
STRING
TIMESTAMP
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,
America/Los_Angeles, 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,
America/Los_Angeles.

Examples
The following example casts a stringformatted timestamp as a timestamp:
SELECT CAST("20200602 17:00:53.110+00:00" AS TIMESTAMP) AS as_timestamp
 Results depend upon where this query was executed.
++
 as_timestamp 
++
 20200603T00:00:53.11Z 
++
SAFE_CAST
SAFE_CAST(expression AS typename)
Description
When using CAST
, a query can fail if GoogleSQL 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 UTF8 characters
are replaced with the unicode replacement character, U+FFFD
.
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 
FROM_BASE32  STRING  BYTEs 
FROM_BASE64  STRING  BYTES 
FROM_HEX  STRING  BYTES 
PARSE_DATE  STRING  DATE 
PARSE_JSON  STRING  JSON 
PARSE_TIMESTAMP  STRING  TIMESTAMP 
SAFE_CONVERT_BYTES_TO_STRING  BYTES  STRING 
STRING  TIMESTAMP  STRING 
TIMESTAMP  Various data types  TIMESTAMP 
TO_BASE32  BYTES  STRING 
TO_BASE64  BYTES  STRING 
TO_HEX  BYTES  STRING 
TO_JSON  All data types  JSON 
TO_JSON_STRING  JSON  STRING 
Machine learning functions in GoogleSQL
GoogleSQL for Spanner supports the following machine learning (ML) functions.
ML.PREDICT
ML.PREDICT(input_model, input_relation)
input_model:
MODEL model_name
input_relation:
{ input_table  input_subquery }
input_table:
TABLE table_name
Description
ML.PREDICT
is a tablevalued function that helps to access registered
machine learning (ML) models and use them to generate ML predictions.
This function applies ML computations defined by a model to each row of an
input relation, and then, it returns the results of the predictions.
Supported Argument Types
input_model
: The model to use for predictions. Replacemodel_name
with the name of the model. To create a model, see CREATE_MODEL.input_relation
: A table or subquery upon which to apply ML computations. The set of columns of the input relation must include all input columns of the input model; otherwise, the input won't have enough data to generate predictions and the query won't compile. Additionally, the set can also include arbitrary passthrough columns that will be included in the output. The order of the columns in the input relation doesn't matter. The columns of the input relation and model must be coercible.input_table
: The table containing the input data for predictions, for example, a set of features. Replacetable_name
with the name of the table.input_subquery
: The subquery that's used to generate the prediction input data.
Return Type
A table with the following columns:
 Model outputs
 Passthrough columns from the input relation
Examples
The examples in this section reference a model called DiamondAppraise
and
an input table called Diamonds
with the following columns:
DiamondAppraise
model:Input columns Output columns value FLOAT64
value FLOAT64
carat FLOAT64
lower_bound FLOAT64
cut STRING
upper_bound FLOAT64
color STRING(1)
Diamonds
table:Columns Id INT64
Carat FLOAT64
Cut STRING
Color STRING
The following query predicts the value of a diamond based on the diamond's carat, cut, and color.
SELECT id, color, value
FROM ML.PREDICT(MODEL DiamondAppraise, TABLE Diamonds);
++++
 id  color  value 
++++
 1  I  280 
 2  G  447 
++++
You can use ML.PREDICT
in any DQL/DML statements, such as INSERT
or
UPDATE
. For example:
INSERT INTO AppraisedDiamond (id, color, carat, value)
SELECT
1 AS id,
color,
carat,
value
FROM
ML.PREDICT(MODEL DiamondAppraise,
(
SELECT
@carat AS carat,
@cut AS cut,
@color AS color
));
Mathematical functions in GoogleSQL
GoogleSQL for Spanner supports mathematical functions. All mathematical functions have the following behaviors:
 They return
NULL
if any of the input parameters isNULL
.  They return
NaN
if 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  FLOAT64 

OUTPUT  INT64  NUMERIC  FLOAT64 
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].
If X is NUMERIC
then, the output is FLOAT64
.
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.
If X is NUMERIC
then, the output is FLOAT64
.
X  ACOSH(X) 

+inf 
+inf 
inf 
NaN 
NaN 
NaN 
X < 1  Error 
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].
If X is NUMERIC
then, the output is FLOAT64
.
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.
If X is NUMERIC
then, the output is FLOAT64
.
X  ASINH(X) 

+inf 
+inf 
inf 
inf 
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.
If X is NUMERIC
then, the output is FLOAT64
.
X  ATAN(X) 

+inf 
π/2 
inf 
π/2 
NaN 
NaN 
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 [π,π].
If Y is NUMERIC
then, the output is FLOAT64
.
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 
ATANH
ATANH(X)
Description
Computes the inverse hyperbolic tangent of X. Generates an error if X is outside of the range (1, 1).
If X is NUMERIC
then, the output is FLOAT64
.
X  ATANH(X) 

+inf 
NaN 
inf 
NaN 
NaN 
NaN 
X < 1  Error 
X > 1  Error 
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  FLOAT64 

OUTPUT  FLOAT64  NUMERIC  FLOAT64 
CEILING
CEILING(X)
Description
Synonym of CEIL(X)
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.
If X is NUMERIC
then, the output is FLOAT64
.
X  COSH(X) 

+inf 
+inf 
inf 
+inf 
NaN 
NaN 
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. If
both inputs are NUMERIC
and the result is overflow,
then it returns a numeric overflow
error.
X  Y  DIV(X, Y) 

20  4  5 
12  7  1 
20  3  6 
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 

INT64  INT64  NUMERIC 
NUMERIC  NUMERIC  NUMERIC 
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  FLOAT64 

OUTPUT  FLOAT64  NUMERIC  FLOAT64 
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  FLOAT64 

OUTPUT  FLOAT64  NUMERIC  FLOAT64 
GREATEST
GREATEST(X1,...,XN)
Description
Returns the greatest value among X1,...,XN
. If any argument is NULL
, returns
NULL
. Otherwise, in the case of floatingpoint arguments, if any argument is
NaN
, returns NaN
. In all other cases, returns the value among X1,...,XN
that has the greatest value according to the ordering used by the ORDER BY
clause. The arguments X1, ..., XN
must be coercible to a common supertype, and
the supertype must support ordering.
X1,...,XN  GREATEST(X1,...,XN) 

3,5,1  5 
Return Data Types
Data type of the input values.
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 
IS_INF
IS_INF(X)
Description
Returns TRUE
if the value is positive or negative infinity.
Returns FALSE
for NUMERIC
inputs since NUMERIC
cannot be INF
.
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.
Returns FALSE
for NUMERIC
inputs since
NUMERIC
cannot be NaN
.
X  IS_NAN(X) 

NaN 
TRUE 
25  FALSE 
LEAST
LEAST(X1,...,XN)
Description
Returns the least value among X1,...,XN
. If any argument is NULL
, returns
NULL
. Otherwise, in the case of floatingpoint arguments, if any argument is
NaN
, returns NaN
. In all other cases, returns the value among X1,...,XN
that has the least value according to the ordering used by the ORDER BY
clause. The arguments X1, ..., XN
must be coercible to a common supertype, and
the supertype must support ordering.
X1,...,XN  LEAST(X1,...,XN) 

3,5,1  1 
Return Data Types
Data type of the input values.
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  FLOAT64 

OUTPUT  FLOAT64  NUMERIC  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  FLOAT64 

INT64  FLOAT64  NUMERIC  FLOAT64 
NUMERIC  NUMERIC  NUMERIC  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 
+inf 
X <= 0  Error 
Return Data Type
INPUT  INT64  NUMERIC  FLOAT64 

OUTPUT  FLOAT64  NUMERIC  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 

INT64  INT64  NUMERIC 
NUMERIC  NUMERIC  NUMERIC 
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  Noninteger  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  FLOAT64 

INT64  FLOAT64  NUMERIC  FLOAT64 
NUMERIC  NUMERIC  NUMERIC  FLOAT64 
FLOAT64  FLOAT64  FLOAT64  FLOAT64 
POWER
POWER(X, Y)
Description
Synonym of POW(X, Y)
.
ROUND
ROUND(X [, N])
Description
If only X is present, rounds X to the nearest integer. If N is present, rounds X to N decimal places after the decimal point. If N is negative, rounds off digits to the left of the decimal point. Rounds halfway cases away from zero. Generates an error if overflow occurs.
Expression  Return Value 

ROUND(2.0) 
2.0 
ROUND(2.3) 
2.0 
ROUND(2.8) 
3.0 
ROUND(2.5) 
3.0 
ROUND(2.3) 
2.0 
ROUND(2.8) 
3.0 
ROUND(2.5) 
3.0 
ROUND(0) 
0 
ROUND(+inf) 
+inf 
ROUND(inf) 
inf 
ROUND(NaN) 
NaN 
ROUND(123.7, 1) 
120.0 
ROUND(1.235, 2) 
1.24 
Return Data Type
INPUT  INT64  NUMERIC  FLOAT64 

OUTPUT  FLOAT64  NUMERIC  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  FLOAT64 

INT64  INT64  NUMERIC  FLOAT64 
NUMERIC  NUMERIC  NUMERIC  FLOAT64 
FLOAT64  FLOAT64  FLOAT64  FLOAT64 
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  FLOAT64 

INT64  FLOAT64  NUMERIC  FLOAT64 
NUMERIC  NUMERIC  NUMERIC  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  FLOAT64 

INT64  INT64  NUMERIC  FLOAT64 
NUMERIC  NUMERIC  NUMERIC  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  FLOAT64 

OUTPUT  INT64  NUMERIC  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  FLOAT64 

INT64  INT64  NUMERIC  FLOAT64 
NUMERIC  NUMERIC  NUMERIC  FLOAT64 
FLOAT64  FLOAT64  FLOAT64  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  FLOAT64 

OUTPUT  INT64  NUMERIC  FLOAT64 
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.
If X is NUMERIC
then, the output is FLOAT64
.
X  SINH(X) 

+inf 
+inf 
inf 
inf 
NaN 
NaN 
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  FLOAT64 

OUTPUT  FLOAT64  NUMERIC  FLOAT64 
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.
If X is NUMERIC
then, the output is FLOAT64
.
X  TANH(X) 

+inf 
1.0 
inf 
1.0 
NaN 
NaN 
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  FLOAT64 

OUTPUT  FLOAT64  NUMERIC  FLOAT64 
Hash functions in GoogleSQL
GoogleSQL for Spanner supports the following hash functions.
FARM_FINGERPRINT
FARM_FINGERPRINT(value)
Description
Computes the fingerprint of the STRING
or BYTES
input using the
Fingerprint64
function from the
opensource 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 
+++++
SHA1
SHA1(input)
Description
Computes the hash of the input using the
SHA1 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 base64encoded string.
++
 sha1 
++
 Ck1VqNd45QIvq3AZd8XYQLvEhtA= 
++
SHA256
SHA256(input)
Description
Computes the hash of the input using the
SHA256 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
SHA512 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 in GoogleSQL
GoogleSQL for Spanner supports string functions.
These string functions work on two different values:
STRING
and BYTES
data types. STRING
values must be wellformed UTF8.
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 position. When working on STRING
types, the
returned positions refer to character positions.
All string comparisons are done bytebybyte, without regard to Unicode canonical equivalence.
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 
+++
CODE_POINTS_TO_BYTES
CODE_POINTS_TO_BYTES(ascii_code_points)
Description
Takes an array of extended ASCII
code points
as ARRAY<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 base64encoded string.
 In BYTES format, b'AbCd' is the result.
++
 bytes 
++
 QWJDZA== 
++
The following example uses a rotateby13 places (ROT13) algorithm to encode a string.
SELECT CODE_POINTS_TO_BYTES(ARRAY(
(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(ARRAY[code]) AS chr
FROM UNNEST(TO_CODE_POINTS(input)) AS code WITH OFFSET
ORDER BY OFFSET)
))) AS encoded_string
FROM UNNEST(ARRAY['Test String!']) AS input;
 Note that the result of CODE_POINTS_TO_BYTES is of type BYTES, displayed as a base64encoded string.
 In BYTES format, b'Grfg Fgevat!' is the result.
++
 encoded_string 
++
 R3JmZyBGZ2V2YXQh 
++
CODE_POINTS_TO_STRING
CODE_POINTS_TO_STRING(unicode_code_points)
Description
Takes an array of Unicode code points
as ARRAY<INT64>
and returns a 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 STRING
or BYTE
values into a single result.
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 
++
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 
++
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 onetoone mapping, except when the*
specifier is present. For example,%.*i
maps 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 GoogleSQL 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 '20150901')  20150901 
TIMESTAMP  FORMAT('%t', timestamp '20150901 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
typespecific format functions, such as FORMAT_DATE()
or FORMAT_TIMESTAMP()
.
For example:
SELECT FORMAT('date: %s!', FORMAT_DATE('%B %d, %Y', date '20150102'));
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 subspecifiers:
Additional information about format specifiers:
 %g and %G behavior
 %p and %P 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 nonfinite values  392.650000 inf nan 
NUMERIC FLOAT64 
F 
Decimal notation, in [](integer part).(fractional part) for finite values, and in uppercase for nonfinite values  392.650000 INF NAN 
NUMERIC FLOAT64 
e 
Scientific notation (mantissa/exponent), lowercase  3.926500e+02 inf nan 
NUMERIC FLOAT64 
E 
Scientific notation (mantissa/exponent), uppercase  3.926500E+02 INF NAN 
NUMERIC 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 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 FLOAT64 
p 
Produces a oneline printable string representing JSON. See %p and %P behavior. 
{"month":10,"year":2019} 
JSON 
P 
Produces a multiline printable string representing JSON. See %p and %P behavior. 
{ "month": 10, "year": 2019 } 
JSON 
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 GoogleSQL 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 subspecifiers identified above in the specifier prototype.
These subspecifiers must comply with the following specifications.
Flags
Flags  Description 
 
Leftjustify within the given field width; Right justification is the default (see width subspecifier) 
+ 
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 
Leftpads the number with zeroes (0) instead of spaces when padding is specified (see width subspecifier) 
' 
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.
%p and %P behavior
The %p
format specifier produces a oneline printable string. The %P
format specifier produces a multiline printable string. You can use these
format specifiers with the following data types:
Type  %p  %P 
JSON 
JSON input: JSON ' { "month": 10, "year": 2019 } ' Produces a oneline printable string representing JSON: {"month":10,"year":2019} 
JSON input: JSON ' { "month": 10, "year": 2019 } ' Produces a multiline printable string representing JSON: { "month": 10, "year": 2019 } 
%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 nonfinite 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 inf NaN

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  20110203  DATE "20110203" 
TIMESTAMP  20110203 04:05:06+00  TIMESTAMP "20110203 04:05:06+00" 
ARRAY  [value, value, ...] where values are formatted with %t 
[value, value, ...] where values are formatted with %T 
JSON 
oneline printable string representing JSON.{"name":"apple","stock":3} 
oneline printable string representing a JSON literal.JSON '{"name":"apple","stock":3}' 
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 STRING
s 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%t00', NULL_expression);
Returns
00NULL00
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
GoogleSQL casts these values to STRING
. For %T
,
GoogleSQL returns quoted strings for
FLOAT64
values that don't have nonstring literal
representations.
FROM_BASE32
FROM_BASE32(string_expr)
Description
Converts the base32encoded input string_expr
into BYTES
format. To convert
BYTES
to a base32encoded 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 base64encoded string.
++
 byte_data 
++
 YWJjZGX/ 
++
FROM_BASE64
FROM_BASE64(string_expr)
Description
Converts the base64encoded input string_expr
into
BYTES
format. To convert
BYTES
to a base64encoded STRING
,
use [TO_BASE64][stringlinktobase64].
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 [AZaz09+/=]
.
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 base64encoded 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
urlsafe and filenamesafe 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 base64encoded string.
++
 binary 
++
 /+A= 
++
FROM_HEX
FROM_HEX(string)
Description
Converts a hexadecimalencoded 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 hexadecimalencoded
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 base64encoded string.
+++
 hex_str  bytes_str 
+++
 0AF  AAECA6ru7/8= 
 00010203aaeeefff  AK8= 
 666f6f626172  Zm9vYmFy 
+++
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 
++++
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 languagespecific 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 
++
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_length
is negativepattern
is 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" 
+++++
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_AND_CASEFOLD
NORMALIZE_AND_CASEFOLD(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.
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 
+++++++
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 
+++
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'@[azAZ09]+\.[azAZ09.]+') 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_ALL
REGEXP_EXTRACT_ALL(value, regexp)
Description
Returns an array of all substrings of value
that match the
re2 regular expression, regexp
. Returns an empty array
if there is no match.
If the regular expression contains a capturing group ((...)
), and there is a
match for that capturing group, that match is added to the results. If there
are multiple matches for a capturing group, the last match is added to the
results.
The REGEXP_EXTRACT_ALL
function only returns nonoverlapping matches. For
example, using this function to extract ana
from banana
returns only one
substring, not two.
Returns an error if:
 The regular expression is invalid
 The regular expression has more than one capturing group
Return type
ARRAY<STRING>
or ARRAY<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_EXTRACT
REGEXP_EXTRACT(value, regexp)
Description
Returns the first substring in value
that matches the
re2 regular expression,
regexp
. Returns NULL
if there is no match.
If the regular expression contains a capturing group ((...)
), and there is a
match for that capturing group, that match is returned. If there
are multiple matches for a capturing group, the last match is returned.
Returns an error if:
 The regular expression is invalid
 The regular expression has more than one capturing group
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'^[azAZ09_.+]+')
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'^[azAZ09_.+]+@[azAZ09]+\.([azAZ09.]+$)')
AS top_level_domain
FROM email_addresses;
++
 top_level_domain 
++
 com 
 org 
 net 
++
WITH
characters AS (
SELECT 'ab' AS value, '.b' AS regex UNION ALL
SELECT 'ab' AS value, '(.)b' AS regex UNION ALL
SELECT 'xyztb' AS value, '(.)+b' AS regex UNION ALL
SELECT 'ab' AS value, '(z)?b' AS regex
)
SELECT value, regex, REGEXP_EXTRACT(value, regex) AS result FROM characters;
++++
 value  regex  result 
++++
 ab  .b  ab 
 ab  (.)b  a 
 xyztb  (.)+b  t 
 ab  (z)?b  NULL 
++++
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 backslashedescaped 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.
To add a backslash in your regular expression, you must first escape it. For
example, SELECT REGEXP_REPLACE('abc', 'b(.)', 'X\\1');
returns aXc
. You can
also use raw strings to remove one layer of
escaping, for example SELECT REGEXP_REPLACE('abc', 'b(.)'