Reputation: 59225
What's the easiest way to convert dates formatted as m/d/y (aka mm/dd/yyyy) to a TIMESTAMP in BigQuery?
Upvotes: 0
Views: 596
Reputation: 14014
If you use standard SQL in BigQuery, it becomes even simpler by using PARSE_TIMESTAMP function:
SELECT ds, PARSE_TIMESTAMP('%m/%d/%Y', ds)
FROM (SELECT '03/23/2015' ds)
(details how to enable standard SQL in BigQuery: https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql)
Upvotes: 2
Reputation: 173056
SELECT ds,
TIMESTAMP(REGEXP_REPLACE(ds, r'(\d{2})/(\d{2})/(\d{4})', r'\3-\1-\2')) ts
FROM (SELECT '03/23/2015' ds)
Upvotes: 0
Reputation: 59225
Try REGEXP_REPLACE():
SELECT ds,
TIMESTAMP(REGEXP_REPLACE(ds, r'(..)/(..)/(....)', r'\3-\1-\2')) ts
FROM (SELECT '03/23/2015' ds)
Upvotes: 1