This is my current oracle table:
I need a query to get RUN_DURATION between two dates with hours like
Select * from Datatable where DATE BETWEEN to_date('myStartDate', 'dd/mm/yyyy hh24:mi:ss') + HOURS? and to_date('myEndDate', 'dd/mm/yyyy hh24:mi:ss') + HOURS?
For example all data between 30.10.14 11:00:00 and 30.10.14 15:00:00
I stuck to get the hours to the dates. I tried to add the hours into myStartDate but this will be ignored for the start date because of BETWEEN
.
I know BETWEEN
shouldn't be used for dates but I can't try other opportunities because I don't know how to get DATE
and HOUR
together...
Thanks!
To calculate the difference between the timestamps in Oracle, simply subtract the start timestamp from the end timestamp (here: arrival - departure ). The resulting column will be in INTERVAL DAY TO SECOND . The first number you see is the number of whole days that passed from departure to arrival .
As stated above, the format of date and time in our table shall be yyyy:mm: dd hh:mm: ss which is implied by DATETIME2. The time is in a 24-hour format. Syntax: SELECT * FROM TABLE_NAME WHERE DATE_TIME_COLUMN BETWEEN 'STARTING_DATE_TIME' AND 'ENDING_DATE_TIME';
Well, you can add hour to your field date this way
select "DATE" + (hour / 24) from <yourTable>
this will give you ( from your first sample, may be different based on your format)
August, 14 2015 10:00:00
August, 14 2015 08:00:00
Based on that, you can do any between, select that you need.
In your case
where "DATE" + (hour / 24 )
which would make
Select *
from Datatable
where "DATE" + (hour / 24 )
BETWEEN to_date('30/10/2014 11:00:00', 'dd/mm/yyyy hh24:mi:ss') and
to_date('30/10/2014 15:00:00', 'dd/mm/yyyy hh24:mi:ss')
see SqlFiddle
(By the way, don't use reserved keywords for column name, but I guess this is just a sample).
A DATE has both date and time elements. To add hours to date, you just need to do some mathematics.
For example,
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Session altered.
SQL> SELECT SYSDATE, SYSDATE + 1/24 FROM dual;
SYSDATE SYSDATE+1/24
------------------- -------------------
2015-08-14 15:02:59 2015-08-14 16:02:59
Adds 1 hour to sysdate. So, if you have a column of date data type, just add number of hours to be added divided by 24 because you add number of days to a date. So, you just need to first convert the number of hours into date as hours/24
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With