sysdate difference ( how to make query between sysdate and my table date) - oracle

I am new to oracle.
I have some problems to make query.
Im trying to make query that solves the difference between sysdate() and the date from my own table
select to_char(to_char(sysdate,'YYYYMMDDHH24MISS')
- to_char(S_DATE||S_HOUR||':00' , 'YYYYMMDDHH24MISS'))
from dual;
I'm doing like that.
In my table, I have two columns 'S_DATE' and 'S_HOUR' that means time.
So, I would like to know the time difference and how to make this query.
Thank you in advance.

You can get difference between two dates by simple - (minus) operator. However first you need to convert date string to date using TO_DATE.
Sample in SQL Fidlle is here
The sample query:
select
sysdate , S_DATE || ' ' || S_HOUR "Date",
round((sysdate - to_date(s_date || s_hour ,' YYYY/MM/DDHH24:MI') ) * 24 * 60, 2) "Dif In Min",
round((sysdate - to_date(s_date || s_hour ,' YYYY/MM/DDHH24:MI') ) * 24 * 60 * 60, 2) "Dif In Sec"
from myDate

Related

Display all the rides for which the travel duration is more than 2 hours

Sample output:
Rise_ID FROM_LOCATION TO_LOCATION SEATS_LEFT
SEATS_TOTAL RIDE_PROVIDER START_ON ENDS _ON IS_STARTED IS_FINISHED 12014 Nandi hills banglore 0 3 11002 13-Nov-20
04.00.13.36 PM. 13-Dec-20 08.02.13.36PM yes yes
one way you can get the difference between the 2 date columns as follows.
If the following SQL SYSDATE is getting the current time, the other column is a hard coded date time column.
SELECT ROUND(minutes_ / 60, 2) || ' Hours '
FROM ( SELECT (sysdate - to_date('14/09/2022 19:35:00', 'DD/MM/YYYY HH24:MI:SS')) * 24 * 60 AS minutes_
FROM dual
);
SELECT FROM_LOCATION || ' - ' || TO_LOCATION
FROM TABLE_OUTPUT T
WHERE (TO_DATE(START_ON, 'DD-MON-YY HH.MI.SS') - TO_DATE(ENDS_ON, 'DD-MON-YY HH.MI.SS')) > 2;
I assuming you are looking this data from a table, also you need to check the format, the current you're using not gonna work, i suggest the format 'DD-MON-YY HH.MI.SS'

Difference between two dates and getting result in timestamp

I´m trying to calculate the difference between two dates in Oracle and getting the result as a TimeStamp. This is the easiest thing to do in SQL Server, but it seems that Oracle does not have a easy way to solve this. I refuse to believe that I have to write that much code to get what I need. Can someone tell me if there is a easier way to get that difference?:
SELECT TO_CHAR(EXTRACT(HOUR FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
|| ':' ||
TO_CHAR(EXTRACT(MINUTE FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
|| ':' ||
TO_CHAR(EXTRACT(SECOND FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
I need the result be something like:
enddate = '2017-03-01 17:30:00'
startdate = '2017-03-01 10:00:00'
difference: 07:30:00
Substract the two dates. Add the result to the current date (without any time component, trunc(sysdate)) and show only the time.
select to_char(trunc(sysdate) + (to_date('2017-03-01 17:30:00', 'YYYY-MM-DD HH24:MI:SS') -
to_date('2017-03-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'))
,'HH24:MI:SS')
from dual

Get diff of time Oracle

I have the next idea :
SELECT TO_CHAR('14:00:00','HH24:MI:SS') - MIN(TO_CHAR(DATETIME,'HH24:MI:SS')) AS MINFECHA
FROM ARCHIVO2
WHERE DIA='LUNES';
I want to get the difference between the 2 fields that should be something like
00:46:00
Any comment will be appreciated.
You can't add/subtract dates and times when they're in character strings. To accomplish what you're trying to do you need to convert the character strings to DATE values, perform the necessary calculations, and then convert the result back to a character string:
WITH DATE_DATA AS
(SELECT DIA,
DATETIME,
TO_DATE(TO_CHAR(DATETIME, 'DD-MON-YYYY') || ' ' || '14:00:00', 'DD-MON-YYYY HH24:MI:SS') AS BASE_TIME
FROM ARCHIVO2)
SELECT DIA,
DATETIME,
BASE_TIME,
(DATETIME - BASE_TIME) * 1440 AS MINUTES_LATE
FROM DATE_DATA;
SQLFiddle here
Best of luck.

PLSQL Date Filter Error

I have a table with data since 2012. I need to get data in this table from 31 days back from given date. so i wrote below query to get data.
select ('[' || work_date || '] ' || field_name || ' - ' ||work_desc) d
from DAILY_WORK
where TO_CHAR(work_date,'DD/MM/YYYY') >= TO_CHAR(to_date('30-Jan-13','dd-MON-yyyy') - (31),'DD/MM/YYYY')
order by work_date desc
When i execute this query, it returns data in below dates only.
31-AUG-12
31-OCT-12
30-DEC-12
31-DEC-12
But actually i need to get data from 2012-12-30 to 2013-01-30.
How could i do this ?
use this:
select ('[' || to_char(work_date, 'dd-MON-yyyy') || '] ' || field_name || ' - ' ||work_desc) d
from DAILY_WORK
where work_date >= to_date('30-Jan-2013','dd-MON-yyyy') - 31
order by work_date desc
You can use the below query:
select * from DAILY_WORK where work_date >= TRUNC(to_date('01-30-2013','MM/DD/YYYY')) - 31
Check date format which tool you are using As per tool i am using date format is 'MM/DD/YYYY'
This query give correct result.You can check.

Oracle : how to subtract two dates and get minutes of the result

I wrote this function to get minutes from a date, but I cannot get minutes between two dates, How to get that ?
FUNCTION get_minute(p_date DATE)
RETURN NUMBER
IS
BEGIN
IF p_date IS NOT NULL THEN
return EXTRACT(MINUTE FROM TO_TIMESTAMP(to_char(p_date,'DD-MON-YYYY HH:MI:SS'),'DD-MON-YYYY HH24:MI:SS'));
ELSE
RETURN 0;
END IF;
END get_minute;
When you subtract two dates in Oracle, you get the number of days between the two values. So you just have to multiply to get the result in minutes instead:
SELECT (date2 - date1) * 24 * 60 AS minutesBetween
FROM ...
For those who want to substrat two timestamps (instead of dates), there is a similar solution:
SELECT ( CAST( date2 AS DATE ) - CAST( date1 AS DATE ) ) * 1440 AS minutesInBetween
FROM ...
or
SELECT ( CAST( date2 AS DATE ) - CAST( date1 AS DATE ) ) * 86400 AS secondsInBetween
FROM ...
I think you can adapt the function to substract the two timestamps:
return EXTRACT(MINUTE FROM
TO_TIMESTAMP(to_char(p_date1,'DD-MON-YYYY HH:MI:SS'),'DD-MON-YYYY HH24:MI:SS')
-
TO_TIMESTAMP(to_char(p_date2,'DD-MON-YYYY HH:MI:SS'),'DD-MON-YYYY HH24:MI:SS')
);
I think you could simplify it by just using CAST(p_date as TIMESTAMP).
return EXTRACT(MINUTE FROM cast(p_date1 as TIMESTAMP) - cast(p_date2 as TIMESTAMP));
Remember dates and timestamps are big ugly numbers inside Oracle, not what we see in the screen; we don't need to tell him how to read them.
Also remember timestamps can have a timezone defined; not in this case.
I can handle this way:
select to_number(to_char(sysdate,'MI')) - to_number(to_char(*YOUR_DATA_VALUE*,'MI')),max(exp_time) from ...
Or
if you want to the hour just change the MI;
select to_number(to_char(sysdate,'HH24')) - to_number(to_char(*YOUR_DATA_VALUE*,'HH24')),max(exp_time) from ...
the others don't work for me.
Good luck.

Resources