MySql difference between two timestamps in Seconds? - sql

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')

Related

SQL datetime into date and time

Not sure the way to break the datetime field become 2 columns weekday and time
ContactDate
2019-07-09 09:15:12.000
My query:
SELECT FORMAT(contactdate, 'ddd') AS Result FROM contact
And the outcome only week date but no time. How do we get the time? Thank you.
Result
Tue
Just use DATENAME and CONVERT:
SELECT CONVERT(varchar(3),DATENAME(WEEKDAY,ContactDate)),
CONVERT(time(3),ContactDate)
FROM dbo.Contact;
I recommend against FORMAT. Although it's a useful function, it is significantly slower than other functions; especially when dealing with large sets of data.
SELECT cast(contactdate as time) [time]

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

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)

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

SELECT average timestamp difference in JPA with Postgres and HSQL

I have a table with two timestamp columns, startTime and stopTime, and I would like to calculate the average difference of these timestamps in my table. I have a solution that works in Postgres and in HSQLDB (which I use for local unit testing) but not both, and I'm having trouble trying to figure out a common solution.
Postgres:
select EXTRACT(EPOCH FROM(avg(m.stopTime - m.startTime))) from Measures m
HSQL:
select avg(FUNC('UNIX_TIMESTAMP', m.stopTime) - FUNC('UNIX_TIMESTAMP', m.startTime) from Measures m
Is there a way to use the same query for both databases? All of the functions I've found seem to only be supported in one database or the other.
I think my main problem is that there isn't a common function to convert a timestamp to seconds in order to perform the calculation. EPOCH is only compatible with Postgres and UNIX_TIMESTAMP is only compatible with HSQL.
The crux of your problem is converting the dates and timestamps down to a number of seconds. Instead of using epoch, I'll use a julian date for the date. I'll convert the julian date to seconds, then add the number of seconds since minight for each timestamp being compared. The following query does not calculate the difference, it simply converts the date to a number that's similar on both platforms .. you'll have to do this once for each date being compared. note: replace "current"timestamp" with m.startTime and m.stopTime respectively.
select
(to_number(to_char(current_timestamp,'J'),'99999999999999999999')*86400/*convert from julian days to julian seconds*/)
+ (to_number(to_char(current_timestamp,'HH'),'99') * 3600) /*Hours to seconds */
+ (to_number(to_char(current_timestamp,'MM'),'99') * 60) /*Minutes to seconds */
+ (to_number(to_char(current_timestamp,'SS'),'99') /*add in the seconds*/
Ugly as sin, I know-- perhaps you can rewrite it easier as function, but as I don't know hsqls full feature set, I'll leave it in this form rather than using a CTE or function.