Oracle: 12c
Description: Trying to add 99 years to a date using add_months, which works however it adds an additional day to the return date value. How should i properly add years to a date?
declare
toRetDate DATE;
inputDate DATE;
numYears number;
begin
numYears := 99;
inputDate := TO_DATE('28-FEB-85', 'DD-Mon-YY' );
toRetDate := add_months(inputDate, numYears*12);
DBMS_OUTPUT.put_line(toRetDate);
end;
Output:29-FEB-84
ADD_MONTHS does not add a day to the result. Rather, it has this very general feature: if you ADD MONTHS to a date, and that date is the end of the month in that particular month/year, then the result of ADD_MONTHS is the last day of the resulting month.
So, for example, if you add one month to January 31, you always get February 28 (or Feb 29 in a leap year). If you add one month to June 30, you get July 31.
If you add a month to February 28, you get March 31 UNLESS it's in a leap year; in a leap year Feb 28 is not the last day of the month, so if you add a month to it you get March 28.
In all cases, if the result is a day number greater than the number of days in the resulting month, the result is simply the last day of that month.
EDIT: In a comment below this Answer, the OP asks if there is a way to keep the exact same day of the month, with the exception of Feb. 29 (which should become Feb. 28 in a non-leap year).
The answer is YES. As #Xing shows in the other answer, adding interval '99' year will preserve the date, but it may cause trouble when the input date is Feb. 29 and the resulting year is not a leap year. That can be fixed with a preliminary check. It can be done with an IF statement or with a CASE expression. I prefer the latter because it works the same in PL/SQL and in SQL; there is no IF in plain SQL.
toretdate := case when to_char(inputdate, 'mm-dd') = '02-29'
then (inputdate + 1) + interval '99' year - 1
else inputdate + interval '99' year end
The trick is, when the inputdate is 29 February, push it forward one day to make it March 1, then add 99 years (still March 1), and then subtract one day to make it either February 29 (if it's a leap year) or February 28 (otherwise).
You can use this way:
declare
toRetDate DATE;
inputDate DATE;
numYears number;
begin
numYears := 99;
inputDate := TO_DATE('28-FEB-1985', 'DD-Mon-YYYY' );
toRetDate := inputDate + INTERVAL '99' YEAR; --add_months(inputDate, numYears*12);
DBMS_OUTPUT.put_line(to_date(toRetDate,'dd-mon-yyyy'));
end;
Although it's worth noting that the arithmetics with intervals is limited (if not broken) because it simply "increments" the month/year value of the date value. That can lead to invalid dates (e.g. from January to February).
EDIT:
In General Add_months generally adds 30/31(depending on months) to the date on which its applied. The case is slight different for monthends. If add_months is done for month of FEBRUARY, it will add (28/29) days depending its a leap year or not.
If your requirement is to simple add 99 years without checking if the resultant date is a valiad or not, then you can use INTERVAL, but if you really wanted to check if the resultant date should be very well evaluted and correct date then you must your ADD_MONTHS. It does not make any sense to first use INTERVAL then check if the resultant date(using any logic) is a valid date or not. Oracle has already provided solution for such scenarios.
Related
I need to handle specific scenario in StorProc where I need to do date calculation excluding Sat & Sun. Weekends are holiday I need to handle the data within working days.
I have implemented below code
if (purchase_date = (trunc(sysdate)-2) or purchase_date = (trunc(sysdate)-1)) Then
specific operation
As I have to exclude Sat & Sun by above implementation is giving wrong results obliviously . For example if today is Monday it has to give me back the date of Friday, my implementation is giving me Saturday or Sunday. I need to calculation with dates for weekdays only. Any help would be appreciated.
Thanks
To compare it to the previous week day, you can use:
IF purchase_date = TRUNC(SYSDATE)
- CASE TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW')
WHEN 0 THEN 3
WHEN 6 THEN 2
ELSE 1
END
THEN
-- Do something
NULL;
END IF;
TRUNC(date_value) - TRUNC(date_value, 'IW') will count the number of days since the start of the ISO week (which is always midnight on Monday).
Note: Do not use TO_CHAR(date_value, 'D') in an international setting as it will give a different result depending on which country you run it in (the week starts on Friday in Bangladesh, Saturday in some Middle-Eastern countries, Sunday in America and Monday in most of Europe).
I need to find the date of particular year, week, and weekday in oracle.
Is there a built-in function for this in oracle? or how can I achieve this?
Ex: If Year=2019, Week=22, Day=2(Tuesday), Then date should be '28-05-2019'.
Assuming "Week" and "Year" means week and year according to ISO-8601 you can use this function:
CREATE OR REPLACE FUNCTION ISOWeekDate(YEAR INTEGER, WEEK INTEGER, DAY INTEGER) RETURN DATE DETERMINISTIC IS
res DATE;
BEGIN
IF WEEK > 53 OR WEEK < 1 THEN
RAISE VALUE_ERROR;
END IF;
res := NEXT_DAY(TO_DATE( YEAR || '0104', 'YYYYMMDD' ) - INTERVAL '7' DAY, 'MONDAY') + ( WEEK - 1 ) * 7;
IF TO_CHAR(res, 'fmIYYY') = YEAR THEN
RETURN res + DAY - 1;
ELSE
RAISE VALUE_ERROR;
END IF;
END ISOWeekDate;
I think there is no built-in function for this, you will have to build one on your own.
below is one of the solutions you can make use of.
WITH FUNCTION getDate(p_year IN NUMBER, p_weeks IN NUMBER, p_day in NUMBER) RETURN DATE
IS
v_tmp date;
v_day number;
BEGIN
v_tmp := to_date('01/01/'||to_char(p_year),'dd/mm/yyyy');
v_day := to_char(v_tmp,'D');
RETURN v_tmp+(p_weeks-1)*7+p_day-(v_day)+1;
END;
SELECT getDate(2019,22,2)
FROM DUAL;
/
As per my NLS_TERRITORY settings, Oracle numbers the day of the week starting from Sunday, but from your example, you seem to be considering Monday as day 1 of the week. So, the same adjustment is made to the function to return the expected result.
Also, please note that with clause function is a new feature of Oracle 12c, if you happen to use an older version of Oracle, you will have to create the stored function before calling it.
Logically, the ISO week number is the occurrence of Thursdays in that (Gregorian) calendar year. The ISO year of an ISO week, is the Gregorian year in which the Thursday of that ISO week falls.
In the ISO schema, weeks always contain 7 days. Weeks start on a Monday, finish on a Sunday, and the days of that week are numbered 1-7.
This weekday numbering scheme often differs from that returned from built-in WEEKDAY functions and similar in different software packages (or requires particular database settings), which is something to be aware of. You will need available a function that returns the weekday of a particular date according to the ISO numbering scheme.
The first Thursday of the year can occur anywhere in the range 01-Jan to 07-Jan.
If the first Thursday falls on 01-Jan, then the first ISO week of that year will include days (29/31-Dec) from the previous Gregorian year.
If the first Thursday falls on 07-Jan, then the last ISO week of the previous Gregorian year will include days (01/03-Jan) from the current Gregorian year.
If the first Thursday falls on 04-Jan, then by implication the first ISO week of the year runs from Monday 01-Jan.
To devise an algorithm, the easiest approach is probably to establish on what weekday the 04-Jan falls in the given year, then apply the following calculation 4 - weekday. If the weekday of 04-Jan is found to be Thursday, the result will be 0. If it is Monday, the result will be 3 (4-1). If it is Sunday, the result will be -3 (4-7). We will store this offset for use after the next step.
We can derive a provisional day-of-year-offset from the ISO week and day numbers as follows: (((iso_week - 1) * 7) - (4 - iso_day)).
We then apply the offset derived in the first stage for the final day-of-year-offset. This is an offset from 04-Jan of the given Gregorian year. This offset may be a minus figure, if the relevant day falls prior to 04-Jan of the given year.
Given that day-of-year-offset, we can then use built-in functions to produce 04-Jan of the given year, and apply the day-of-year-offset to produce the final Gregorian date.
For example, given 2019-W01-1.
2019-01-04 is a Friday, so it's weekday is 5. The result of the first step is -1 (4-5). The result of the second step is -3 (((1-1)*7) - (4-1)). Added together they produce a day-of-year-offset of -4. 04-Jan-2019 offset by -4 results in 31-Dec-2018.
And that is our result: 2019-W01-1 = 2018-12-31.
Can someone please explain what the below is doing?
and effective_DATE < (TRUNC(CURRENT_DATE) - NUMTODSINTERVAL(EXTRACT(DAY FROM TRUNC( CURRENT_DATE)), 'DAY' ) + INTERVAL '1' DAY)
I don't have access to the database so can not run it myself to test.
What I understand is that the NUMTODSINTERVAL converts a number to an INTERVAL DAY TO SECOND literal.
So say if I ran this query today it would check to see if the effective date is less than the 5th of Feb 2019 - 5 days converted to seconds plus 1 day?
So, 5th Feb - 000000005 + 1 = 6th Feb?
Is this correct or am I looking at this wrong?
Also why would developers use this method?
The second argument to NUMTODSINTERVAL specifies the unit. "INTERVAL DAY TO SECOND" is a confusing name - it just means an abstract interval of time, in this case 5 days.
EXTRACT(DAY FROM TRUNC( CURRENT_DATE)) finds the current day of the month (5 today)
NUMTODSINTERVAL(5, 'DAY') creates an INTERVAL of +5 days
So today it's saying, take Feb 5, subtract 5 days (which = Jan 31), then add one day (so Feb 1).
It seems to be trying to find the first day of the month, which would be much simpler as:
and effective_DATE < TRUNC(CURRENT_DATE, 'MM')
Oracle 11g
here is a quick one hopefully.
Below is part of a script that gets date only from from the next month
first day of next month to last day. But today 29th feb it thrown an error of
ORA-01839: date not valid for month specified
M.MS_DATE between trunc(sysdate + interval '1' month,'MM') and last_day(sysdate + interval '1' month)
Is there a way round this. Many thanks
I have seen this as well and I consider this a bug in Oracle.
The workaround is to use add_months() instead :
between trunc(add_months(sysdate,1),'MM') and last_day(add_months(sysdate,1));
I would probably use add_months() as a_horse_with_no_name suggests, but just as an alternative if you want to use intervals, you can move the point you do the truncation in the first expression, and include the same truncation in the second expression:
select trunc(sysdate, 'MM') + interval '1' month as first_day,
last_day(trunc(sysdate, 'MM') + interval '1' month) as last_day
from dual;
FIRST_DAY LAST_DAY
---------- ----------
2015-02-01 2015-02-28
This works because all months have a first day, so you don't trip over the documented caveat.
When interval calculations return a datetime value, the result must be an actual datetime value or the database returns an error
I need to get all records with a date between Sunday and Saturday last week, inclusive, for whatever date the query is run. For today, April 19th 2011, that would be from April 10th to April 16th.
When I entered the dates manually and converted the filter to SQL, I got the following syntax:
RESOLVED_DATE BETWEEN timestamp '2011-04-10 00:00:00' AND timestamp '2011-04-16 00:00:00'
I'm not even sure this is correct, because it seems to exclude dates on the 16th itself (shouldn't the time be 23:59:59?)
It is possible to determine the dates you want using combinations of next_day and regular date arithmetic. Below code should be pretty close, but it's untested and probably fails on some corner case, but at least you get the general idea :)
where resolved_date >= next_day( trunc(sysdate) - interval '14' day, 'SUN')
and resolved_date < next_day( trunc(sysdate) - interval '7' day, 'SUN')
trunc(sysdate) truncate the date to day; 2011-04-19 23:32:34 becomes 2011-04-19 00:00:00, i.e. removing the time component.
next_day(sysdate, 'SUN') returns the next sunday. If sysdate happens to be a sunday, the next sunday is returned.
Important: The day names have to be in the same language as your session.
The interval thing is just a standard way of adding/subtracting different units of time from a date.
Putting it all together, the logic for the 19th of April 2011 would be:
Truncate sysdate => 2011-04-19 00:00:00
subtract 14 days => 2011-04-05 00:00:00
Find the next sunday => 2011-04-10 00:00:00
...and
Truncate sysdate => 2011-04-19 00:00:00
subtract 7 days => 2011-04-12 00:00:00
Find the next sunday => 2011-04-17 00:00:00
..resulting in the following query:
where resolved_date >= timestamp '2011-04-10 00:00:00'
and resolved_date < timestamp '2011-04-17 00:00:00'
All resolved_dates that happened on or after the first second of the 10:th but before the first second of the 17:th would be included. Note that >= and < isn't equivalent to between.
A note on performance: I would make sure that Oracle correctly estimates the date range to be 7 days, and that the correct join order/method is used. If you expect the query to run for a while, you can afford to calculate the dates in the application and supply them as date litterals instead of computing them on the fly like I did above.
take a look at the to_date function: http://psoug.org/reference/builtin_functions.html