Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to date time conversion Bigquery

I have a source table which has dates stored in string format as below:

1984-10-28 00:00:00
1988-11-22 00:00:00 
1990-01-08 00:00:00
1983-06-22 00:00:00 

Then created a table test1 as below:

mk -t xyz.test birthdate:timestamp

When I write a query to convert all the dates from source table to timestamp and save it to test1 it gives an error:

SELECT FORMAT_UTC_USEC(birthdate) AS birthdate FROM [sourcetable] LIMIT 10

Error: Invalid schema update. Field birthdate has changed type
Job ID: [Project]:job_5P4gFVl4wiYuyXL0vY5VZNtCZOU

Please help if I am doing anything wrong

like image 662
Maulik Dholaria Avatar asked Apr 24 '14 21:04

Maulik Dholaria


People also ask

How do you create a timestamp in BigQuery?

The format is: YYYY-MM-DD HH:MM:SS (e.g. 2021-05-15 16:45:23). Timestamp type: Date, time, and time zone information are all included in timestamps. If no time zone is given, the format falls back to UTC. The format is: YYYY-MM-DD [Timezone] HH:MM:SS (e.g. 2021-05-15 16:45:18 UTC).

How does BigQuery store timestamp?

BigQuery stores TIMESTAMP data internally as a UNIX timestamp with microsecond precision. A positive or negative decimal number. A positive number specifies the number of seconds since the epoch (1970-01-01 00:00:00 UTC), and a negative number specifies the number of seconds before the epoch.


1 Answers

Never mind. I found it. I was overlooking a big part of bigquery's query reference document.

TIMESTAMP()
Convert a date string to a TIMESTAMP data type.

SELECT TIMESTAMP("2012-10-01 01:02:03");

Returns: 2012-10-01 01:02:03 UTC

So the correct query as below: SELECT TIMESTAMP(birthdate) AS birthdate FROM [sourcetable] LIMIT 10

like image 80
Maulik Dholaria Avatar answered Oct 02 '22 00:10

Maulik Dholaria