10

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

dani herrera
  • 48,760
  • 8
  • 117
  • 177
helloworld
  • 416
  • 2
  • 6
  • 15

1 Answers1

19

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
dani herrera
  • 48,760
  • 8
  • 117
  • 177
  • Any reason why this wouldn't work and produces this message: Failed to parse input string "12102021"? Any help here is most appreciated! – lcm Jun 08 '22 at 07:14
  • "12102021" using "%Y%m%d" is "1210-20-21" and raises error because 20 is not a valid moth. Change mask format to "%m%d%Y" or whatever according to format you are using. – dani herrera Jul 05 '23 at 14:23