I need a report that runs once on the following month below:
JAN,FEB,APR,MAY,JUL,AUG,OCT,NOV (essentially skipping MAR, JUN, SEP and DEC)
and this is how I do it (I had to have two schedules to achieve it: See screenshot below). and My question does my two-schedule method will get me what I need? is there a better way?
Will this still work in 2024, 2025 and so on?
thanks
The only difference between the schedule is the start date.
THE FIRST Schedule ( I am hoping this report will run on JAN APR JUL OCT)
The second Schedule: ( And the second schedule, I am hoping it will run on FEB MAY AUG NOV)
Simply have a single job that executes monthly and in the t-sql job step
if month(getdate()) in (1,2,4,5,7,8,10,11)
begin
exec...
end
Related
I'm struggling to format a cron schedule for gcp properly and the docs aren't really helping me out.
Cron #1: Run every 50 minutes from 11:00 to 21:00 only on the months from march to october inclusive
schedule: every 50 minutes from 11:00 to 21:00 of mar,apr,may,jun,jul,aug,sep,oct
Cron #2: Run every day at 22:00 only on the months from march to october inclusive
schedule: every day 22:00 of mar,apr,may,jun,jul,aug,sep,oct
Neither of those work, but they were one of my attempts. What am I doing wrong here?
Referring to the Formatting the schedule docs below.
There is no supported syntax for your 1st cron:
specifying minutes in an [INTERVAL_VALUE] is only supported by END-TIME INTERVAL and START-TIME INTERVAL formats, but neither of them allows specifying months in the [INTERVAL_SCOPE].
the only format supporting month specification in [INTERVAL_SCOPE] is CUSTOM INTERVAL, but that only supports day specifications in [INTERVAL_VALUE].
But you can achieve an equivalent functionality by using the finer time specification in cron.yaml and making a check for the remaining conditions inside the cron job itself, doing nothing if the condition is not met. So your 1st cron would be achieved with:
this cron.yaml entry:
schedule: every 50 minutes from 11:00 to 21:00
an additional check for the current month inside the cron job itself, doing nothing (just returning) if the month is Jan, Feb, Nov or Dec.
Your 2nd cron is possible using a CUSTOM INTERVAL, you just need to place the hour at the end of the [INTERVAL_SCOPE]. From the doc:
[INTERVAL_SCOPE]: Specifies a clause that corresponds with the
specified [INTERVAL_VALUE]. Custom intervals can include the of
[MONTH] clause, which specifies a single month in a year, or a
comma-separated list of multiple months. You must also define a
specific time for when you want the job to run, for example: of
[MONTH] [HH:MM].
So your entry would be:
schedule: every day of mar,apr,may,jun,jul,aug,sep,oct 22:00
I need to return both the last instance of the 25th of the month before today and next instance of the 25th after today in SQL Server 2014.
I don't have code ready for this as I'm not SQL proficient
As an example
if Today is 28th June, I need to return 25th June and 25th July
If Today is 15th June, I need to return 25th May and 25th June
There wont be a case where this is requested on the 25th of each month, so no validation required there.
One idea using DATEFROMPARTS (assumes you're using 2012+, but with 2008 having 4 weeks of support left, a "safe" assumption no?):
SELECT DATEFROMPARTS(YEAR(V.Today),MONTH(V.Today),25),
DATEADD(MONTH,1,DATEFROMPARTS(YEAR(V.Today),MONTH(V.Today),25))
FROM (VALUES(DATEADD(DAY, -25,GETDATE()))) V(Today);
For today, that returns 2019-05-25 and 2019-06-25 and returns the correct values for your example: I.e.:
SELECT DATEFROMPARTS(YEAR(V.Today),MONTH(V.Today),25),
DATEADD(MONTH,1,DATEFROMPARTS(YEAR(V.Today),MONTH(V.Today),25))
FROM (VALUES(DATEADD(DAY, -25,'20190628'))) V(Today);
I'm trying to set a biweekly Cron job for a small Slack bot that I run at work off of App Engine. I previously had set it up in App Engine using the custom interval from the documentation to run every other week:
schedule: 1st,third Wednesday 10:00
I was waiting for the Slack bot to message today and it never came. Now the obvious solution is that I missed the 5th Wednesday of the month in my original scheduling statement. The problem here is that next week we will also get a message because it will again be the 1st Wednesday of the month. Is there a way to get the job to run as
schedule: every two Wednesday 10:00
or similar so that the job will run every two weeks?
In the documentation about formatting the schedule you can see under the "Custom Interval" tab what syntax options you have.
Given that you can specify days up to the 31st, I believe adding 5th to the expression you are using would fix the issue with "fifth" Wednesday:
schedule: 1st, 3rd, 5th Wednesday of month 10:00
If you don't mind the week day that you receive the update, you can also just set it to run every 14 days for simplicity:
schedule: 1, 14, 28 of month 10:00
I have DB job and now I would like to run this job only on specific dates. dates could be in any month like first run on 15 May 2015 and next run woule be 20 July 2015 .. 11 OCT 2015 etc etc.
Through scheduler we dont see such fluxibility. I guess this can be done through some function and call that in scheduler.
Could someone please help me on that.
Your task can be solved without using any additional functions, you can create a schedule with FREQ=YEARLY and with BYDATE clause you can specify an exact dates. For example:
BEGIN
dbms_scheduler.create_schedule('my_sched',
repeat_interval => 'FREQ=YEARLY;BYDATE=0515,0720,1011');
END;
This plsql block will create the schedule named my_sched which contains 15 May, 20 July and 11 OCT of every year. You can use this schedule while creating your jobs.
You can read more about oracle calendaring syntax here: http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sched.htm#BABEJGCH
Disclaimer: I am just looking for a logic not code
John discovered a strange island called Rasa. The years and weeks on the island are weird. Digging deeper into the island's calendar, he found out that it is similar to rest-of-the-world's (ROW) calendar but the island calendar's Year starts on 1st week of February's calendar. John is asking you to help him solve the problem of converting ROW's calendar into Island's calendar. Here is the question.
You are given a date (today's date). You have to determine the Island week's number. The catch here is that the Island year starts from 1st week of February and every Island's week starts from Sunday and ends on a Saturday. Write a SQL statement in SQL Server to achieve this. Use SQL Server functions and devise a logic.
Input parameter: Any date.
Output parameter: Week No in Rasa Calendar.
Here is an example:
Date: 5th May 2015 --
Week No in ROW Calendar:19
Week No in Rasa's Calendar:14
Date: Jan 1 2017:
Week No in ROW Calendar:1
Week No in Rasa's Calendar:49
My question: can this be achieved in SQL Server?
My homework: I tried a couple of ways to solve the problem.
Approach #1:
Step 1: Calculate the total no of days between today and Feb 1.
Step 2: Divide it by 7 and add 1 to the result.
Later found out that this approach will not work if Feb 1 is on any day other than Sunday.
Eg: 1st Feb is on Wednesday. 5th Feb will be on Monday
So, 1st Feb is on Rasa's week 1, and 5th Feb is on Rasa's week no 2. According to my approach 1st and 5th feb are on week 1 which is incorrect.
Approach #2:
I thought removing 5 weeks of Jan from ROW's calendar should work
select
case
when f.RasaWeek = -4 then 48
when f.RasaWeek = -3 then 49
when f.RasaWeek = -2 then 50
when f.RasaWeek = -1 then 51
when f.RasaWeek = 0 then 52
else
f.RasaWeek
end as Rasa_week,
f.year, f.month, f.date
from
(select
datepart(wk, date) - 5 as RasaWeek, *
from
<datetable>
where
Year(date) in (2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018)) as f
Info: I tested this on a <datetable> but this code will break if there is a 53rd week. Notice that I was not able to take care of the 53rd week.
Any inputs to solve this problem are welcome.
With dates, it's almost always easier to make a calendar table and store the data you care about rather than trying to do anything beyond basic date arithmetic. Use SQL's strengths: storing and retrieving data.
In this case, what you care about are all of the first Sundays in February. If you store these dates in a table, the solution is:
RETURN
SELECT TOP 1
DATEDIFF(day,[date],#input_date) / 7
FROM IslandCalendarStartDates
WHERE [date] <= #input
ORDER BY [date] DESC
This way you don't need to worry about leap years or 53-weeks, or any of the edge cases. Just count the days from the most recent first Sunday in February and divide by 7. If you need to change the solution to accommodate a different start date, you only change the data, not the code.