Subtracting bigint timestamp in postgreSQL but not getting exact answer - sql

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

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)

SQL date as bigint

I have a DB with date looks like BIGINT "15321600".
It's represent 17/2/2017
How can I convert it?
edit:
I understood that the number represent the minute passed from 1/1/1988...
You really need logic to specify what the conversion is. My trial-and-error, it looks like minutes since 1988-01-01 produces your value.
That means that you can use logic like this
select date '1988-01-01' + col * interval '1' minute
Of course, date/time functions vary by database, so the exact syntax might be different in your database.

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)

Find entries in sql that were done milliseconds against each other

I need to debug an issue I found with someone else's code, where it reads both the id and the timestamp for a key. But, I noticed that it is not reading the millisecond information. While this is a potential cause, this not confirmed, and I need to find out if this is the cause.
The problem could occur if two entries in the table happened within the same second, 10:20:05.0500 pm and 10:20:05.5000 pm, but not 10:20:05.5000 and 10:20:06.0500.
How do I write such a query to look for it?
I am using Oracle pl/sql.
In Oracle to use fractional seconds a column must be of data type "TIMESTAMP".
Maybe someone else's code is using a variable of type DATE, which is year-month-day hour-minute-second without fractional seconds.
Database SQL Language Reference: Data Types
Can you give the table description and someone else's code?
To find records with the same date/time up to second precision but with different milliseconds, you can compare different records by joining the table to itself
SELECT A.ts, B.ts
FROM
Test A
INNER JOIN Test B
ON TO_CHAR(A.ts, 'YYYY-MM-DD HH24:MI:SS') = TO_CHAR(B.ts, 'YYYY-MM-DD HH24:MI:SS')
WHERE
A.ts < B.ts
ORDER BY
A.ts, B.ts;
TO_CHAR truncates the milliseconds. This is important, because functions that round could yield different seconds. E.g., CAST(ts as timestamp(0)) rounds, which is not what wee need.
The example from the link below has a record with 999 milliseconds to test this.
See example on SQL Fiddle.

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