Extract date, hour from the utc time - google-bigquery

I would like to extract the date & hour from UTC time from the below table in bigquery. I have used timestamp for getting the date or time using the below code. I would like to apply the code for the entire column. How to apply timestamp for the entire column? Can you please assist with it?
SELECT EXTRACT(HOUR FROM TIMESTAMP "2020-05-03 16:49:47.583494")
My data is like this
I want result like this:

You can do it this way:
SELECT my_column AS original_value,
DATE_FORMAT(STR_TO_DATE(my_column, "%Y-%m-%d %H:%i:%s.%f UTC"), "%e/%m/%Y") AS date,
DATE_FORMAT(STR_TO_DATE(my_column, "%Y-%m-%d %H:%i:%s.%f UTC"), "%l%p") AS hour
FROM my_table;
I am assuming that the column is VARCHAR, that's why I am converting it to DATE.
Output:
Demo:
You can check the demo here.
Edit:
My initial thought was that OP wanted the query for MySQL (probably BigQuery is based on that). But it turns out that BigQuery is not based on MySQL. So you can use FORMAT_TIMESTAMP in BigQuery, this is how the query would look:
SELECT Occurrence AS original_value,
FORMAT_TIMESTAMP("%e/%m/%Y", Occurrence) AS date,
FORMAT_TIMESTAMP("%l%p", Occurrence) AS hour
FROM mytable

Related

Converting time into an integer on SQL

I am trying to calculate the average of ride_length (hh:mm:ss format) on Big Query.
1
For example, I am aiming for a result that says 1 instead of 01:00:00.
I tried using the CAST function to convert it into an integer but it didn't work. I also tried following the steps of answers to questions similar to mine but they didn't work as well.
You can use Extract
function which will return the specified part of the time expressions. You can try the below queries.
To extract hour from the TIME expression you can consider the below query.
Query:
SELECT EXTRACT(HOUR FROM TIME "01:00:00") as hour;
Output:
To extract hour from TIMESTAMP expression below query can be considered.
Query:
select EXTRACT(hour from timestamp "2023-01-13 15:30:00" AT TIME ZONE "America/Los_Angeles") as hour
Output:
To extract hour from the TIME column you can consider the below sample query.
Query:
SELECT time,EXTRACT(HOUR FROM (time)) AS hour from `bigquery-public-data.austin_incidents.incidents_2008` limit 10
Output:

How to extract only the hour from a timestamp in sql?

Hi I have data with column name 'trx_time'.
The data looks like this:
'2022-03-06 11:25:36'
I just want to extract the hour from the string. How can I do it?
I was thinking regex_extract. But don't know how to write the regex.
This will extract the Hour from a date field in Presto.
SELECT extract(HOUR FROM trx_time) as hour FROM table_name;
However, if your field is a String type representing a timestamp (ISO 8601), you would have to use the from_iso8601_date or from_iso8601_timestamp functions.
SELECT extract(HOUR FROM from_iso8601_date(trx_time)) as hour FROM table_name;

Impala: set a constant date for timestamp

is there an alternative to the to_char() function in impala? I want to set a timestamp field where the date and minutes are fixed and only showing the hours, but can't seem to find an alternative.
This is my existing code in postgres which I need to convert to impala.
select to_char(starttime, '1900-01-01 HH24:00:00')::timestamp
Any help is appreciated!
In Impala, you can use
SELECT hours_add('1900-01-01', hour(now()));
to get the same result
function hour extracts the hour part from a timestamp
function hours_add adds hour to a timestamp

EXTRACT the date and time - (Teradata)

I am trying to extract the date and time from a field in Teradata.
The field in question is:
VwNIMEventFct.EVENT_GMT_TIMESTAMP
Here is what the data look like:
01/02/2012 12:18:59.306000
I'd like the date and time only.
I have tried using EXTRACT(Date, EXTRACT(DAY_HOUR and a few others with no success.
DATE_FORMAT() does not appear to work since I'm on Teradata.
How would I select the date and time from VwNIMEventFct.EVENT_GMT_TIMESTAMP?
If the datatype of EVENT_GMT_TIMESTAMP is a TIMESTAMP, it's simple Standard SQL:
CAST(EVENT_GMT_TIMESTAMP AS DATE)
CAST(EVENT_GMT_TIMESTAMP AS TIME)
If it's a CHAR you need to apply a FORMAT, too:
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS DATE)
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS TIME)
Edit:
For simply changing the display format you need to add a FORMAT and a CAST to a string:
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMI') AS CHAR(12))
or
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMISS') AS CHAR(14))
If you don't care about display, just want to truncate the seconds:
EVENT_GMT_TIMESTAMP - (EXTRACT(SECOND FROM EVENT_GMT_TIMESTAMP) * INTERVAL '1.000000' SECOND)
Working with timestamps is a bit tricky :-)
I know this is an old topic, but I've struggled with this too. Try:
CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP(0))
The result will be
01/02/2012 12:18:59
The datatype will still be timestamp, but it will just be the date and time with no microseconds (looks just like a datetime object in Microsoft SQL).

how to have the right sum of type Time with sql [duplicate]

How to caculate sum of times of my colonne called "timeSpent" having this format: HH:mm
in SQL? I am using MySQL.
the type of my column is Time.
it has this structure
TimeFrom like 10:00:00 12:00:00 02:00:00
TimeUntil 08:00:00 09:15:00 01:15:00
Time spent
total time 03:15:00
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) AS timeSum
FROM YourTableName
100% working code to get sum of time out of MYSQL Database:
SELECT
SEC_TO_TIME( SUM(time_to_sec(`db`.`tablename`)))
As timeSum
FROM
`tablename`
Try and confirm.
Thanks.
In MySQL, you would do something like this to get the time interval:
SELECT TIMEDIFF('08:00:00', '10:00:00');
Then to add the time intervals, you would do:
SELECT ADDTIME('01:00:00', '01:30:00');
Unfortunately, you're not storing dates or using 24-hour time, so these calculations would end up incorrect since your TimeUntil is actually lower than your TimeFrom.
Another approach would be (assuming you sort out the above issue) to store the time intervals as seconds using TIMESTAMPDIFF():
UPDATE my_table SET time_spent=TIMESTAMPDIFF(start, end));
SELECT SEC_TO_TIME(SUM(time_spent)) FROM my_table;
If the data type of the timeSpent column is TIME you should be able to use the following query:
SELECT SUM(timeSpent)
FROM YourTableName -- replace YourTableName with the actual table name
However, if that is not the case, you may have to use a cast to convert to the Time data type. Something like this should work:
SELECT SUM(timeSpent - CAST('0:0:0' as TIME))
FROM YourTableName -- replace YourTableName with the actual table name