Find the Friday after Next in Oracle - sql

I have a weird situation where I need to make a DueDate to be the following Friday.
So if the date is Monday, March 11th 2019 then the DueDate is Friday March 22nd.
I am able to do this easily with:
TRUNC(Next_Day(FilingPeriod, 'FRI')) + 7 as DueDate
My issue comes when the day is Friday March 15th 2019 ad it should also have a DueDate of Friday March 22nd, instead it has a DueDate of Friday March 29th.
And I get why. I have been looking for a way to simplify this or throw in a check to determine if the current date is a Friday and go from there.
I have looked for a bit and read similar questions but I still can not seem to find it.
Thanks for any help

Just subtract 1 from the date before using NEXT_DAY():
TRUNC(Next_Day(FilingPeriod - 1, 'FRI')) + 7 as DueDate

Get back to Monday, the first day of the ISO week. Then go plus 4, then plus 7 days:
TRUNC(FilingPeriod, 'iw') + 11
On a sidenote: I'd avoid NEXT_DAY whenever possible. The day name depends on the current language setting and NEXT_DAY has no parameter to override this. This renders the function rather dangerous, because it makes a query fail-prone.

Related

Informix SQL to get date range that changes automatically

Please help me with an Informx SQL query to be run every Friday.
Period: 1st of the Month to the Thursday before the Friday that it is being run on. I can't just choose date ranges as it will be an auto report so the dates needs to update automatically. Is there a way to do this?
You've got two issues: the start of the month and the date of last Thursday. The simplest expression to identify the 1st day of the current month is:
MDY(MONTH(TODAY), 1, YEAR(TODAY))
The way to identify the most recent Thursday is via a CASE statement on WEEKDAY(TODAY). Something like:
CASE
WHEN WEEKDAY(TODAY) < 5 -- (Sunday (0) - Thursday (4))
THEN TODAY - WEEKDAY(TODAY) - 3 -- Calc last Sunday, back 3 days
ELSE TODAY - WEEKDAY(TODAY) + 4 -- Calc last Sunday, forward 4 days
END
Note, you will still run into issues where the 1st of current month is later than the most recent Thursday. But hopefully the examples above will point you in the right direction.
Of course, if you know this report is only ever going to be run on Fridays, then calculating the most recent Thursday is just TODAY -1.

Datepart Week Sql (With 53 weeks for year)

I'm calculating the week for a specific date in SQL for example
'2016-01-20' (yyyy-mm-dd) but SQL returns week: 4, and that is wrong because this year the first week started on '2016-01-04' the result must be week: 3.
I think the issue is generatad because 2015 was a year with 53 weeks, any solution to that? Thank you and I'm sorry for my bad English
In tSQL the DATEPART() is returning the correct data based on US and Most of Europe as well as UK See here
You can use SET DATEFIRST to adjust the start position however.
The ISO 8601 definition for week 01 is the week with the year's first Thursday in it. I am using Intersystems cache which apparently does not account for that either. So I have used this to address that
CASE WHEN 7-datepart(dw,dateadd(dd,1,dateadd(yy,datediff(yy,0,getdate())-1,0))) < 2
THEN datepart(wk,getdate())-1 ELSE datepart(wk,getdate()) END as WeekNum

What exactly does trunc(date, 'IW')?

For my project I need to have an absolute numerical correspondence between days of the week and 1...7 values.
As you probably know the association between days and numbers can vary according to the locale, for example in Germany Monday is 1 and Sunday is 7, while in US Monday is 2 while Sunday is 1.
So, searching for a solution, I found the following code which seems working regardless of the locale, assigning Monday=1...Sunday=7:
1 + TRUNC (date) - TRUNC (date, 'IW')
Can someone explain me how does it work? In particular I just can't understand what this instruction:
TRUNC (date, 'IW')
exactly does.
TRUNC(DATE,'IW') returns the first day of the week. For me TRUNC(SYSDATE,'IW) returns Monday. Today is Tuesday Feb 21. Subtract from that TRUNC(SYSDATE,'IW') which would be Monday the 20th, and you'll get 1 (because 21-20=1). Add 1 onto that as you do in the beginning of your equation and you get 2, which we associate with Tuesday.
The very basic concept of ISO week is to make it NLS territory independent.
From documentation,
Week of year (1-52 or 1-53) based on the ISO standard.
A week starts on a Monday and ends on a Sunday.

Querying Last Week's data amd this week's data up to today

I am having a problem with making a Oracle Query that sums up everything that occurs during the whole last week and everything during this week. so lets say i have this table
DATA DATE
3 2-feb-15
4 3-feb-15
6 3-feb-15
7 27-jan-2015
5 27-jan-2015
4 25-jan-2015
so lets today is feb 5th and the query this week should be 3+4+6 assuming that sunday is feb 1st. The this week's data range should be (feb 2nd - feb 5th) minus sunday.
and the sum for last week should be 7+5+4 no matter what date today is as long as it still last week from Monday to saturday
Another case is, if todays date is feb 2nd, then the sum should be 3.
Please help me, i tried with SYSDATE - (Var) and it doesnt work because it doesnt position the date according to the calendar
thank you very very much
try this :
trunc(sysdate)
I hope its working if its not please give me more detail of your problem

Pulling a dynamic day range from the previous year in DB2

I have two current SQL queries that I currently use to compare GM% from previous year vs. GM% this year. This is a daily report that I run every morning. The date arithmetic is not very solid and I am trying to find an alternative. Previously I thought that the report would only be for Monday forward, and not including the current day (ie if ran on Tuesday, it would only pull Monday. If ran on Monday it would not pull anything.) Recently that has changed to where when the report is ran on Monday, they want to see Friday-Sunday. What I am considering is setting it to pull the previous 5 day, not including the current day. (Ran on Monday would pull Thur, Fri, Sat, Sun.) The problem is that it has to be a day this year vs same day last year comparison. Anyone who;s tried this knows that it is not easy to get this. Here is my current code for the date arithmetic. I am at a loss guys, I could use some help.
Where DB1.TB1.CLM1>=Current Date-364 days - (DAYOFWEEK(CURRENT DATE) - 2) DAYS
And DB1.TB1.CLM1< Current Date- 364 days
If I hear you right, on Tuesday you would pull stats for Monday. Wed, you pull stats for Mon-Tues. Friday, you pull Mon-Thurs. And for all of these, you need the equivalent day prior year.
The trick is that now on Monday, you need to pull the previous weekend, i.e. Thu-Sun.
You have not defined what to do on Sunday, so I'm leaving that case out.
Try this WHERE statement:
where
( -- do this after Monday
dayofweek(current date) > 2 and
DB1.TB1.CLM1 between ((current date - 364 days) - (dayofweek(current date) - 2) days) and (current_date - 365 days)
)
or
( -- do this on Monday
dayofweek(current date) = 2 and
DB1.TB1.CLM1 between (current date - 368 days) and (current date - 365 days)
)