TimeDiff Calculation needed Including Milliseconds in HIVE - hive

We need to calculate the TIME DIFF along with Milliseconds in HIVE Using HUE.
Please find the below screen shot.
select
((unix_timestamp('2017-12-26 14:35:19.609')
- unix_timestamp('2017-12-26 14:35:18.779'))*1000) as timediff
Output :
timediff
1000
In the above case we are getting only Seconds but we are not able to get the Milliseconds Precision.
Could you please provide the solution to achieve this issue using HIVE.(Without Using UDF's in HIVE).

Referring from this answer,for milliseconds, you shouldn't use the unix_timestamp functions because these consider date as seconds since epoch.
How do I get millisecond precision in hive?
So, you could CAST it to TIMESTAMP and then DOUBLE to get the desired result.
SELECT ROUND((CAST(CAST('2017-12-26 14:35:19.609' AS TIMESTAMP) AS DOUBLE)
- CAST(CAST('2017-12-26 14:35:18.779' AS TIMESTAMP) AS DOUBLE)) * 1000)
as timediff
timediff
---------
830

Related

SQL Duration Column

I'm trying to convert a duration column which is in a Date/Time format to minutes Integer.
so for 8 Hours the data looks like this 01/01/1970 08:00:00 what can I do to convert to 480 minutes
I'm connecting to Service Now DB through a ODBC connector
Thanking you in advance
In standard SQL, you can use:
select extract(hour from duration) * 60
If you wanted to include minutes as well:
select extract(hour from duration) * 60 + extract(minute from duration)
However, date/time functions vary significantly depending on the underlying database. So, you may need to tweak the above logic.

Mysql Date to minutes

Im new using sql, i tried to convert interval to minutes. Is there a developed function that did this.
Thank you
You can use epoch and divide by 60.
select extract(epoch from <date>) / 60
extract(epoch) gives the number of seconds since 1970-01-01. So, this gives the number of minutes since 1970-01-01, which seems like a reasonable interpretation of your question.
I suppose from tag that you use PostgreSql.
Postgresql has a very good documentation. In this link you can find all the date-time functions.
In your case you can use this function to truncate date to minute part:
date_trunc(text, timestamp)
date_trunc('minute', date_column)
If you need the timestamp, you can cast to timestamp ::timestamp and then convert to minutes

time difference in sql Oracle

I need to know a difference between start time and end time. Both are DATETIME fields, I tried to use "-" and DATADIFF.
I already tried using DATADIFF and simple subtraction converting the field to just time.
(to_date(Fim_Hora,'HH24:MI') - to_date(Inicio_Hora,'HH24:MI')) AS Diferenca
DATADIFF(MIN,Fim_Hora,Inicio_Hora)
I need to know the time in minutes for use as parameters.
Oracle does not have a time data type. Usually, subtraction works well enough:
select (end_time - start_time) as diff
You may need to convert to a string if you want it formatted in a particular way.
In Oracle, you can directly substract dates, it returns the difference between the dates in days. To get the difference in minutes, you can multiply the result by 24 (hours per days) and 60 (minutes per hour):
(Fim_Hora - Inicio_Hora) * 24 * 60 diff_minutes
This assumes that both Fim_Hora and Inicio_Hora are of datatype DATE.

Hive SQL- Adding (not appending) seconds to an existing time stamp

I am trying to add seconds to an existing time stamp to find the end time.
I have start time in one field and total duration in one field. How could i add. i tried to use date_add function, but that dont work.
Select
testdate,
DATE_ADD ( testdate,CAST (testduration as INT) ) as Test_end_date
from <DBName> limit 10;
TestDate is this format= 9/14/2017 16:33:25.000000
Testduration is in seconds e.g: 144 seconds or so
It adds to the date rather than to the seconds component.
Any help is appreciated.
Tried Date_Add function, wont work.
Convert to seconds using unix_timestamp, add seconds
select from_unixtime(UNIX_TIMESTAMP('9/14/2017 16:33:25.000000','M/dd/yyyy HH:mm:ss.SSSSS')+144, 'yyyy-MM-dd HH:mm:ss.SSSSS')
This returns timestamp in standard Hive timestamp format, change output format if you need to keep original one: 'M/dd/yyyy HH:mm:ss.SSSSS' as a second parameter of from_unixtime() function.

PGSQL convert time to second

why i get error with this code
SELECT EXTRACT(SECOND FROM TIME total_time) from tr_empl_quiz;
and i got error to with this code
SELECT EXTRACT(EPOCH FROM TIMESTAMP total_time) from tr_empl_quiz;
this is my table content tr_empl_quiz and type data is time without time zone
total_time
============
00:01:00
00:02:00
When you use the extract() function you are getting the value of the date/time part. In your examples, the seconds are zero, so that is what you get.
Postgres does support what you want, using the perhaps unintuitive name epoch. Epoch returns the number of seconds. For an date or datetime value, this is the number since 1970-01-01 (the beginning of Unix time). For a time or interval it is the total number of seconds during the period. So:
select extract(epoch from time '00:02:00')
returns 120.
Surprisingly, the documentation for epoch doesn't mention that it works on the time data type. The functionality is entirely consistent with what the function does. Either the documentation (which is generally quite excellent) overlooks time; or time is treated as an interval.
For a column in a table, you would just do:
SELECT EXTRACT(EPOCH FROM total_time)
FROM tr_empl_quiz;
or:
SELECT EXTRACT(EPOCH FROM CAST(total_time as time))
FROM tr_empl_quiz;
Depending on what you want.