Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting YYYYMMDD string to date in standard SQL / BigQuery

I have this column called 'Date' that contains dates formatted this way: '20150101'.

I tried using sql substring, but when I run the query using 'date' function in sql, it doesn't work on the date format I have.

Here is the query I made:

SELECT (DATE ((SUBSTR(JOUR, 1, 4), 
               SUBSTR(JOUR, 5, 2), 
               SUBSTR(JOUR, 7, 2)))) As date 
FROM TABLE

Any idea? I couldn't find anything similar to this date format! I found one that uses the convert function but it's not in StandardSQL or BigQuery

like image 447
helloworld Avatar asked Feb 18 '19 14:02

helloworld


People also ask

How do you convert date format from Yyyymmdd to yyyy-mm-dd in SQL?

Convert Char 'yyyymmdd' back to Date data types in SQL Server. Now, convert the Character format 'yyyymmdd' to a Date and DateTime data type using CAST and CONVERT. --A. Cast and Convert datatype DATE: SELECT [CharDate], CAST([CharDate] AS DATE) as 'Date-CAST', CONVERT(DATE,[CharDate]) as 'Date-CONVERT' FROM [dbo].

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

What is BigQuery date format?

In BigQuery, dates and times can be one of the following datatypes: DATE : calendar date (e.g. 2020-01-01)


1 Answers

What about PARSE_DATE ?

SELECT PARSE_DATE("%Y%m%d", "20190101") as parsed;

For your table:

SELECT PARSE_DATE ("%Y%m%d", JOUR) As mydatecolumn from TABLE
like image 69
dani herrera Avatar answered Sep 18 '22 01:09

dani herrera