Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Big Query Extracting date from string

| orders |

|sold [2022-06-09 to 2022-06-17] |

What I am trying to get is

orders date1 date2
sold [2022-06-09 to 2022-06-17] 2022-06-09 2022-06-17
select
    Orders, REGEXP_EXTRACT_ALL(Orders,r'([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])') dates
from table

But BQ gives me the result in one row then split into 2 rows

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Row Orders Dates
1 text with dates 2022-06-09
2022-09-17

when exported to csv it is

Orders Dates
text with dates [2022-06-09,2022-06-27]

I have tried to select left/right to get the fields I want but get an array error from Big Query

Pointers Appreciated

>Solution :

Use below approach

select Orders, dates[offset(0)] as date1, dates[offset(1)] as date2
from table, 
UNNEST([struct(REGEXP_EXTRACT_ALL(Orders,r'([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])') as dates)])    

if applied to sample data in your question – output is

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading