When working with data, situations will arise where data will need to be converted from one type to another. For instance, an analyst might receive date in a format that BigQuery does not support. When loaded, BigQuery will automatically detect the type as a string. If the analyst wants to do any type of date arithmetic, they will need to convert the string to a date.
The following is a list of all types that can be cast into other types:
- Casting can be achieved using the CAST() function:
CAST(<EXPRESSION> AS NEWTYPE)
For instance, if I want to cast a Boolean into an integer: CAST(X AS INT64).
The SAFE_CAST() function can be used to return a NULL value instead of an error when a cast is unsuccessful.
- Strings of the format YYYY-DD-MM can be cast to Date, Datetime, or Timestamp: CAST(X AS DATE). In the case of casting to Datetime or Timestamp...