Convert seconds to days, hours, minutes in Bigquery - sql

i'm having trouble in converting seconds in Bigquery, is there any function to convert seconds to hour:minute:second format in Bigquery? i already tried the TIMESTAMP_SECONDS() function but it also returns some date and i can't use it if the hour more than 23.
for example:
second= 100000
result= 27:46:40
or maybe as 1 day 3 hour 46 minute 40 second
and i also want it in timestamp datatype so i can order it ascending or descending.

Below is for BigQuery Standard SQL
#standardSQL
select seconds,
regexp_replace(
cast(time(ts) as string),
r'^\d\d',
cast(extract(hour from time(ts)) + 24 * unix_date(date(ts)) as string)
) as option1,
format(
'%i day %i hour %i minute %i second',
unix_date(date(ts)),
extract(hour from time(ts)),
extract(minute from time(ts)),
extract(second from time(ts))
) as option2
from `project.dataset.table`,
unnest([timestamp_seconds(seconds)]) ts
if to apply to sample data from your question as in
with `project.dataset.table` AS (
select 100000 seconds union all
select 200000 union all
select 300000
)
the output is

With recently introduced INTERVAL data type and respective functions - such conversion becomes much easier
select seconds,
make_interval(second => seconds) result,
justify_interval(make_interval(second => seconds)) normalized_result
from `project.dataset.table`
with output like

Related

Changing Schema in GBQ

I manually created a duration field in excel which calculates a time duration between a start date and end date and formatted it as HH:MM:SS. When I upload this in Google Big query it appears as a string. Then I used a CAST function
SELECT CAST (ride_length AS INTERVAL)
to change the type from STRING to INTERVAL and got the following error
Invalid INTERVAL value '0.00:22:28'
I need to change the data type to a number so I can calculate maximum, minimum, and average duration. GBQ doesn't let me do that with a string data type.
SELECT CAST (ride_length AS INTERVAL)
Can you try below:
WITH
sampleData AS(
SELECT
'01:05:07' AS duration
UNION ALL
SELECT
'02:05:07' AS duration
UNION ALL
SELECT
'04:05:07' AS duration ),
sample AS (
SELECT
CAST(duration AS TIME FORMAT 'HH24:MI:SS') AS duration2
FROM
sampleData --converts string to time format
)
SELECT
MAX(duration2) AS MAXTIME,
MIN(duration2) AS MINTIME,
TIME(
EXTRACT(hour FROM AVG(duration2 - '0:0:0')),
EXTRACT(minute FROM AVG(duration2 - '0:0:0')),
EXTRACT(second FROM AVG(duration2 - '0:0:0'))
) as AVERAGE from
sample;
This is given that your sample's format are accepted by BigQuery, See the sample output below for getting Max, Min, Average:
UPDATE:
Additional Code (for better readability, just change the names appropriate to your table ):
WITH sample AS (
SELECT
CAST(yourfieldName AS TIME FORMAT 'HH24:MI:SS') AS duration
FROM
yourTableId --converts string to time format
)
SELECT
MAX(duration) AS MAXTIME,
MIN(duration) AS MINTIME,
TIME(
EXTRACT(hour FROM AVG(duration - '0:0:0')),
EXTRACT(minute FROM AVG(duration - '0:0:0')),
EXTRACT(second FROM AVG(duration - '0:0:0'))
) as AVERAGE from
sample;

Timestamp operations in hive

How can I subtract 2 timestamp columns in hive and store the result in a separate column in its equivalent hours format?
Let's say if you have timestamp in the given format : 2016-10-16 10:51:00.000
You can try following:
SELECT
cast(
round(
cast((e-s) as double) * 1000
) as int
) time_difference
FROM (SELECT cast(starttime as double) s, cast(endtime as double) e from table1) q;
It will give you the difference of both timestamps in millisecond. Then you can convert it to your expected format(hours,days etc.) .
Use the unix_timestamp function to convert a hive timestamp to a number of seconds since epoch. Subtracting the unix timestemp results gives an answer in seconds.
SELECT start_dttm,
(unix_timestamp(end_dttm) - unix_timestamp(start_dttm))/60 AS duration_in_minutes
FROM dev_edw.audit_history_hb
WHERE script_name LIKE '%0_hive_d%'
AND parent_audit_id is null
ORDER BY start_dttm desc

oracle sql date format to only seconds [duplicate]

This question already has answers here:
How to find difference b/w TIMESTAMP format values in Oracle?
(3 answers)
Closed 8 years ago.
So I'm trying to convet a timestamp to seconds.
I read that you could do it this way
to_char(to_date(10000,'sssss'),'hh24:mi:ss')
But turns out this way you can't go over 86399 seconds.
This is my date format: +000000000 00:00:00.000000
What's the best approach to converting this to seconds? (this is the result of subtracting two dates to find the difference).
You could convert timestamp to date by adding a number (zero in our case).
Oracle downgrade then the type from timestamp to date
ex:
select systimestamp+0 as sysdate_ from dual
and the difference in secondes between 2 timestamp:
SQL> select 24*60*60*
((SYSTIMESTAMP+0)
-(TO_TIMESTAMP('16-MAY-1414:10:10.123000','DD-MON-RRHH24:MI:SS.FF')+0)
)
diff_ss from dual;
DIFF_SS
----------
15140
It looks like you're trying to find the total number of seconds in an interval (which is the datatype returned when you subtract two timestamps). In order to convert the interval to seconds, you need to extract each component and convert them to seconds. Here's an example:
SELECT interval_value,
(EXTRACT (DAY FROM interval_value) * 24 * 60 * 60)
+ (EXTRACT (HOUR FROM interval_value) * 60 * 60)
+ (EXTRACT (MINUTE FROM interval_value) * 60)
+ EXTRACT (SECOND FROM interval_value)
AS interval_in_sec
FROM (SELECT SYSTIMESTAMP - TRUNC (SYSTIMESTAMP - 1) AS interval_value
FROM DUAL)
If you want the number of seconds between two dates (or timestamps),
select floor(
(to_date(to_char(timestamp1, 'yyyy-mm-dd HH24:MI:ss'),'yyyy-mm-dd HH24:MI:ss')
- to_date(to_char(timestamp2, 'yyyy-mm-dd HH24:MI:ss'),'yyyy-mm-dd HH24:MI:ss')
)
* 24 -- hours per day
* 60 -- minutes per hour
* 60 -- seconds per minute
)
etc

timestamp having milliseconds convert to seconds in oracle

I captured request and response time for procedure i need to calculate time take by substract both request -response to find time taken.
P_REQUEST_TIME :='20/MAR/2014 03:03:50.785662 PM';
P_RESPONSE_TIME :='20/MAR/2014 03:03:50.785816 PM';
SELECT TO_TIMESTAMP(P_REQUEST_TIME)-TO_TIMESTAMP(P_RESPONSE_TIME)
into l_actual_time
FROM dual;
Getting result is Result:='-000000000 00:00:00.000154000';
I need this as seconds.
Use EXTRACT function.
SELECT EXTRACT(SECOND FROM TO_TIMESTAMP(P_REQUEST_TIME)-TO_TIMESTAMP(P_RESPONSE_TIME)) diff_seconds
FROM <table_name>;
select extract( day from diff ) days,
extract( hour from diff ) hours,
extract( minute from diff ) minutes,
extract( second from diff ) seconds
from (SELECT TO_TIMESTAMP(REQUEST_DTTM)-TO_TIMESTAMP(RESPONS_DTTM) diff FROM hit_tracer);
if u want the difference between two timestamps in seconds, this is the query
SELECT (P_REQUEST_TIME-P_RESPONSE_TIME) *24 *60*60
into l_actual_time
FROM dual;

Teradara: Casting Seconds to Hours, Minutes and Seconds

I need to cast a Duration, measured in seconds, Decimal(18,0) to Hours, Minutes and Seconds.
e.g.:
8192 Seconds => 2:16:32 (Hours:Minutes:Seconds)
I've found (here) that I can do this if the seconds are a literal string, like this:
SELECT CAST(INTERVAL '8192' SECOND AS INTERVAL HOUR TO SECOND)
But when I try:
SELECT Event_Id
, CAST(INTERVAL Duration SECOND AS INTERVAL HOUR TO SECOND)
FROM EVENT_TABLE
WHERE <some conditions>
Event_Id and Duration are both declared Decimal(18,0) in the table EVENT_TABLE
Teradata complains:
[Error 3707] Syntax error, expected something like a string or Unicode character literal between the 'INTERVAL' keyword and the integer 8
What is the correct syntax or approach to use here?
SOLUTION: based on BellevueBob's post
SELECT Event_Id
, CAST(
Duration SECOND * INTERVAL '0000:01' MINUTE TO SECOND /* 1 */
AS INTERVAL HOUR TO SECOND /* 2 */
)
FROM EVENT_TABLE
WHERE <some conditions>
Explanation:
Multiply the known duration in seconds with an interval of one second length
convert the resulting intervall of seconds into an interval of hours, minutes and seconds.
From the same solution you found, try this:
select CAST(duration * INTERVAL '0000:01' MINUTE TO SECOND
AS INTERVAL HOUR TO SECOND)
from (
select cast(8192 as decimal(18,0)) as duration
) x