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

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

Related

Sorting SQL Query By Date Column

I'm working on a project with a db that contains a date column for patient visits in the format of %m-%d-yyyy and need to sort so that it only pulls the rows where that date is within the last two weeks. I've tried a few different functions of convert, to_date, and can't seem to get anything to work.
I'm still very new to SQL and I don't know if this is a special case because I'm working with an oracle db
Not the full code, because it has dozens of queries and multiple joins (would that affect the date syntax?) but this is the format I'm trying for...
create table "Visits"
insert into "Visits" (
'John Doe',
'5/24/2021',
'Story about the visit',
'More room for story if needed')
select
"User_Name",
"Visit_Date",
"Visit_Narrative",
"Visit_Narrative_Overflow"
from "Visits"
where "Visits"."Visit_Date" >= TRUNC(SYSDATE) - 14
I'm working on a project with a db that contains a date column for patient visits in the format of %m-%d-yyyy
No, you don't have it in the format mm.dd.yyyy. A DATE data type value is stored within the database as a binary value in 7-bytes (representing century, year-of-century, month, day, hour, minute and second) and this has no format.
need to sort so that it only pulls the rows where that date is within the last two weeks.
You want a WHERE filter:
If you want to have the values that happened in the last 14 days then TRUNCate the current date back to midnight and subtract 14 days:
SELECT visit_date
FROM patient
WHERE visit_date >= TRUNC(SYSDATE) - INTERVAL '14' DAY
or
SELECT visit_date
FROM patient
WHERE visit_date >= TRUNC(SYSDATE) - 14
(or subtract 13 if you want today and 13 days before today.)
If you want it after Monday of last week then TRUNCate to the start of the ISO Week (which is always a Monday) and subtract 7 days:
SELECT visit_date
FROM patient
WHERE visit_date >= TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY
I ended up figuring it out based on an answer from another forum (linked below);
it appears that my original to_date() was incomplete without a second and operator in my where clause. This code is working perfectly
select
"User_Name",
"Visit_Date",
"Visit_Narrative",
"Visit_Narrative_Overflow"
from "SQLUser"."Visits"
where "SQLUser"."Visits"."Visit_Date" >= to_date('5/10/2021', 'MM/DD/YYYY')
and "SQLUser"."Visits"."Visit_Date" < to_date('5/24/2021', 'MM/DD/YYYY')

Data of last 6 quarter including current quarter

How to get data of last 6 quarter in Oracle including current quarter. I mean if I run the query today so data between 01-JAN-2018 to 30-JUN-2019 should come in the query.
You could do something like this:
SELECT
*
FROM
DUAL
WHERE
DATE_FIELD >= (SYSDATE - (30*(3*6)))
What this query is doing is taking the current date (SYSDATE), and grabbing all values from the previous 6 quarters. The rationale is:
30 = days in a month | 3 = months in a quarter | 6 = quarters specified by OP
You can use add_months and trunc functions for date value with Q(quarter) argument
select t.*
from tab t
where insert_date between
trunc(add_months(sysdate,-3*5),'Q')
and trunc(add_months(sysdate,3),'Q')-1;
Demo for verification
for the starting date, -3*(6-1) = -3*5 considered, starting from 5 quarter back to be able to count 6 quarter including the current quarter. 3 is
obvious as being the number of months in each quarter.

PLSQL - How to find Monday and Friday of the week of a given date

I have spent days trying to figure this out to no avail, so hopefully someone can help me. I have a queried date set which contains several fields including a column of dates. What I want to do is create a new field in my query that tells what the Monday and Friday is for the week of that row's particular date.
So for example; if the date in one of my rows is "1/16/18",
the new field should indicate "1/15/18 - 1/19/18".
So basically I need to be able to extract the Monday date (1/15/18) and the Friday date (1/19/18) of the week of 1/16/18 and then concatenate the two with a dash ( - ) in between. I need to do this for every row.
How on earth do I do this? I've been struggling just to figure out how to find the Monday or Friday of the given date...
Assuming that your column is of type date, you can use trunc to get the first day of the week (monday) and then add 4 days to get the friday.
For example:
with yourTable(d) as (select sysdate from dual)
select trunc(d, 'iw'), trunc(d, 'iw') + 4
from yourTable
To format the date as a string in the needed format, you can use to_char; for example:
with yourTable(d) as (select sysdate from dual)
select to_char(trunc(d, 'iw'), 'dd/mm/yy') ||'-'|| to_char(trunc(d, 'iw') + 4, 'dd/mm/yy')
from yourTable
gives
15/01/2018-19/01/18
There may be a simpler, canonical Oracle method to this but you can still reduce it to a simple calculation on your own either way. I'm going to assume you're dealing with only dates falling Monday through Friday. If you do need to deal with weekend dates then you might have to be more explicit about which logical week they should be attached to.
<date> - (to_char(<date>, 'D') - 2) -- Monday
<date> + (6 - to_char(<date>, 'D')) -- Friday
In principle all you need to do is add/subtract the appropriate number of days based on the current day of week (from 1 - 7). There are some implicit casts going on in there and it would probably be wise to handle those better. You might also want to check into NLS settings to make sure you can rely on to_char() using Sunday as the first day of week.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm
You can also use the NEXT_DAY function, as in:
SELECT TRUNC(NEXT_DAY(SYSDATE, 'MON')) - INTERVAL '7' DAY AS PREV_MONDAY,
TRUNC(NEXT_DAY(SYSDATE, 'FRI')) AS NEXT_FRIDAY
FROM DUAL;
Note that using the above, on weekends the Monday will be the Monday preceding the current date, and the Friday will be the Friday following the current date, i.e. there will be 11 days between the two days.
You can also use
SELECT TRUNC(NEXT_DAY(SYSDATE, 'MON')) - INTERVAL '7' DAY AS PREV_MONDAY,
TRUNC(NEXT_DAY(SYSDATE, 'MON')) - INTERVAL '3' DAY AS NEXT_FRIDAY
FROM DUAL;
in which case the Monday and Friday will always be from the same week, but if SYSDATE is on a weekend the Monday and Friday returned will be from the PREVIOUS week.

ORA-01839: date not valid for month specified

The following query returns
select to_char( trunc(sysdate) - numtoyminterval(level - 1, 'month'), 'mon-yy') as month from dual connect by level <= 12
last 12 months according to today's date(i.e. 2-Jan-18).
Say if today's date is 29-DEC-17 it gives oracle sql error:
ORA-01839: date not valid for month specified
(since on subtracting there would be a date in the result as '29-FEB-17' which is not possible). So on specific dates this error would pop-up. How do you suggest to overcome this?
I prefer ADD_MONTHS (TRUNC of a date literal is stupid, but I left it as you'd have SYSDATE anyway, wouldn't you?):
SQL> select to_char(add_months(trunc(date '2017-12-29'), -level), 'dd-mon-yy',
2 'nls_Date_language = english') as month
3 from dual
4 connect by level <= 12;
MONTH
------------------
29-nov-17
29-oct-17
29-sep-17
29-aug-17
29-jul-17
29-jun-17
29-may-17
29-apr-17
29-mar-17
28-feb-17
29-jan-17
29-dec-16
12 rows selected.
SQL>
This behavior of INTERVAL YEAR TO MONTH is as documented, see Datetime/Interval Arithmetic
You should consider function ADD_MONTHS:
If date is the last day of the month or if the resulting month has
fewer days than the day component of date, then the result is the last
day of the resulting month. Otherwise, the result has the same day
component as date.
It depends on your requirements what you consider as "right". In fact "one month" does not have a fixed duration.

day of the week in the week numbering

in oracle there is a possibility to change starting day of the week.
In US first day is SUN, and in EU - MON
This change works by changing nls_territory
However if I consider day of the week and week numbering at once, I have following situation:
day week_number
7 9 <- here it should be still week 8
1 9
2 9
3 9
4 9
5 9
6 9
7 10 <- here it should still be week 9
Do you have any idea which nls should could affect this start_day of the week?
Similar post
Get First Day Of Week From Week Number
Oracle has two date formats which give us the Week Number.
'WW' starts from the first of January and increments every seven days. As 01-JAN-2013 was a Tuesday that means the Week Number increments on a Tuesday. I guess this is the version you're using at the moment.
But there is also the 'IW' format, which is the ISO standard. In this version, the week number starts from the Monday of the week where the first of January falls. This has the peculiar side-effect of making 31-DEC-2012 the first date of Week 1 on 2013, but it does mean the week number always increments on day 1 of the week.
Find out more.
Not sure I understand your question and I see no connection between day# and week# in your example, sorry... Maybe this will help you to get ISO week. This builds annual week table. You may add 'WW' format to the query and compare dates, weeks in diff. NLS settings:
-- ISO_WK# --
SELECT mydate
, TRUNC(mydate, 'w') wk_starts
, TRUNC(mydate, 'w') + 7 - 1/86400 wk_ends
, TO_NUMBER (TO_CHAR (mydate, 'IW')) ISO_wk#
FROM
(
SELECT TRUNC(SYSDATE, 'YEAR')-1 + LEVEL AS mydate
FROM dual
CONNECT BY LEVEL <=
(
SELECT TRUNC(SYSDATE, 'YEAR')-TRUNC(ADD_MONTHS (SYSDATE, -12), 'YEAR') "Num of Days"
FROM dual
)
)
/