How to find date diff in years in 'YYYY-MM-DD' format .
I am using below two querues:
SELECT TRUNC(TO_NUMBER(SYSDATE - TO_DATE('1994-08-13')) / 365.25) AS AGE FROM DUAL;
ORA-01861: literal does not match format string
SELECT (TO_DATE(SYSDATE, 'YYYY-MM-DD') - TO_NUMBER('1994-08-13', 'YYYY-MM-DD')) FROM DUAL;
O/P-: -727738
Desired o/p: 26
I suggest to use "months_between" function because it takes leap years into account (months_between wants 2 dates as parameters):
select months_between(sysdate, to_date('1994-08-13', 'YYYY-MM-DD'))/12 from dual;
26,4729751904122
of course, if you need to truncate:
select trunc(months_between(sysdate, to_date('1994-08-13', 'YYYY-MM-DD'))/12) from dual;
26
Just subtract one date from the other:
sysdate - date '1994-08-13'
Or
sysdate - to_date('1994-08-13', 'yyyy-mm-dd')
returns the number of days between the two dates.
So rewrite your first query to:
select (sysdate - date '1994-08-13') / 365.25 as age
from dual
So, extract years from these dates and subtract them:
SQL> select extract (year from sysdate) - extract(year from date '1994-08-13') diff
2 from dual;
DIFF
----------
27
SQL>
Because,
SQL> select 2021 - 1994 diff from dual;
DIFF
----------
27
SQL>
Related
I have a query that calculates the difference between two dates and returns a decimal date. I would just like to extract days and hours from the final calculated date.
This is my query.
select sysdate - (to_date('24/AUG/2021 14:00:00', 'DD/MON/YYYY HH24:MI:SS')) as FinalDate from dual;
FinalDate
162.013252314814814814814814814814814815
How do I get my desired output:?
Desired output
| Days | Hours |
| -------- | ------|
|162 |0.24 |
A little bit of arithmetic.
SQL> with temp (finaldate) as
2 (select sysdate - (to_date('24/AUG/2021 14:00:00', 'DD/MON/YYYY HH24:MI:SS')) from dual)
3 select trunc(finaldate) as days,
4 round((finaldate - trunc(finaldate)) * 24, 2) as hours
5 from temp;
DAYS HOURS
---------- ----------
162 6,49
SQL>
Why your and my hours don't match? Because of time difference; it's
SQL> select sysdate from dual;
SYSDATE
-------------------
02.02.2022 20:30:04
SQL>
over here.
This question already has an answer here:
Trying to export a Oracle via PL/SQL gives a date of 0000-00-00
(1 answer)
Closed 1 year ago.
Duplicate of Trying to export a Oracle via PL/SQL gives a date of 0000-00-00 as mentioned by #AlexPoole
I have a table with different dates in it, however, when I try to extract year, a single row returns an incorrect result.
This incorrect result happens when I execute the following (note that TO_DATE and FROM_DATE are both of data_type DATE):
select
TO_DATE
,EXTRACT(YEAR FROM TO_DATE) as "TO_YEAR"
,EXTRACT(MONTH FROM TO_DATE) as "TO_MONTH"
,EXTRACT(DAY FROM TO_DATE) as "TO_DAY"
,FROM_DATE
,EXTRACT(YEAR FROM FROM_DATE) as "FROM_YEAR"
,EXTRACT(MONTH FROM FROM_DATE) as "FROM_MONTH"
,EXTRACT(DAY FROM FROM_DATE) as "FROM_DAY"
,DUMP(FROM_DATE, 1016) as FROM_DUMP
,to_char(FROM_DATE, 'SYYYY-MM-DD HH24:MI:SS') FROM_STRING
from SomeTable
The incorrect result is (date format is YY-MM-DD):
TO_DATE TO_YEAR TO_MONTH TO_DAY FROM_DAT FROM_YEAR FROM_MONTH FROM_DAY FROM_DUMP FROM_STRING
-------- ---------- ---------- ---------- -------- ---------- ---------- ---------- ----------------------------- --------------------
00-02-01 2000 2 1 01-02-01 0 2 1 Typ=12 Len=7: 64,64,2,1,1,1,1 00000-00-00 00:00:00
My question is why does FROM_YEAR return a zero and not 2001?
A DATE is stored in 7-bytes using:
century + 100
year-of-century + 100
month + 0
day + 0
hour + 1
minute + 1
second + 1
Looking at the output of DUMP, which is Typ=12 Len=7: 64,64,2,1,1,1,1 then you have:
Century = -36
Year-of-century = -36
Month = February
Day = 1
Hour = 0
Minute = 0
Year = 0
Which would make your date midnight of 1st February 3636 BC.
Unless you intended to use dates from ancient history then it would suggest that somewhere in your application some corrupted data has been stored.
However, something else appears to be going on as that is a valid date that can be stored and TO_CHAR should work.
CREATE TABLE table_name ( dt ) AS
SELECT DATE '-3636-02-01'FROM DUAL;
SELECT TO_CHAR( dt, 'SYYYY-MM-DD HH24:MI:SS' ) AS dt_string,
DUMP( dt )
FROM table_name;
Outputs:
DT_STRING
DUMP(DT)
-3636-02-01 00:00:00
Typ=12 Len=7: 64,64,2,1,1,1,1
db<>fiddle here
and the DUMP matches your data but the TO_CHAR output is valid whereas yours is zeros.
SELECT TO_CHAR(TO_DATE(Column_d, 'dd/mm/yyyy'), 'YYYY-MM') as Date_y FROM Table_d
Even want to get quarter from the same date which is again giving me the error ORA-01848 like
SELECT (CASE WHEN (TO_CHAR(TO_DATE(Column_d, 'dd/mm/yyyy'), 'mm')) IN ('01','02','03') THEN 'Q1'
WHEN (TO_CHAR(TO_DATE(Column_d, 'dd/mm/yyyy'), 'mm')) IN ('04','05','06') THEN 'Q2'
WHEN (TO_CHAR(TO_DATE(Column_d, 'dd/mm/yyyy'), 'mm')) IN ('07','08','09') THEN 'Q3'
ELSE 'Q4' END as QTR
FROM Table_d
The way I see it, nothing of what you posted returns ORA-01848. It is - usually, if not always - related to the DDD date format mask which represents day number within the year.
For example, this is correct, as day number 001 equals the 1st of January:
SQL> select to_date('001-2020', 'ddd-yyyy') from dual;
TO_DATE('0
----------
01.01.2020
This returns ORA-01848 as there's no day 405 in this (nor any) year:
SQL> select to_date('405-2020', 'ddd-yyyy') from dual;
select to_date('405-2020', 'ddd-yyyy') from dual
*
ERROR at line 1:
ORA-01848: day of year must be between 1 and 365 (366 for leap year)
So: if you're doing anything like this, make sure that stored values are correct; I presume that not all of them are.
Besides, that's what happens when people store dates as strings into VARCHAR2 (instead of DATE datatype) columns. I'm not saying that you're the cause, but you certainly are a victim here.
[EDIT]
Julian date, eh? In a comment, you said that you used such a query:
Select TO_CHAR(TO_DATE(Julian_dateColumn+1900000, 'YYDDD'), 'YYDDD'), 'DD/MM/YYYY') as column_d from sometable
I doubt it as it is invalid (I suppose you have superfluous , 'YYDDD')).
Anyway, I have no idea why you tried to "convert" Julian date in such a manner. There's a simple and correct way to do so. Here's how:
This is today's date (21.05.2020) presented as Julian date:
SQL> select to_char(sysdate, 'j') julian from dual;
JULIAN
-------
2458991
That's kind of values you have stored in the table. In order to convert it to format you wanted (yyddd), you'd
SQL> select to_char(to_date(2458991, 'j'), 'yyddd') as column_d from dual;
COLUM
-----
20142
I suggest you use this instead of code you currently have, i.e.
SQL> create or replace view table_d as
2 select to_char(to_date(julian_datecolumn, 'j'), 'yyddd') as column_d
3 from sometable;
View created.
SQL> select * From table_d;
COLUM
-----
20142
SQL>
I have a table abc as:
-- start_time |end_time | total_time_taken
-- 27.05.2020 00:52:48 |27.05.2020 02:08:33 |
I want to set the value of total_time_taken as the difference of end_time-start_time. in the format "HH:MM:SS".I searched the similar topic but didnot find the exact answer.
My expected output is like : 01:44:12 (HH:MM:SS)
So,i tried :
SELECT To_Char(end_time,'HH24:MM:SS'),To_Char(start_time,'HH24:MM:SS'),
To_Char(end_time,'HH24:MM:SS')-To_Char(start_time,'HH24:MM:SS') FROM abc;
The datatypes of start_time,end_time,total_time_taken is DATE.Please help me to find the solution.
If you cast those dates as timestamps, you can easily subtract them and see relatively nice result:
SQL> with test (st, et) as
2 (select to_date('27.05.2020 00:52:48', 'dd.mm.yyyy hh24:mi:ss'),
3 to_date('27.05.2020 02:08:33', 'dd.mm.yyyy hh24:mi:ss')
4 from dual
5 )
6 select cast(et as timestamp) - cast(st as timestamp) diff
7 from test;
DIFF
--------------------------------------------------------------------------
+000000000 01:15:45.000000
SQL>
If you want to format it as you wanted (note that mm format mask is for months; mi is for minutes), then you could do some extracting - again from timestamp (won't work for date):
SQL> with test (st, et) as
2 (select to_date('27.05.2020 00:52:48', 'dd.mm.yyyy hh24:mi:ss'),
3 to_date('27.05.2020 02:08:33', 'dd.mm.yyyy hh24:mi:ss')
4 from dual
5 ),
6 diff as
7 (select cast(et as timestamp) - cast(st as timestamp) diff
8 from test
9 )
10 select extract(hour from diff) ||':'||
11 extract(minute from diff) ||':'||
12 extract(second from diff) diff
13 from diff;
DIFF
-------------------------------------------------------------------------
1:15:45
SQL>
You can further make it pretty (e.g. two digits for hours, using LPAD function). Or, you can even write your own function which will actually work on difference of DATE datatype values, do some calculations (using trunc function, subtractions, whatnot), but the above looks pretty elegant if compared to a home-made function.
The answer by Littlefoot is perfectly fine. This answer is just to show there is more than one way to get the result.
First, we can subtract one date from another and get the difference in days, then convert that difference to an interval.
with test (st, et) as
(select to_date('27.05.2020 00:52:48', 'dd.mm.yyyy hh24:mi:ss'),
to_date('27.05.2020 02:08:33', 'dd.mm.yyyy hh24:mi:ss')
from dual
)
select numtodsinterval(et-st, 'day') diff
from test;
Then, since we can't control interval formatting directly, we can add DIFF to an arbitrary date and then use built-in date formatting.
with test (st, et) as
(select to_date('27.05.2020 00:52:48', 'dd.mm.yyyy hh24:mi:ss'),
to_date('27.05.2020 02:08:33', 'dd.mm.yyyy hh24:mi:ss')
from dual
)
select to_char(date '1-1-1' + numtodsinterval(et-st, 'day'), 'hh24:mi:ss') diff
from test;
DIFF
--------
01:15:45
I have a date field
DATE = 10/10/2010
sum = 4 (this are number of years by calculation)
is there a way to add four years to 10/10/2010 and make it
10/10/2014?
Try adding months (12 * number of years) instead. Like this-
add_months(date'2010-10-10', 48)
Use add_months
Example:
SELECT add_months( to_date('10-OCT-2010'), 48 ) FROM DUAL;
Warning
add_months, returns the last day of the resulting month if you input the last day of a month to begin with.
So add_months(to_date('28-feb-2011'),12) will return 29-feb-2012 as a result.
I believe you could use the ADD_MONTHS() function. 4 years is 48 months, so:
add_months(DATE,48)
Here is some information on using the function:
http://www.techonthenet.com/oracle/functions/add_months.php
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1157035034361
You can try this:
someDate + interval '4' year
INTERVAL
I am not sure, if I understood Your question correctly, but
select add_months(someDate, numberOfYears * 12) from dual
might do the trick
One more option apart from ADD_MONTHS
SELECT
SYSDATE,
SYSDATE
+ TO_YMINTERVAL ( '1-0' )
FROM
DUAL;
SYSDATE SYSDATE+TO_YMINTERVAL('1-0')
--------- ----------------------------
29-OCT-13 29-OCT-14
1 row selected.
SELECT
SYSDATE,
SYSDATE
+ TO_YMINTERVAL ( '2-0' )
FROM
DUAL;
SYSDATE SYSDATE+TO_YMINTERVAL('2-0')
--------- ----------------------------
29-OCT-13 29-OCT-15
1 row selected.
SELECT
TO_DATE ( '29-FEB-2004',
'DD-MON-YYYY' )
+ TO_YMINTERVAL ( '1-0' )
FROM
DUAL
*
Error at line 4
ORA-01839: date not valid for month specified
But the last one is illegal since there is no 29th day of February in 2005, hence it fails on leap year cases (Feb 29)
Read the documentation for the same
SELECT TO_CHAR(SYSDATE,'YYYY')-2 ANO FROM DUAL