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

How to decrypt stored password from SSMS registered servers

Cause for Parameter is incorrect 0x80070057 error in ssis

How to fail sql server agent job if powershell script fails