BigQuery timestamp does not support more than six decimals

Converting iso dates with timezones to BigQuery timestamp can be a hassle if the date contains more than six decimals.

select cast('2023-03-21T06:49:19.1234567+00:00' as timestamp)

Invalid timestamp: '2023-03-21T06:49:19.1234567+00:00'






One solution could be to truncate the fractional part like so

create or replace function [your_dataset].truncate_iso_timestamp_string(detailed_iso_timestamp string)
/* big query crashes with more than 6 decimals on dates
This function will truncate to 6 decimals and cast to timestamp. */
returns timestamp
as (
  cast(
    concat(
      left(
        split(detailed_iso_timestamp,'+')[offset(0)]26),
        '+'
        split(detailed_iso_timestamp,'+')[offset(1)]) as timestamp)
);



select [your_dataset].truncate_iso_timestamp_string('2023-03-21T06:49:19.1234567+00:00')



Comments

Popular posts from this blog

SSIS: Set parent package variable from child package

How to decrypt stored password from SSMS registered servers

Insert bulk statement does not support recompile (SQL 2017)