Return records from todays month + 6 - oracle

how can i return records sales from todays + 6 months.
Example:
Today´s month: May-2018.
Today´s month + 6: Nov-2018
So, i want a function to retrieve records from Table_Date, from November (1 to 30).
Thanks.

Use the TRUNC and ADD_MONTHS functions:
SELECT *
FROM sales
WHERE SalesDate >= ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 6 )
AND SalesDate < ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 7 );

You have SQL server and oracle, it cant be both.
This is for SQL Server (though logic would work in ORACLE, but syntax may not)
This is to get 6 months from today
SELECT DATEADD(MM, 6, GETDATE())
Added extra code here for more details after looking at question more:
I am using variables to do the calculations to make it easier to understand but you could do it all inline (but harder to read).
This script gets a date 6 months from now, takes that month and finds the first day of that month, and then the last day of that month so you can use those 2 dates in a where clause logic.
DECLARE #NewMonth AS VARCHAR(10)
DECLARE #NewYear AS VARCHAR(10) -- in case adding months goes to next year
SELECT #NewMonth = DATEPART(MONTH, DATEADD(MM, 6, GETDATE())),
#NewYear = DATEPART(YEAR, DATEADD(MM, 6, GETDATE()))
DECLARE #FirstOfMonth AS DATE = #NewMonth + '/1/' + #NewYear
-- for start of month check we can just hard code one in it, for end of month we cant because we dont know how many days are in it
SELECT #FirstOfMonth, DATEADD(DAY, -1, DATEADD(MONTH, 1, #FirstOfMonth)) AS LastDayOfMontbh

Related

How can I get data of the current week in Oracle

How can I get data of the current week in Oracle? Assume that the week goes from Sunday to Saturday
SELECT * FROM TABLENAME
WHERE DATEFIELD IS {WITHIN CURRENT WEEK THAT GOES FROM 12AM ON SUNDAY TO 11:59PM ON SATURDAY}
Use Oracle trunc(sysdate,'IW') returns the first day of the week and add 6 to get the end of week.
-- not tested
SELECT * FROM TABLENAME
WHERE DATEFIELD >= trunc ( sysdate, 'iw' )
AND DATEFIELD < trunc ( sysdate, 'iw' ) + 5

Get 9 AM of current day in Oracle

Can anyone tell me how to get 9AM of the current day in Oracle?
In SQL Server I would do the following:
SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), '09:00:00')
I'm writing a stored procedure that only retrieves data from 9 AM of the same day.
Just take the current day at midnight and add nine hours
trunc(sysdate) + interval '9' hour
select trunc(sysdate) +9/24 from dual;
Try to get 9AM of the current day in Oracle using the following:
select to_char(trunc(sysdate + 1) + 9/24, 'HH')|| 'AM' as "current day 9AM"
from dual;

Between with Variables for prior months start and stop

I use the following WHERE with sysdate to get a between range.
WHERE TO_CHAR(mopstart, 'yyyy-mm-dd hh24:mi:ss') BETWEEN TO_CHAR(sysdate,'YYYY-MM-DD')||' 21:00:00' AND TO_CHAR(sysdate+1,'YYYY-MM-DD')||' 20:59:59'
My question is, how do I create a BETWEEN with variables for the date to return the prior months start and end date?
You would usually not use BETWEEN for this. As in real life you would not say "the time from beginning of last month till the end of last month", but just "last month", so you would do in SQL.
Knowing that a month is actually the year and the month:
where extract(year from mopstart) = extract(year from sysdate)
and extract(month from mopstart) = extract(month from sysdate) - 1
Or:
where to_char(mopstart,'yyyymm') = to_char(add_months(sysdate,-1),'yyyymm')
If I understand you well, that would be:
where ... between trunc(add_months(sysdate, -1), 'MON') and trunc(sysdate, 'MON') - 1
One more thing: don't use TO_CHAR to compare dates, just use dates and intervals.

Business days calculation (by including time portion) between start date and end date by excluding weekends and custom/business specific holidays

Could you please help me out how to calculate the no. of business days between start date and end date by excluding weekends & business specific holidays. Let's say if we take start date, end date and custom holiday table with list of holiday dates as mentioned below.
Start date (mm/dd/yyyy hh24:mi:ss) : '10/15/2012 08:00:00'
End date (mm/dd/yyyy hh24:mi:ss) : '10/23/2012 10:30:00'
holiday_table.business_holiday_date values (mm/dd/yyyy):
10/17/2012
10/19/2012
10/22/2012
Need to calculate business days by taking into consideration of the time portion of dates in the calculation (so can expect business days with fractions as well ex. 1.25, 3.7 etc)
Appreciate if you can help me on this ASAP.
-James
Try:
SELECT(end_date - start_date) -
(select count(*)
from holiday_table
where business_holiday_date >= start_date
and business_holiday_date <= end_date)
FROM dual
Here is a fiddle
I have a function that use only 2 dates but you can improve it. Here you go...
First check how many days you got in the holiday table, excluding weekend days.
Get business days (MON to FRI) between the 2 dates and after that subtract the holiday days.
create or replace
FUNCTION calculate_business_days (p_start_date IN DATE, p_end_date IN DATE)
RETURN NUMBER IS
v_holidays NUMBER;
v_start_date DATE := TRUNC (p_start_date);
v_end_date DATE := TRUNC (p_end_date);
BEGIN
IF v_end_date >= v_start_date
THEN
SELECT COUNT (*)
INTO v_holidays
FROM holidays
WHERE day BETWEEN v_start_date AND v_end_date
AND day NOT IN (
SELECT hol.day
FROM holidays hol
WHERE MOD(TO_CHAR(hol.day, 'J'), 7) + 1 IN (6, 7)
);
RETURN GREATEST (NEXT_DAY (v_start_date, 'MON') - v_start_date - 2, 0)
+ ( ( NEXT_DAY (v_end_date, 'MON')
- NEXT_DAY (v_start_date, 'MON')
)
/ 7
)
* 5
- GREATEST (NEXT_DAY (v_end_date, 'MON') - v_end_date - 3, 0)
- v_holidays;
ELSE
RETURN NULL;
END IF;
END calculate_business_days;
After that you can test it out, like:
select
calculate_business_days('21-AUG-2013','28-AUG-2013') as business_days
from dual;

Oracle Date - How to add years to date

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

Resources