Search code examples
datetimecastinggoogle-bigquerystring-to-datetime

How do I cast dd/mm/yyyy string into date in BigQuery?


I have 3 columns 1. dd/mm/yyyy (stored as a string) 2. app_id and #downloads of apps

I have to find unique ids of apps downloaded within a week.

Thank you


Solution

  • You can convert your dd/MM/yyyy strings into BigQuery timestamps using something like the following:

    SELECT TIMESTAMP(year + '-' + month + '-' + day) as output_timestamp
    FROM (
      SELECT 
        REGEXP_EXTRACT(input_date, '.*/([0-9]{4})$') as year, 
        REGEXP_EXTRACT(input_date, '^([0-9]{2}).*') as day, 
        REGEXP_EXTRACT(input_date, '.*/([0-9]{2})/.*') AS month 
      FROM 
        (SELECT '30/10/2015' as input_date),
        (SELECT '25/01/2015' as input_date)
    )
    

    Once you have converted them to timestamps, you may find the date and time functions useful, depending on what you're trying to do.