5

I'm trying to pull an array of INT64 s in BigQuery standard SQL from a column which is a long string of numbers separated by commas (for example, 2013,1625,1297,7634). I can pull an array of strings easily with:

SELECT
  SPLIT(string_col,",")
FROM
  table

However, I want to return an array of INT64 s, not an array of strings. How can I do that? I've tried

CAST(SPLIT(string_col,",") AS ARRAY<INT64>) 

but that doesn't work.

2 Answers 2

10

Below is for BigQuery Standard SQL

#standardSQL
WITH yourTable AS (
  SELECT 1 AS id, '2013,1625,1297,7634' AS string_col UNION ALL
  SELECT 2, '1,2,3,4,5'
)
SELECT id, 
  (SELECT ARRAY_AGG(CAST(num AS INT64)) 
    FROM UNNEST(SPLIT(string_col)) AS num
  ) AS num,
  ARRAY(SELECT CAST(num AS INT64) 
    FROM UNNEST(SPLIT(string_col)) AS num
  ) AS num_2
FROM yourTable
Sign up to request clarification or add additional context in comments.

1 Comment

You beat me to it :) Maybe demonstrate using an ARRAY subquery too? (That's what I was going to post).
0

Mikhail beat me to it and his answer is more extensive but adding this as a more minimal repro:

SELECT CAST(num as INT64) from unnest(SPLIT("2013,1625,1297,7634",",")) as num;

1 Comment

Check this : "However, I want to return an array of INT64s" :o)

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.