Difference between two dates and getting result in timestamp - oracle

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

Related

How do I separate the time and date in SQL navigator?

I am trying to separate the time and date in one column to be independent off each other. I am new at writing scripts
this is my query:
select
*
from
[tablename]
where
to_date([column_name]) in ( '15-Jun-2021', '16-Jun-2021' )
and
to_char([column_name],'dd-Mon-yyyy HH:MM:ss') < '15-Jun-2021 19:54:30'
The way you put it, it would be
select *
from your_table
where date_column >= date '2021-06-15'
and date_column < to_date('15.06.2021 19:54:30', 'dd.mm.yyyy hh24:mi:ss')
because
date_column should be of date datatype. If it isn't, you'll have problems of many kinds in the future. Therefore,
don't to_date it, it is already a date
don't to_char it either, because you'd be comparing strings and get unexpected result. Use that function when you want to nicely display the result
the second condition you wrote makes the first one questionable. If date_column is less than value you wrote, then you can omit date '2021-06-16' from the first condition because you won't get any rows for that date anyway
date literal (date '2021-06-15') sets time to midnight, so condition I wrote should return rows you want
SQL> select date '2021-06-15' first,
2 to_date('15.06.2021 19:54:30', 'dd.mm.yyyy hh24:mi:ss') second
3 from dual;
FIRST SECOND
------------------- -------------------
15.06.2021 00:00:00 15.06.2021 19:54:30
SQL>

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.

ORA-01849: hour must be between 1 and 12

WHERE (ResTRRequest.RequestTime BETWEEN TO_CHAR(TRUNC(TO_DATE('2012-12-01 20:10:10', 'HH')), 'YYYY-MM-DD HH24:MI:SS')
AND TO_CHAR(TRUNC(CURRENT_TIMESTAMP, 'HH') + INTERVAL '59:59' MINUTE TO SECOND, 'YYYY-MM-DD HH24:MI:SS'))
I have above where condition in query when i execute it,it gives me hours must be between 1 to 12 due to static date I have given i.e ''2012-12-01 20:10:10', 'HH')' if I put sysdate the its working fine but due to static date it gives me error.
Let's break this down a bit:
WHERE (ResTRRequest.RequestTime
BETWEEN TO_CHAR(TRUNC(TO_DATE('2012-12-01 20:10:10', 'HH')), 'YYYY-MM-DD HH24:MI:SS')
AND TO_CHAR(TRUNC(CURRENT_TIMESTAMP, 'HH') + INTERVAL '59:59' MINUTE TO SECOND, 'YYYY-MM-DD HH24:MI:SS'))
In the first place, I don't think you mean this: TRUNC(TO_DATE('2012-12-01 20:10:10', 'HH')), I think maybe you mean this: TRUNC(TO_DATE('2012-12-01 20:10:10'), 'HH'). The number from 1-12 error comes from the fact that you have an hour of 20 and are trying to convert it into a date with the mask of HH. But as I said I think that's a typo. You can also use a TIMESTAMP literal here rather than TO_DATE():
TRUNC(TIMESTAMP'2012-12-01 20:10:10', 'HH')
Second, and just to get this out of the way, are you storing dates or timestamps as strings? That's not a good idea.
Third, it's not a good idea to use BETWEEN in date comparisons because you can miss the edge cases. It might be better to rewrite this as follows:
WHERE ( ResTRRequest.RequestTime >= TO_CHAR(TRUNC(TO_DATE('2012-12-01 20:10:10'), 'HH'), 'YYYY-MM-DD HH24:MI:SS')
AND ResTRRequest.RequestTime < TO_CHAR(TRUNC(CURRENT_TIMESTAMP, 'HH') + INTERVAL '1' HOUR, 'YYYY-MM-DD HH24:MI:SS') )
Problem is in mask:
TO_DATE('2012-12-01 20:10:10', 'HH')
Replace with this one:
TO_DATE('2012-12-01 20:10:10', 'HH24')
Assuming that ResTRRequest.RequestTime is of a date type, this Where clause will work:
where ResTRRequest.RequestTime
BETWEEN TRUNC(TO_DATE('2015-02-26 20:10:10', 'YYYY-MM-DD HH24:MI:SS'), 'HH')
AND TRUNC(CURRENT_TIMESTAMP, 'HH') + INTERVAL '59:59' MINUTE TO SECOND
If you have to compare character representations, keep in mind that you compare in lexicographic order, meaning that prefixes of strings are sorted before their strings! Avoid ensueing complications by using identical formatting models with componnents arranged in the order of decreasing significance. E.g.
TO_CHAR(<whatever>, 'YYYY-MM-DD HH24:MI:SS')
but not
TO_CHAR(<whatever>, 'MM/DD/YYYY HH24:MI:SS')
If the language setting on Oracle is set for using the 12 hours time, this problem will occur when converting the 24 hours time format.
There are two solutions to this :
Convert TIMESTAMP/DATE format in Oracle client
alter session set nls_timestamp_format='YYYY-MM-DD HH24:MI:SS.FF6';
Convert query to match 24hr format
SELECT * FROM TEST_ WHERE DOB > TRUNC(TIMESTAMP'1970-01-01 20:10:10', 'HH');
or
SELECT * FROM TEST_ WHERE DOB > to_date('1970-01-01 20:00:00','YYYY-MM-DD HH24:MI:SS');
One more thing to watch out for in the case you get this error is the data itself.
I've had date stored in xml tag that I had to parse and convert with TO_DATE with this format specifier 'MM/dd/YYYY HH:MI:SS AM'. SQL broke with "ORA-01849: hour must be between 1 and 12" because some records were written like this: "12/20/2017 16:45:00 PM". Pay attention to 16h and PM specfier...

Get days between tow dates

Have a date in the table with format 10/04/14
then use this to_char(nameofcolumn, 'yyyymmdd')) to get this
20140410
Now, need rest a this value the sydate
I use this
select nameofcolumn,
to_char(nameofcolumn, 'yyyymmdd')) - to_char(sysdate, 'yyyymmdd') AS days
from
table;
But te result for example 07/07/14 return 300 days when is 90
Why are you converting these into strings? You can perform basic date arithmetic without converting them:
select nameofcolumn,
nameofcolumn - sysdate AS days
from
table;
I can't add comments so i will post a little addition as a separate answer.
SELECT to_date('07/07/2014','dd/mm/yyyy') - SYSDATE AS days FROM dual;
would return a number with a floating point instead of an integer. You've got to do either
SELECT trunc(to_date('07/07/2014','dd/mm/yyyy') - SYSDATE) AS days FROM dual;
or
SELECT to_date('07/07/2014','dd/mm/yyyy') - trunc(SYSDATE) AS days FROM dual;
depeding on what exactly you want to achieve.

DateTime Comparison in Oracle

I am doing comparison between several dates in my stored procedure.
TODAY := TO_DATE(TO_CHAR(SYSDATE, 'DD-MON-YYYY') ||
' 09:00:00', 'DD-MON-YYYY HH24:MI:SS');
IF PREVIOUS_DATE < TODAY AND
TO_DATE(CURRENT_DATE, 'DD-MON-YYYY HH24:MI:SS') >= TODAY THEN
-- do something
ELSE
-- do something else
When I set CURRENT_DATE = SYSDATE, it did not get into the IF part. Can someone tell me where did i do wrong?
"CURRENT_DATE is of type VARCHAR2 "
Well that scuppers my first idea 8-)
But I think the problem is in your use of CURRENT_DATE. Assuming that is the Oracle function there are two potential problems:
CURRENT_DATE is a DATE datatype and so has a time element. There's no point in casting it to a date.
CURRENT_DATE returns a value adjusted for the system timezone. Which is presumably the point of your IF test. But do you know what the values are?
But it remains a straightforward debugging task. Check your values to understand what's happening. Here is an example using DBMS_OUTPUT (AKA The Devil's Debugger) because I don't know if you're working in an environment with better tools.
TODAY := trunc(sysdate) + 9/24;
dbms_output.put_line('TODAY = '||to_char(today, 'DD-MON-YYYY HH24:MI:SS'));
dbms_output.put_line('PREVIOUS_DATE = '||to_char(previous_date, 'DD-MON-YYYY HH24:MI:SS'));
dbms_output.put_line('CURRENT_DATE = '||current_date);
IF PREVIOUS_DATE < TODAY AND
to_date(CURRENT_DATE, 'DD-MON-YYYY HH24:MI:SS') >= TODAY
THEN
....
Might as well simplify your code at the same time.
Remember, to see output from DBMS_OUTPUT.PUT_LINE you need to SET SERVEROUTPUT ON for whatever client you're using.
Incidentally, it is bad practice to declare variables which share the same name as Oracle built-ins. This is why it is a good idea to add a scoping prefix (l_ for local, p_ for parameter, etc) to our declarations. We don't have to do the full Hungarian to get a lot of benefit.

Resources