Working out elapsed time from two columns in ORACLE SQL [duplicate] - sql

This question already has answers here:
Calculate difference between 2 date / times in Oracle SQL
(21 answers)
Closed 8 years ago.
I have two rows stored in a table called client_booking. These are called start_time and end_time. I need to create a text box in ORACLE APEX which generates the elapsed time through an SQL query.

Time difference being in days, let us multiply it by no of hours 24 and no of minutes 60. Assuming they are of DATE or TIMESTAMP datatypes
select (
to_date('01-01-2014 '|| end_time ||'00','DD-MM-YYYY HHMISS') -
to_date('01-01-2014 '|| start_time||'00','DD-MM-YYYY HHMISS')
) * 24 * 60 as time_elapsed_in_mins
from your_table;

Related

Oracle select statement where date is greater than 30 days [duplicate]

This question already has an answer here:
Oracle SQL Where clause to find date records older than 30 days
(1 answer)
Closed 2 years ago.
I want to get an ORACLE statement which returns rows that are older than 30 days from the the date of creation.
My table has a field "date_entered" that contains the date and the time it was inserted in the database .
Thanks,
your select statement
WHERE date_entered < TRUNC(SYSDATE)-30

Number of days between two columns SQL [duplicate]

This question already has answers here:
Postgresql difference between two columns dates?
(3 answers)
Get month,days difference between two date columns
(1 answer)
Date column arithmetic in PostgreSQL query
(1 answer)
how to calculate only days between two dates in postgres sql query .
(2 answers)
Closed 4 years ago.
I was looking for the answer to this question, and I found here that DATEDIFF() function is a way to do it when you have the dates. But, what if you don't have specific dates?
What if you want to find the interval of time between the date in 2 columns (rental_date, return_date).
I tried to use DATEDIFF() function, but it looks like you need the actual date.
You can use date_part for finding differences between two dates in the PostgreSQL.
The syntax is like as follow.
DATE_PART('day', enddate - startdate);
You can get years, months, weeks, hours, minutes seconds as well. You can check more about the usage of date different and date part function in PostgreSQL by clicking on this link.
if your two column data type is date then you could do subtraction
select '2018-12-10'::date - '2018-11-18'::date
so in your case it would be
with t1 as
(
select '2018-12-10'::date as rental_date, '2018-11-18'::date as return_date
)
select rental_date-return_date from t1

How to do Query in Postgres SQL based on time [duplicate]

This question already has answers here:
How to add a variable number of hours to a date in PostgreSQL?
(5 answers)
Closed 8 years ago.
I would like to do a query in postgres which should bring count of records that updated previous one hour
For example 9AM-10AM records updated is 100 means I should get 100.
10AM-11AM records update is 200 means I should get 200(not 300).
Sample time stamp in updated column 2011-02-03 09:00:00
I have tried like this
select count(*) from customer where updated>=now()-1 and updated<now()
I know now()-1 will take yesterday. I dont know how to minus one hour from now
This should be doable using the date/time related functions and operations provided by Postgres:
where updated between
(date_trunc('hour',current_timestamp) - interval '1 hour')
and
(date_trunc('hour',current_timestamp))
Explanation:
date_trunc('hour',current_timestamp)
Gives the current time with minutes, seconds and milliseconds set to 0, and
- interval '1 hour'
subtracts one hour from a given timestamp.
Combine those to create the range you want ( between )

Compare a datetime field within 2 hours [duplicate]

This question already has answers here:
SQL: Get records created in time range for specific dates
(3 answers)
Closed 8 years ago.
I have a table with the field ENTERED_ON (with both date and time value). I want to write a query that return records that have ENTERED_ON value that is past 2 hours comparing to current date time.
For example, if entered_on is 2014-05-06 11:00AM, and currently it's 2014-05-06 2:00PM, I would like to return all records that past the 2 hours when comparing to current date time.
You can write query using INTERVAL. It will looks like
SELECT * FROM Table WHERE `ENTERED_ON` > DATE_ADD(NOW(), INTERVAL -2 HOUR)

ORACLE calculate hours between two timestamps [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calculate difference between 2 date / times in Oracle SQL
so i am creating a database for an airline as part of my university coursework. first time using oracle.
I am trying to calculate the hours between two timestamps.
so something like
v_interval = new.departure_time - v_arrival_time;
v_days = extract (day . v_interval);
v_hours = extract (hours, v_interval) + (v_days * 24);
how do you guys think i should do it?
Similar question:
Calculate difference between 2 date / times in Oracle SQL
More examples:
Date / Time Arithmetic with Oracle 9/10