0

I have an array of strings that are valid dates and I want to convert them into an array of dates.

Is there a more straightforward way to do the following in BigQuery?

SELECT CAST(['2014-01-01', '2015-01-01'] AS ARRAY<date>)

Currently, I'm having to do the following workaround:

SELECT ARRAY(select cast(_ as date) FROM UNNEST((select ["2014-01-01", "2015-01-01"])) _)

The input/output should be:

IN:  ["2014-01-01", "2015-01-01"] 
--> 
OUT: [DATE "2014-01-01", DATE "2015-01-01"]
5
  • please clarify! so, let's say you have initial array of string and now you want to transform it into array of dates? is this the case? Commented Oct 11, 2022 at 1:05
  • @MikhailBerlyant yes, exactly. How to get: ["2014-01-01"] into [DATE "2014-01-01"] Commented Oct 11, 2022 at 1:31
  • 1
    I think the way you do is the only way - select array_strings, array(select date(_) from t.array_strings _) array_dates from your_table as t obviously assuming those strings are dates, otherwise you do that extra safe_cast(_ as date) . Note safe_cast not just cast Commented Oct 11, 2022 at 2:24
  • @MikhailBerlyant can you post your comment as answer? Commented Oct 12, 2022 at 1:00
  • I don't feel my above comments really qualified as an answer :o( they are just comments Commented Oct 12, 2022 at 3:40

1 Answer 1

2

There is already a filed feature request for this function. You may click on +1 to bring more attention to the feature request and also STAR it for you to be notified for the updates.

Currently, there is no other workaround to achieve this other than what you mentioned.

SELECT ARRAY(select cast(_ as date) FROM UNNEST((select ["2014-01-01", "2015-01-01"])) _)

Posting the answer as community wiki for the benefit of the community that might encounter this use case in the future.

Feel free to edit this answer for additional information.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.