Skip weekends Business day tasks NetSuite - sql

Using NetSuite, I am trying to automate the creation of monthly tasks. These tasks will fall on the last day of the month, a day before, and 1,2,3,4,5 days after the last day of the month. But, the tasks can't be due on a weekend, only business days. So if the day falls on a sat or sun the dates have to move up. How can I use my custom record to calculate the next months task dates skipping weekends? Here is a screenshot of the record - see that 10/4 and 10/5 fall on saturday and sunday, how can I have it skip sat and sunday? I used the sql function option to generate the dates and days of the week.

I tested the following formula. Replace custbody_date with your field name. This formula should be set in your 'next month' column.
CASE WHEN INSTR(to_char({custbody_date}, 'DAY'),'SATURDAY') != 0
THEN {custbody_date}+2
WHEN INSTR(to_char({custbody_date}, 'DAY'),'SUNDAY') != 0
THEN {custbody_date}+1 ELSE {custbody_date}
END
This should also give you an idea to set the value for 'Next Month Day' (skipping saturday and sunday).

Related

How can I Format the Date so that the fiscal week starts in December?

I want to format a date as follows: Y17W15, but there is no option to set the start of the year. However, there is no consistent way of calculating this. I cannot just subtract a month (other times I will need to show the month too), and I cannot just add 4 or 5 to the week field due to leap years, etc.
Our year starts on a Saturday that is closest to December 1. This means if November 30 is on a Saturday, the Fiscal Year will start on November 30.
Currently what I have is below, which works fine except it shows Y17W10. The easiest option in my head is to have a way to actually set the start of a fiscal year, but if I have to go through a bunch of if statements it's okay as long as it works.
MsgBox(Format(Now, """Y""yy""W""mm"""))
Thanks for your time!
I am aware that: Given a 4-5-4 calendar and a date, how do I determine what fiscal week that date falls in? exists but I am looking for an answer that isn't as hard-coded.
Michael

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.

How to subtract 13 weeks from a date in PL SQL?

I have a date in sql which will always fall on a Monday and I'm subtracting 13 weeks to get a weekly report. I am trying to get the same 13 week report but for last year's figures as well.
At the moment, I'm using the following:
calendar_date >= TRUNC(sysdate) - 91
which is working fine.
I need the same for last year.
However, when I split this into calendar weeks, there will also be a partially complete week as it will include 1 or 2 days from the previous week. I need only whole weeks.
e.g. the dates that will be returned for last year will be 14-Feb-2015 to 16-May-2015. I need it to start on the Monday and be 16-Feb-2015. This will change each week as I am only interested in complete weeks...
I would do this:
Get the date by substracting 91 days as you're already doing.
Get the number of the day of the week with TO_CHAR(DATE,'d')
Add the number of days until the next monday to the date.
Something like this:
SELECT TO_DATE(TO_DATE('16/05/2015','DD/MM/YYYY'),'DD/MM/YYYY')-91 + MOD(7 - TO_NUMBER(TO_CHAR(TO_DATE(TO_DATE('16/05/2015','DD/MM/YYYY'),'DD/MM/RRRR')-91,'d'))+1,7) d
FROM dual
next_day - returns date of first weekday named by char.
with dates as (select to_date('16/05/2015','DD/MM/YYYY') d from dual)
select
trunc(next_day( trunc(d-91) - interval '1' second,'MONDAY'))
from dates;
I want to get next monday from calculated date. In situation when calculated date is monday i have to move back to previous week ( -1 second).

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)
)

Access query (SQL) to return records sorted (grouped by) WEEKS

Greetings SQL gurus,
I don't know if you can help me, but I will try. I have several large databases grouped by year (each year in a different database). I want to be able to compare values from a particular week from one year to the next. For example, "show me week 17 of 2008 vs. week 17 of 2002."
I have the following definition of weeks that ideally I would use:
Only 52 weeks each year and 7 days a week (that only takes 364 days),
The first day of the first week starts from January 2nd - which means we do not use January 1st data, and
In leap year, the first day of the first week ALSO starts from the January 2nd plus we skip Feb. 29.
Any ideas?
Thanks in advance.
Best to avoid creating a table because then you have to update and maintain it to get your queries to work.
DatePart('ww',[myDate]) will give you the week number. You may run into some issues though deciding which week belongs to which year - for example if Jan 1 2003 is on Wednesday does the week belong as week 52 in 2002 or week 1 in 2003? Your accounting department will have a day of the week that is your end of week (usually Sat). I usually just pick the year that has the most days in it. DatePart will always count the first week as 1 and in the case of the example above the last week as 53. You may not care that much either way. You can create queries for each year
SELECT DatePart('ww',[myDate]) as WeekNumber,myYearTable.* as WeekNumber
FROM myYearTable
and then join the queries to get your data. You'll loose a couple days at the end of the year if one table has 52 weeks and one has 53 (most will show as 53). Or you can do it by your weekending day - this always gives you Saturday which would push a late week into the following year.
(7-Weekday([myDate]))+[myDate]
then
DatePart('ww',(7-Weekday([myDate]))+[myDate])
Hope that helps
To get the week number
'to get the week number in the year
select datepart( week, datefield)
'to get the week number in the month
select (datepart(dd,datefield) -1 ) / 7 + 1
You don't need to complicate things thinking about leap years, etc. Just compare weeks mon to sun
SInce you havea a specifc defintion of when the week starts that is differnt that the standard used by the db, I think a weeks table is the solution to your problem. For each year create a table that defines the dates contained in each week and the week number. Then by joining to that table as well as the relevant other tables, you can ask for just the data for week 17.
Table structure
Date Week
20090102 1
20090103 1
etc.
I needed to create a query that shows BOTH year AND week numbers, like 2014-52. The year shows correct when you use the Datepart() formula to convert week 53 to week 52 in the previous year, but shows the wrong year for the week that was week 1 previously that should be week 52 now. It show that week as 2015-52 instead of 2014-52.
Furthermore, it sorts the data wrong if you only use only the week number, eg:
2014-1,2014-11,2014-2
To overcome this I created the following query to insert a 0 and also to check for days in week 1 that should still fall under week 52.
ActualWeek: IIf(DatePart("ww",[SomeDate],1,3)=52 And DatePart("ww",[SomeDate])=1, DatePart("yyyy",[SomeDate],1,3)-1,DatePart("yyyy",[SomeDate],1,3)) & "-" & IIf(DatePart("ww",[SomeDate],1,3)<10,"0" & DatePart("ww",[SomeDate],1,3),DatePart("ww",[SomeDate],1,3))