SELECT rows where seconds since epoch from NOW - sql

I am wondering is there a way to select only rows where time since epoch and "now()" is greater than a certain amount of seconds.
I store the rows with a field holding seconds since epoch created from the php function time().
So something like this:
SELECT * FROM table WHERE (now - field_time) > 60 seconds

NOW() returns a SQL DateTime and what you need is UNIX_TIMESTAMP() which returns seconds since epoch (unix timestamp) for now if you don't give any date as parameter.

Related

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.

Getting incorrect date when converting epoch to timestamp

I have a timestamp in epoch format like this: 1551187548876. If I convert this to a timestamp using https://www.epochconverter.com/, I get the correct time back.
However, if I convert it to time using PostgreSQL to_timestamp, I get the following:
select to_timestamp(1551187548876) from my_table;
51125-03-06 14:14:36+00
Question
How can I convert epoch 1551187548876 to a timestamp in PostgreSQL?
I assume that the epoch number you have is the number of milliseconds since the epoch, not the number of seconds, as customary.
Try dividing by 1000:
select to_timestamp(1551187548.876);
to_timestamp
----------------------------
2019-02-26 14:25:48.876+01
(1 row)

best way to determine if two epoch timestamps are on the same date in oracle database

I have a table that contains an epoch timestamp column(transaction_date), if I have two rows and I want to determine if they are on the same day; without converting them to date using to_date. how can I do that and what is the best way that takes less operations while determining it.
An epoch "timestamp" is a number which should be generated by converting the date to UTC (if it has a timezone) and subtracting the epoch to give a number of seconds (or milliseconds) since the epoch.
Without converting to a date, you can just divide by the number of seconds (or milli-seconds) in a day and truncate to get the number of days since the epoch and then compare those values:
SELECT TRUNC( transaction_date / ( 24 * 60 * 60 ) ) FROM your_table
If the values given by two rows have an identical number of days since the epoch then they are on the same day.
Using this transformation from timestamp to day, you can compare
SELECT
* (all but transaction_date),
(EXTRACT (DAY FROM (transaction_date))*24*60*60) transaction_day
FROM yourtable;

Query to fetch records only form previous half hour with time stamp in unix epoch format

I want SQL query to fetch/select records which are taken only from previous half an hour only. For example if my scheduler ran at 2 pm, and then again in 2:30, during the 2:30 run it should only pick rows from between 2pm and 2:30pm and not earlier, using the column created_timestamp which stores the time as unix epoch format eg:
|created_timestamp|
|1497355750350 |
|1497506182344 |
We can do arithmetic with Oracle dates. Subtracting one date from another gives the interval as a fractional number. Multiplying by 86400 gives us the number of seconds. So this is the current unix expoch:
(sysdate - date '1970-01-01') * 86400
This means your query will be something like
select * from your_table
where created_timestamp >= (:last_run_time - date '1970-01-01') * 86400
The trick is that your scheduler needs to pass in the time of the previous run - last_run_time - to pick up all the records which have been added since then.
You can do Flashback query
SELECT * FROM TABLE
AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '30' MINUTE);

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.