Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to create an array of hours between 2 timestamps?

Start_time: 2023-01-01 07:06:00 UTC

End_time: 2023-01-01 16:03:00 UTC

I need hours in between the two timestamps, the hours of 7am (on the 1st), 8,9,10,11,12,1,2,3,4

Appreciate any help on this!

0 1 148
1 REPLY 1

SELECT ARRAY(
  SELECT EXTRACT(HOUR FROM t1) FROM UNNEST(
    (SELECT
      GENERATE_TIMESTAMP_ARRAY(
        TIMESTAMP_TRUNC(TIMESTAMP("2023-01-01 07:06:00"), HOUR),
        TIMESTAMP_TRUNC(TIMESTAMP("2023-01-01 16:03:00"), HOUR),
        INTERVAL 1 HOUR)
      )
    ) AS t1
  ) AS result