简体   繁体   中英

STRING to DATE in BIGQUERY

I am struggling to try to do this with Google BigQuery:

I do have a column with dates in the following STRING format:

6/9/2017   (M/D/YYYY)

I am wondering how can I deal with this, trying to use the DATE clause in order to get the this format: YYYY-MM-DD.

Easy one, with standard SQL:

#standardSQL
SELECT PARSE_DATE('%m/%d/%Y',  '6/22/2017')


2017-06-22  

https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#supported-format-elements-for-date

This solution can work

SELECT    CAST(
            CONCAT(
              SUBSTR(DT_DOCUMENTO, 0 , 4), 
              '-' ,
              SUBSTR(DT_DOCUMENTO, 5 , 2), 
              '-' , 
              SUBSTR(DT_DOCUMENTO, 7 , 2) 
            ) AS DATE
          ) AS FORMAT_DATE

If you are lazy you can do date parsing with automatic format detection

select bigfunctions.eu.parse_date('1/20/21') as cleaned_date

will give

+--------------------+
| cleaned_date       |
+--------------------+
| date('2021-01-20') |
+--------------------+

as well as

select bigfunctions.eu.parse_date('Wed Jan 20 21:47:00 2021') as cleaned_date

https://unytics.io/bigfunctions/reference/#parse_date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM