only working days on case in Oracle SQL - sql

I've executed the following case in a query:
WHEN phases is null and INSERTIONDATE is null and Priority = 1 and Complexity = 'minor' THEN trunc(CreationDate)+2
But I want to add +2 working days. How can I achieve that?
Example:
Lets say that we have an ID with insertion date NULL and priority 1 minor and creation date on Friday 15/01/2016.
The output should be:
115 prio1 minor acknowledge_date 19/01/2016

This is a total shot in the dark, but you mentioned you wanted to add two work days, and the example you gave (January 16 + 2 work days = January 19) tells me you mean you want to skip weekends. If this is the case, I think something like this might work:
select
case
when phases is null and
insertiondate is null and
priority = 1 and
complexity = 'minor' then
case to_char(creationdate, 'D')
when '5' then to_char(creationdate + 4, 'DD/MM/YYYY')
when '6' then to_char(creationdate + 4, 'DD/MM/YYYY')
when '7' then to_char(creationdate + 3, 'DD/MM/YYYY')
else to_char(creationdate + 2, 'DD/MM/YYYY')
end
end
from test
In essence, I'm doing a poor man's, brute-force business day calculation of "2 work days" by saying on Thursday and Friday, 2 business days = 4 calendar days, on Saturday, 2 business days = 3 calendar days, and all other days 2 days = 2 days.
It wouldn't shock me if this misses the mark, but maybe it's a starting point for clarification.
For the following create days, it returns the following results:
Creation date Result
1/15/2016 9:28:31 AM 19/01/2016
1/16/2016 9:41:49 AM 19/01/2016
1/17/2016 9:41:51 AM 19/01/2016
1/18/2016 9:41:52 AM 20/01/2016
1/19/2016 9:41:54 AM 21/01/2016
1/21/2016 9:42:00 AM 25/01/2016

Related

Pulling last week M-F range from any current week day

SELECT d.i_EmpID as 'EmpID',m.c_EmployeeFirstName as 'First Name',
m.c_employeeLastName as 'Last Name', d.c_date as 'Date', d.c_A as 'A', d.c_M
as 'M', d.c_P as 'P', d.c_lp as 'Lp'
,'School' as Company
FROM [table1].[dbo].[tbl_daily] d
inner join [table2].[dbo].[tbl_emp] m
on d.[i_EmpID] = m.[i_EmpID]
where left(c_date, 2)=datepart(month, getdate()-1) and
right(c_date,4)=datepart(year,getdate()-1) and substring(c_date,
4,2)=datepart(day,getdate()-3)
Hello,
I am trying to pull last week date range past off of current date. Right now I have 5 separate queries unioned for the end of the code (day,getdate()-3) to be the other 5 days in the week (I am looking for Monday to Friday). This is essentially my query without the other 4 unions to be able to pull Monday to Friday and I already this is not the best way at all to do it and in fact it doesn't even work today.
I am able to get this done in MySQL because it has a built in week function and I can just pull last week, however I can't figure out how to pull last week in SQL because the language is different. My issue is that my code worked yesterday, however due to the fact it is subtracting the days, on Tuesday to Friday, it won't work/it doesn't work. I am by no means an expert with using dates in SQL and I am at a loss.
I know there has to be a better way, I've spent about 4 hours reading various guides and examples but I cannot seem to find an example of specifically what I am looking for.
Long and short of it is that I am trying pull last week (example: 9/24 to 9/28) any day of the current week that I run it. So even if I run my report on Friday(10/5), I am able to see 9/24 to 9/28 and the similarly next week to be able to pull 10/1 to 10/5 of this week. Those are the tables and columns I am pulling. Also, if it matters, this is not a school project but a work related project I was tasked with.
Any guidance would be greatly appreciated. Thank you in advanced.
edit: Sample Data:
EID First Last Date A M P LP Company
5303 Kent Grit 09/25/2018 9 44 54 3 Company 1
6890 Lola Grif 09/28/2018 5 4 3 Company 1
6272 Paul Hammer 09/26/2018 1 2 4 4 Company 1
6273 Joe Hammer 09/24/2018 11 8 7 Company 1
Is this what you're looking for?
...
WHERE
CAST(c_date AS DATE) >= DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE())-1, 0)
AND
CAST(c_date AS DATE) < DATEADD(DAY, +5, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE())-1, 0))
It will pull last Monday at midnight and last Saturday at midnight (so you get the last second of Friday in your result set) from Sunday through Saturday.
Edit: Added the CAST per comments.
This can be accomplished with dateadd and datepart functions with some arithmetic.
with dates(dt) as (select '2018-10-02' union all
select '2018-10-01' union all
select '2018-10-03' union all
select '2018-10-04' union all
select '2018-10-05' union all
select '2018-10-06' union all
select '2018-10-11'
)
--Actual query to be executed by replacing the table name
select dt,
dateadd(day,-datepart(dw,cast(dt as date))-5,cast(dt as date)) as strt,
dateadd(day,-datepart(dw,cast(dt as date))-1,cast(dt as date)) as ed
from dates --replace this with the table being used

ORACLE Find Date ('DD-MON-YYYY') of a given weekday in the past 7 days

i am trying to create a view that compares a SCHEDULE table that has values such as ('Daily', 'Wednesday', 'Tuesday', etc..) and another table (REPORT CREATED) that is updated every day with dates (11-AUG-2017). Basically, if the Schedule table shows Daily, then the Report Created table record value should be whatever sysdate (current date) is equal to. That said, I'm not sure how to find out what the most recent 'Wednesday' or 'Tuesday' is equal to. I did find a function for SQL server (How to get Saturday's Date (Or any other weekday's Date)- SQL Server) ; however, I do not understand how it works and can't find an equivalent in Oracle. Any guidance would be greatly appreciated!
Edit: I have created two sample tables:
Schedules Table:
Report_Name | Frequency
ORDERS_BY_DEPT | Daily
LOW_STOCK | Wednesday
INVENTORY_DISC | Thursday
and the Reports Table:
Report_Name | Create_Dt
INVENTORY_DISC | 3-Aug-2017
LOW_STOCK | 9-Aug-2017
ORDERS_BY_DEPT | 10-Aug-2017
So essentially, the Inventory_Disc report is off, since it should have ran every Thursday but hasn't been updated since last Thursday and the Orders_By_Dept report is off since it is a daily report and didn't run today.
Use the NEXT_DAY( date_value, day_string ) function. Take the current day (SYSDATE) and subtract 7 days from it and then find the next day which matches your required day-of-the-week.
So, to get the most recent Wednesday:
SELECT NEXT_DAY( TRUNC( SYSDATE ) - 7, 'WEDNESDAY' )
FROM DUAL
You can find the day of the week with:
to_char(sysdate, 'D')
So for example, the last Thursday is:
select case
when to_char(sysdate, 'D') < 4 then sysdate - to_char(sysdate, 'D') - 7 + 4
else sysdate - to_char(sysdate, 'D') + 4
end
from dual

Oracle correctly getting the month difference

Hi Ive been having an issue with getting the correct difference in a date from the current month not including the day.
ie if the month when the query is run is march 2013
then the following should be the result
EXECUTION_DATE, EXEC_DIFF
01-FEB-13, 1
31-JAN-13, 2
30-JAN-13, 2
however using the below sql statement im getting
EXECUTION_DATE, EXEC_DIFF
01-FEB-13, 1
31-JAN-13, 2
30-JAN-13, 1
select EXECUTION_DATE,
floor(MONTHS_BETWEEN (trunc(sysdate,'MM')-1, EXECUTION_DATE))+1 "EXEC_DIFF"
from V_CERT_LIST
WHERE EXECUTION_DATE < TO_DATE('02/02/2013','DD/MM/YYYY')
ORDER BY EXECUTION_DATE DESC
Please can someone put me right ive been bashing my head with this for some time now
thanks
select EXECUTION_DATE,
MONTHS_BETWEEN (trunc(sysdate,'MM'), trunc(EXECUTION_DATE,'MM')) "EXEC_DIFF"
from V_CERT_LIST
WHERE EXECUTION_DATE < TO_DATE('02/02/2013','DD/MM/YYYY')
ORDER BY EXECUTION_DATE DESC
Not looking for scores but cannot understand what is the problem with months_between? In my understanding it does not matter when in month execution takes place - Jan-31 or Jan 30... The difference is still 2 months between Jan and Mar as in your example. I can add more days in month in the query but mo_betw. will still be the same...:
SELECT to_char(exec_date, 'DD-MON-YYYY') exec_date, MONTHS_BETWEEN(run_date, exec_date) months_btwn
FROM
(
SELECT to_date('01/03/2013', 'DD/MM/YYYY') run_date
, Add_Months(Trunc(sysdate,'YEAR'),Level-1) exec_date -- first day of each month
FROM dual
CONNECT BY LEVEL <= 3
)
/
EXEC_DATE MONTHS_BTWN
------------------------
01-JAN-2013 2
01-FEB-2013 1
01-MAR-2013 0
Months_Between has complex logic that takes the day of the month into account.
Perhaps what you want is this:
select EXECUTION_DATE,
((year(sysdate)*12+month(sysdate)) - (year(execution_date)*12 + month(execution_date))
) as Exec_Diff
from V_CERT_LIST
WHERE EXECUTION_DATE < TO_DATE('02/02/2013','DD/MM/YYYY')
ORDER BY EXECUTION_DATE DESC
This converts the year/month combination into the number of months since 0 time and then subtracts the results.

Adding relative week number column to MySQl results

I have a table with 3 columns: user, value, and date. The main query returns the values for a specific user based on a date range:
SELECT date, value FROM values
WHERE user = '$user' AND
date BETWEEN $start AND $end
What I would like is for the results to also have a column indicating the week number relative to the date range. So if the date range is 1/1/2010 - 1/20/2010, then any results from the first Sun - Sat of that range are week 1, the next Sun - Sat are week 2, etc. If the date range starts on a Saturday, then only results from that one day would be week 1. If the date range starts on Thursday but the first result is on the following Monday, it would be week 2, and there are no week 1 results.
Is this something fairly simple to add to the query? The only ideas I can come up with would be based on the week number for the year or the week number based on the results themselves (where in that second example above, the first result always gets week 1).
Example:
$start = "05/27/2010";
$end = "06/13/2010";
//Query
Result:
week date user value
1 05/28/2010 joe 123
3 06/07/2010 joe 123
3 06/08/2010 joe 123
4 06/13/2010 joe 123
This can easily be done with this simple and obvious expression :)
SELECT ...,
FLOOR(
(
DATEDIFF(date, $start) +
WEEKDAY($start + INTERVAL 1 DAY)
) / 7
) + 1 AS week_number;
Some explanation: this expression just calculates difference between the given date and the start date in days, then converts this number to weeks via FLOOR("difference in days" / 7) + 1. That's simple, but since this works only when $start is Sunday, we should add an offset for other week days: WEEKDAY($start + INTERVAL 1 DAY) which equals to 0 for Sun, 1 for Mon, ..., 6 for Sat.

Oracle week calculation issue

I am using Oracle's to_char() function to convert a date to a week number (1-53):
select pat_id,
pat_enc_csn_id,
contact_date,
to_char(contact_date,'ww') week,
...
the 'ww' switch gives me these values for dates in January of this year:
Date Week
1-Jan-10 1
2-Jan-10 1
3-Jan-10 1
4-Jan-10 1
5-Jan-10 1
6-Jan-10 1
7-Jan-10 1
8-Jan-10 2
9-Jan-10 2
10-Jan-10 2
11-Jan-10 2
12-Jan-10 2
a quick look at the calendar indicates that these values should be:
Date Week
1-Jan-10 1
2-Jan-10 1
3-Jan-10 2
4-Jan-10 2
5-Jan-10 2
6-Jan-10 2
7-Jan-10 2
8-Jan-10 2
9-Jan-10 2
10-Jan-10 3
11-Jan-10 3
12-Jan-10 3
if I use the 'iw' switch instead of 'ww', the outcome is less desirable:
Date Week
1-Jan-10 53
2-Jan-10 53
3-Jan-10 53
4-Jan-10 1
5-Jan-10 1
6-Jan-10 1
7-Jan-10 1
8-Jan-10 1
9-Jan-10 1
10-Jan-10 1
11-Jan-10 2
12-Jan-10 2
Is there another Oracle function that will calculate weeks as I would expect or do I need to write my own?
EDIT
I'm trying to match the logic used by Crystal Reports. Each full week starts on a Sunday; the first week of the year starts on whichever day is represented by January 1st (e.g. in 2010, January 1st is a Friday).
When using IW, Oracle follows the ISO 8601 standard regarding week numbers (see http://en.wikipedia.org/wiki/ISO_8601). That is the same standard than the one we generally use in Europe here.
Your problem is also mentioned on the Oracle forum: http://forums.oracle.com/forums/thread.jspa?threadID=947291 and http://forums.oracle.com/forums/message.jspa?messageID=3318715#3318715. Maybe you can find a solution there.
I know this is old, but still a common question.
This should give you the correct results in the smallest amount of effort:
select pat_id,
pat_enc_csn_id,
contact_date,
to_char(contact_date + 1,'IW') week,
...
Since it looks like you are using your own special definition of the week number you'll need to write your own function.
It might be helpful that NLS_TERRITORY affects the day with which a week starts as used by the D Format Model
see also:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements004.htm#SQLRF00210
and
http://www.adp-gmbh.ch/ora/sql/to_char.html
Based on this question, How do I calculate the week number given a date?, I wrote the following Oracle logic:
CASE
--if [date field]'s day-of-week (e.g. Monday) is earlier than 1/1/YYYY's day-of-week
WHEN to_char(to_date('01/01/' || to_char([date field],'YYYY'),'mm/dd/yyyy'), 'D') - to_char([date field], 'D') > 1 THEN
--adjust the week
trunc(to_char([date field], 'DDD') / 7) + 1 + 1 --'+ 1 + 1' used for clarity
ELSE trunc(to_char([date field], 'DDD') / 7) + 1
END calendar_week