Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract just the hour of a timestamp using standardSQL

How can I extract just the hour of a timestamp using standardSQL.

I've tried everything and no function works. The problem is that I have to extract the time from a column and this column is in the following format:2018-07-09T02:40:23.652Z

If I just put the date, it works, but if I put the column it gives the error below:

Syntax error: Expected ")" but got identifier "searchIntention" at [4:32]

Follow the query below:

#standardSQL
select TOTAL, dia, hora FROM 
(SELECT cast(replace(replace(searchIntention.createdDate,'T',' '),'Z','')as 
DateTime) AS DIA, 
FORMAT_DATETIME("%k", DATETIME searchIntention.createdDate) as HORA,
count(searchintention.id) as Total
from `searchs.searchs2016626`
GROUP BY DIA)

Please, help me. :(

like image 746
Layla Comparin Avatar asked Jul 26 '18 14:07

Layla Comparin


People also ask

How do I create a timestamp query in SQL?

The basic syntax of “timestamp” data type in SQL is as follows : Timestamp 'date_expression time_expression'; A valid timestamp data expression consists of a date and a time, followed by an optional BC or AD.

What is Timestamp_millis?

TIMESTAMP_MILLIS(int64_expression) Description. Interprets int64_expression as the number of milliseconds since 1970-01-01 00:00:00 UTC and returns a timestamp.


1 Answers

How can I extract just the hour of a timestamp using standardSQL?

Below is for BigQuery Standard SQL

You can use EXTRACT(HOUR FROM yourTimeStampColumn)

for example:

SELECT EXTRACT(HOUR FROM CURRENT_TIMESTAMP())   

or

SELECT EXTRACT(HOUR FROM TIMESTAMP '2018-07-09T02:40:23.652Z')

or

SELECT EXTRACT(HOUR FROM TIMESTAMP('2018-07-09T02:40:23.652Z'))
like image 90
Mikhail Berlyant Avatar answered Sep 28 '22 08:09

Mikhail Berlyant