Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast String into Date in BIGQUERY When Date is in the following format: M/D/YYYY

I have a string that is a date and it is in M/D/YYYY ie:

1/1/2018
12/31/2018

I get an invalid date error ( it shows: '2/18/2018' as the invalid date)

Any ideas?

like image 920
bcascone Avatar asked Oct 18 '25 17:10

bcascone


1 Answers

Below is example for BigQuery Standard SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '1/1/2018' date_as_string UNION ALL
  SELECT '12/31/2018'
)
SELECT PARSE_DATE('%m/%d/%Y', date_as_string) date_as_date
FROM `project.dataset.table`

with output:

Row date_as_date     
1   2018-01-01   
2   2018-12-31   
like image 196
Mikhail Berlyant Avatar answered Oct 21 '25 07:10

Mikhail Berlyant