Conversion rules Stay organized with collections Save and categorize content based on your preferences.
GoogleSQL for BigQuery supports conversion.Conversion includes, but isn't limited to, casting, coercion, andsupertyping.
- Casting is explicit conversion and uses the
CAST()function. - Coercion is implicit conversion, which GoogleSQL performsautomatically under the conditions described below.
- A supertype is a common type to which two or more expressions can be coerced.
There are also conversions that have their own function names, such asPARSE_DATE(). To learn more about these functions, seeConversion functions.
Comparison of casting and coercion
The following table summarizes all possible cast and coercion possibilities forGoogleSQL data types. TheCoerce to column applies to allexpressions of a given data type, (for example, acolumn), butliterals and parameters can also be coerced. Seeliteral coercion andquery parameter coercion for details.
| From type | Cast to | Coerce to |
|---|---|---|
INT64 | BOOLINT64NUMERICBIGNUMERICFLOAT64STRING | NUMERICBIGNUMERICFLOAT64 |
NUMERIC | INT64NUMERICBIGNUMERICFLOAT64STRING | BIGNUMERICFLOAT64 |
BIGNUMERIC | INT64NUMERICBIGNUMERICFLOAT64STRING | FLOAT64 |
FLOAT64 | INT64NUMERICBIGNUMERICFLOAT64STRING | |
BOOL | BOOLINT64STRING | |
STRING | BOOLINT64NUMERICBIGNUMERICFLOAT64STRINGBYTESDATEDATETIMETIMETIMESTAMPRANGE | |
BYTES | STRINGBYTES | |
DATE | STRINGDATEDATETIMETIMESTAMP | DATETIME |
DATETIME | STRINGDATEDATETIMETIMETIMESTAMP | |
TIME | STRINGTIME | |
TIMESTAMP | STRINGDATEDATETIMETIMETIMESTAMP | |
ARRAY | ARRAY | |
STRUCT | STRUCT | |
RANGE | RANGESTRING |
Casting
Most data types can be cast from one type to another with theCAST function.When usingCAST, a query can fail if GoogleSQL is unable to performthe cast. If you want to protect your queries from these types of errors, youcan useSAFE_CAST. To learn more about the rules forCAST,SAFE_CAST andother casting functions, seeConversion functions.
Coercion
GoogleSQL coerces the result type of an argument expression to anothertype if needed to match function signatures. For example, if functionfunc()is defined to take a single argument of typeFLOAT64and an expression is used as an argument that has a result type ofINT64, then the result of the expression will becoerced toFLOAT64 type beforefunc() is computed.
Literal coercion
GoogleSQL supports the following literal coercions:
| Input data type | Result data type | Notes |
|---|---|---|
FLOAT64 literal | NUMERIC | Coercion may not be exact, and returns a close value. |
STRING literal | DATEDATETIMETIMETIMESTAMP |
Literal coercion is needed when the actual literal type is different from thetype expected by the function in question. Forexample, if functionfunc() takes a DATE argument,then the expressionfunc("2014-09-27") is valid because thestring literal"2014-09-27" is coerced toDATE.
Literal conversion is evaluated at analysis time, and gives an error if theinput literal can't be converted successfully to the target type.
Note: String literals don't coerce to numeric types.Query parameter coercion
GoogleSQL supports the following query parameter coercions:
| Input data type | Result data type |
|---|---|
STRING parameter | DATEDATETIMETIMETIMESTAMP |
If the parameter value can't be coerced successfully to the target type, anerror is provided.
Supertypes
A supertype is a common type to which two or more expressions can be coerced.Supertypes are used with set operations such asUNION ALL and expressions suchasCASE that expect multiple arguments with matching types. Each type has oneor more supertypes, including itself, which defines its set of supertypes.
| Input type | Supertypes |
|---|---|
BOOL | BOOL |
INT64 | INT64FLOAT64NUMERICBIGNUMERIC |
FLOAT64 | FLOAT64 |
NUMERIC | NUMERICBIGNUMERICFLOAT64 |
DECIMAL | DECIMALBIGDECIMALFLOAT64 |
BIGNUMERIC | BIGNUMERICFLOAT64 |
BIGDECIMAL | BIGDECIMALFLOAT64 |
STRING | STRING |
DATE | DATE |
TIME | TIME |
DATETIME | DATETIME |
TIMESTAMP | TIMESTAMP |
BYTES | BYTES |
STRUCT | STRUCT with the same field position types. |
ARRAY | ARRAY with the same element types. |
GEOGRAPHY | GEOGRAPHY |
RANGE | RANGE with the same subtype. |
If you want to find the supertype for a set of input types, first determine theintersection of the set of supertypes for each input type. If that set is emptythen the input types have no common supertype. If that set is non-empty, thenthe common supertype is generally themost specific type in that set. Generally,the most specific type is the type with the most restrictive domain.
Examples
| Input types | Common supertype | Returns | Notes |
|---|---|---|---|
INT64FLOAT64 | FLOAT64 | FLOAT64 | If you apply supertyping toINT64 andFLOAT64, supertyping succeeds because they they share a supertype,FLOAT64. |
INT64BOOL | None | Error | If you apply supertyping toINT64 andBOOL, supertyping fails because they don't share a common supertype. |
Exact and inexact types
Numeric types can be exact or inexact. For supertyping, if all of theinput types are exact types, then the resulting supertype can only be anexact type.
The following table contains a list of exact and inexact numeric data types.
| Exact types | Inexact types |
|---|---|
INT64NUMERICBIGNUMERIC | FLOAT64 |
Examples
| Input types | Common supertype | Returns | Notes |
|---|---|---|---|
INT64FLOAT64 | FLOAT64 | FLOAT64 | If supertyping is applied toINT64 andFLOAT64, supertyping succeeds because there are exact and inexact numeric types being supertyped. |
Types specificity
Each type has a domain of values that it supports. A type with anarrow domain is more specific than a type with a wider domain. Exact typesare more specific than inexact types because inexact types have a wider rangeof domain values that are supported than exact types. For example,INT64 is more specific thanFLOAT64.
Supertypes and literals
Supertype rules for literals are more permissive than for normal expressions,and are consistent with implicit coercion rules. The following algorithm is usedwhen the input set of types includes types related to literals:
- If there exists non-literals in the set, find the set of common supertypesof the non-literals.
- If there is at least one possible supertype, find themost specific type towhich the remaining literal types can be implicitly coerced and return thatsupertype. Otherwise, there is no supertype.
- If the set only contains types related to literals, compute the supertype ofthe literal types.
- If all input types are related to
NULLliterals, then the resultingsupertype isINT64. - If no common supertype is found, an error is produced.
Examples
| Input types | Common supertype | Returns |
|---|---|---|
INT64 literalUINT64 expression | UINT64 | UINT64 |
TIMESTAMP expressionSTRING literal | TIMESTAMP | TIMESTAMP |
NULL literalNULL literal | INT64 | INT64 |
BOOL literalTIMESTAMP literal | None | Error |
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.