How to get difference from two timestamp in DB2? - sql

i have a requirement in which i have to get time difference of two timestamp in hours and than later find an average of the hours.
i am using below query to find difference of two timestamp but it does not give exact result it gives approx result. do we have any other solution to achieve the same.My two time stamps are as( LAST_MODIFIED_DATETIME - 2016-11-30 15:39:01.131 CREATE_DATETIME - 2016-07-01 17:25:52.375)
select timestampdiff(8, char(LAST_MODIFIED_DATETIME-CREATE_DATETIME)) as total_time from test_table where name='some name';

Use the built-in Db2 function HOURS_BETWEEN()
https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0061478.html

It's easy, by converting the timestamp to hours as follows:
(24*DAYS(last_modified_datetime)+MIDNIGHT_SECONDS(last_modified_datetime)/3600)
-
(24*DAYS(create_datetime)+MIDNIGHT_SECONDS(create_datetime)/3600)

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:

Hive SELECT records from 1 hour ago

I have a hive table that contains a column called timestamp. The timestamp is a bigint field generated from java System.currenttimemillis(). I suppose it should be in UTC. Right now I am trying to select records from 1 hour ago. I know in MySQL you can do something like:
SELECT * FROM table WHERE datetimefield >= DATE_SUB(NOW(), INTERVAL 1 HOUR)
In hive, it seems like NOW() is missing. I did some searching and find unix_timestamp(). I should be able to get the current UTC time in milliseconds by doing a unix_timestamp()*1000.
So if i want to get records from 1 hour ago I am thinking about doing something like:
SELECT * FROM hivetable WHERE datetimefield >= (unix_timestamp()*1000-3600000);
Can someone suggest if it's the right way to approach this problem? Also what if I want to select like 1 day ago? Seems inconvenient to convert that to milliseconds. Any help or suggested readings will be highly appreciated. Thanks in advance for your help.
Yes unix_timestamp() gets you the seconds elapsed since Unix epoch. You can subtract 60*60*1000 milliseconds and compare your field to get the desired records.
For Hive 1.2.0 and higher you can use current_timestamp
select *
from hivetable
where
datetimefield >= ((unix_timestamp()*1000) - 3600000);
For 1 day,convert the milliseconds to date format and use date_sub
select *
from hivetable
where
from_unixtime(unix_timestamp(datetimefield,'MM-dd-yyyy HH:mm:ss')) >=
date_sub(from_unixtime(unix_timestamp()),1);

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

Oracle timestamp, timezone and utc

I have an application, using an Oracle 11g (11.2.0.2.0 64 bit) db.
I have a lot of entries in a Person table. To access data I'm using different application (same data).
In this example I'm using birth_time field of my person table.
Some application queries data with birth_time directly, some other with to_char to reformat it, and some other with UTC function.
The problem is this: with same data, same query, result are different.
In this screenshot you can see the result with Oracle Sql developer (3.2.20.09)
All the timestamp are inserted with midnight timestamp, and in fact the to_char(..) and birth_time result are at midnight. UTC hours are returned with one hour less (Correct according to my timezone!) but some entry (here one for example, the last one) is TWO HOURS less (only few on thousand are Three)!!
The same query executed with sql*plus return the correct result with one hour of difference for all the entries!
Does anyone have a suggestion to approach this problem?
The issue is born because one of our application made with adobe flex seems to execute queries with UTC time, and the problems appears when you look at data with this component.
ps.:
"BIRTH_TIME" is TIMESTAMP (6)
Would it be possible for you to change the query used? If so, you could use the AT TIME ZONE expression to tell Oracle that this date is in UTC time zone:
SELECT SYS_EXTRACT_UTC(CAST(TRUNC(SYSDATE) AS TIMESTAMP)) AS val FROM dual;
Output:
VAL
----------------------------
13/11/20 23:00:00,000000000
Now, using AT TIME ZONE 'UTC' gets you the date you need:
SELECT SYS_EXTRACT_UTC(
CAST(
TRUNC(SYSDATE) AS TIMESTAMP)
AT TIME ZONE 'UTC') AS val FROM dual;
Output:
VAL
----------------------------
13/11/21 00:00:00,000000000

difference between two timestamps (in days) in oracle

SELECT MIN (snap_id) AS FIRST_SNAP,
MAX (snap_id) AS LAST_SNAP,
MIN (BEGIN_INTERVAL_TIME) AS FIRST_QUERY,
MAX (END_INTERVAL_TIME) AS LAST_QUERY,
max(end_interval_time) - min(begin_interval_time) as "TIME_ELAPSED"
FROM dba_hist_snapshot
ORDER BY snap_id;
2931 3103 5/28/2012 6:00:11.065 AM 6/4/2012 11:00:40.967 AM +07 05:00:29.902000
I would like the last columns output to be 7 (for the days). I have tried trunc and extract like some other posts mentioned but can't seem to get the syntax right. Any ideas?
Judging from your comment, you're using timestamp columns, not datetime. You could use extract to retrieve the hour difference, and then trunc(.../24) to get the whole number of days:
trunc(extract(hour from max(end_interval_time) - min(begin_interval_time))/24)
Or you could cast the timestamp to a date:
trunc(cast(max(end_interval_time) as date) -
cast(min(begin_interval_time) as date))