Substract the hour from the timestamp.( hour is part of the timestamp column) - hive

Need some help to perform this in Hive .
I do have timestamp like "2019-03-11T18:23:49-04:00"
How to I subtract the hour and minutes from the above timestamp.( -04:00)
The hour component may vary based on the timezone.
Thanks in advance.

Looks like you can't do this as easily as you can in MSSQL with dateadd. Some very good suggestions are made here. I also tested this and it works as it should.
select from_unixtime(unix_timestamp('2015-12-12 16:15:17')+8500)
I have modified this to work with your exact timestamp format
select from_unixtime(UNIX_TIMESTAMP(substring(translate("2019-03-11T18:23:49-04:00",'T',' '),1,19))+8500)

Related

Get information between two different times Oracle

I am making a modification to an Oracle Query, where I need to get the information found from the query date and minute up to 30 minutes ago.
For example, I made the query at 16:35, so I need it to show me the information found from 16:05 to 16:35.
I did something like this, but I don't have the result I need.
Also, how can I make it find everything loaded with current date? This is what I have done with no result
AND FV.FEC_CAR = dateadd(minute,-30,getdate()) ORDER BY F.N_FILE DESC
Thank you very much in advance
dateadd and getdate aren't valid in Oracle's SQL dialect. That looks like SQL Server syntax but it probably works in some other database as well.
In Oracle, you'd do
fv.fec_car > sysdate - interval '30' minute
or
fv.fec_car > sysdate - 30/24/60
I find the interval syntax far clearer personally
As far as I can understand and interpret, you need to see the data at a point in the past before applying some modification to your table. This case,
SELECT *
FROM tab AS OF TIMESTAMP SYSTIMESTAMP - INTERVAL '30' MINUTE
might be used to see the values of half an hour before modification if undo_retention parameter's value of your database is big enough(without forgetting that it does not guarantee to return a result even if the value is big enough)

Cannot pull correct time range

After executing this query
AND TRUNC(ITD.TRAN_DATE)>= '02-APR-18'
AND TO_CHAR(ITD.TRAN_DATE,'HH24MI')>='0600'
AND TRUNC(ITD.TRAN_DATE)<= '03-APR-18'
AND TO_CHAR(TRUNC(ITD.TRAN_DATE+1),'HH24MI')<='0030'
Everything after 6AM for April 2nd will show, but also times after 12:30AM the next day shows. What am I doing wrong here?
When we truncate a date we remove the time element. Consequently, a mask of 'HH24:MI' will return '00:00' which means that this will always be true regardless of the actual time component of ITD.TRAN_DATE:
AND TO_CHAR(TRUNC(ITD.TRAN_DATE+1),'HH24MI')<='0030'
Probably what you should do is something more straightforward, such as
where itd.tran_date >= date '2018-04-02' + (6/24)
and itd.tran_date <= date '2018-04-03' + (1/48)
Here's a SQL Fiddle demo comparing your WHERE clause with my suggestion.
Not sure what you want. Maybe this:
and to_char(itd.tran_date, 'YYYYMMDD-HH24MISS')
between '20180402-0600'
and '20180403-0030'
If so however, and if the table have many rows and tran_date is an indexed column, this might be a lot faster:
and itd.tran_date
between to_date('20180402-0600','YYYYMMDD-HH24MI')
and to_date('20180403-0030','YYYYMMDD-HH24MI')
In your question you assume the date_format to be DD-MON-YY. This might be the case now, for the environment you have today, but might not be so always. So it's good practice to use to_char or to_date on DATE values or strings.

Subtracting bigint timestamp in postgreSQL but not getting exact answer

I have two columns which have timestamps in Bigint for example "1480083350667" and I need to subtract it:
The script which i am using right now is:
TO_CHAR(TO_TIMESTAMP((employee_reply_timestamp - matched_log_timestamp)/1000), 'YYYY-MM-DD HH24:MI:SS') AS reaction_time
Where "employee_reply_timestamp" and "matched_log_timestamp" i.e. are two bigint timestamps. If i run this query i am getting the answer which looks something like this 1970-01-01 01:06:58 which is not correct.
I am looking for an answer which can give me the days, hours, minutes and
seconds difference between the two bigint timestamps.
It would be great if someone can give me the solution and thanks in advance.
you probably are looking for interval, like:
select
TO_TIMESTAMP(employee_reply_timestamp/1000)
-
TO_TIMESTAMP(matched_log_timestamp/1000)
AS reaction_time

Function to substract 2 dates in sql with PostgreSQL

I need a way to substract to dates and get the result in days in sql.
I'm using PostgreSQL 8.4
When both columns you want to subtract are of date type, you can just use the - operator.
Take a look at the documentation for date functions, in particular table 9-25.
Check here the documentation of PostgreSQl...
postgresql.org/docs/8.4
I copied it from this code from this website:
age(timestamp '2001-04-10', timestamp '1957-06-13')
hope it will help you...

MySql difference between two timestamps in Seconds?

Is it possible to calculate difference between two timestamps in Mysql and get output result in seconds? like 2010-11-29 13:16:55 - 2010-11-29 13:13:55 should give 180 seconds.
Thank you
I do not think the accepted answer is a good universal solution!
This is because the UNIX_TIMESTAMP() function fails for DATEs before 1970-01-01 (and for dates in the far future using 32 bit integers). This may happen easily for the day of birth of many living people.
A better solution is:
SELECT TIMESTAMPDIFF(SECOND, '2010-11-29 13:13:55', '2010-11-29 13:16:55')
Which can be modified to return DAY YEAR MONTH HOUR and MINUTE too!
Use the UNIX_TIMESTAMP function to convert the DATETIME into the value in seconds, starting from Jan 1st, 1970:
SELECT UNIX_TIMESTAMP('2010-11-29 13:16:55') - UNIX_TIMESTAMP('2010-11-29 13:13:55') as output
Result:
output
-------
180
An easy way to deal with if you're not sure which value is bigger than the other -- use the ABS function:
SELECT ABS(UNIX_TIMESTAMP(t.datetime_col1) - UNIX_TIMESTAMP(t.datetime_col2)) as output
TIMESTAMPDIFF method only works with datetime format. If you want the difference between just two times like '11:10:00' minus '10:20:00' then use
select TIME_TO_SEC('11:10:00')-TIME_TO_SEC('10:20:00')