Schedule a task to run two times a day between two date - sql-server-2016

I use SQL Server 2017. I need to schedule a job to run at 12am and 12pm for each days between 7th and 27th of each months. In the other word, i need to run my job two times per day between two date in each month.
Can i do that in one schedule task or I have to create a two jobs for each exact day?
12 am 7th month
12 pm 7th month
and so on and so on.
If i have to create a job for each hover of each day i will have several schedule.
Update 1: I did it by creating several steps in schedule tab but i am looking to do that in less steps.
Update 2:
If i can create two steps like below it will good for me.
1 : Occurs every month between 7th and 27th at 12 am
2 : Occurs every month between 7th and 27th at 12 pm

One way to do that is to check if the date is in between 7th and 27th using DATENAME or DATEPART.
--IF (DATENAME(DAY, GETDATE()) >= 7 AND DATENAME(DAY, GETDATE()) <= 27)
IF DATEPART(DD, GETDATE()) BETWEEN 7 AND 27
BEGIN
EXEC [Your Stored Procedure]
END
And then set the Daily frequency to start at 12:00 AM and Occurs every 12 hours.

Related

SQL Date and TIme

I need to take the list of students with Application acceptance Date & time in University database. The report is sent on 6 PM evening to the management but the management can run it on 7 PM 8 PM or any time within the night. The application should be just 2 days ahead to the report running date.
ORACLE: I have created a query but that will give the application of entire 24 hours of a day. The problem with this query is when management runs a query on 6 PM and 8 PM , and any students are accepted in between this time. The result will be different.
select
to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from
tbl_application_accepted_date
where
to_date(application_accepted_date, 'DD-MON-rr:HH24:MI:SS') =
to_date(trunc(sysdate-2), 'DD-MON-rr:HH24:MI:SS')
;
If any application is accepted between 6 ahead I need the list in next days report. Means, I should have the list of accepted students from 6PM onwards of previous day to 5:59 application day.
I am getting application accepted after 6 PM, which I don't need in todays report, I should get this on next day report. The report is run on 4th OCT 2018
If it needs to cut off at 6pm, then just add 18/24 (18 hours) to the truncated date. You are also doing a lot of unnecessary casting. As long as application_accepted_date is a date field, then you can just compare it as a date.
select to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from tbl_application_accepted_date
where application_accepted_date >= trunc(sysdate-3)+18/24
and application_accepted_date < trunc(sysdate-2)+18/24
This will give you any applications starting at 6pm 3 days ago until just before 6pm 2 days ago.
EDIT: You could also do this with interval literals if you want. Same query as above, but more explicit in your intentions.
select to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from tbl_application_accepted_date
where application_accepted_date >= trunc(sysdate-3) + interval '18' hour
and application_accepted_date < trunc(sysdate-2) + interval '18' hour
Assuming that application_accepted_date is a DATE datatype in your data model, try this:
select
to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from
tbl_application_accepted_date
WHERE application_accepted_date BETWEEN to_date(To_char(Trunc(SYSDATE - 1),'YYYY-MM-DD')
||'18:00:00','YYYY-MM-DD HH24:MI:SS) and to_date(to_char(trunc(sysdate),'yyyy-mm-dd')||'17:59:59','yyyy-mm-dd hh24:MI:SS)
That should always return data from 6:00pm yesterday to 5:59:59pm today.

SSIS Package to archive data on monthly basis

Can any one let me know how can i create a ssis package which will serve the below mentioned criteria.
1) Extract new member joiner data on monthly basis .
2) Store the data on a separate table call 'Joiner' which has column member_name,join_date,member_class.
3) the job will be scheduled to run 1st day of every month.
For example the package will run on 1st April with a join_date 1st March to 31st Match and dump it to the 'joiner' table. Next month it will run on 1st May with a join_date of 1st April to 30th April.
I know i have to create a store procedure with a join date parameter to pass but my concern is how should i achieve this automation of passing date every month and archive joiners data on month and month basis.
Any help will be much appreciated.
It sounds like your question is about how to get the previous month of data on an automated basis. DATEADD() and EOMONTH() can accomplish your need of grabbing a rolling timeframe each month automatically if you are using SQL Server.
SELECT
getdate(), --current date
EOMONTH(GETDATE()), --last day of the current month
EOMONTH(GETDATE(),-1), --last day of the previous month
DATEADD(DAY, 1, EOMONTH(GETDATE(),-2)) --go back two months and add 1 day
Your query would need to include something like this in the WHERE clause.
WHERE join_date >= DATEADD(DAY, 1, EOMONTH(GETDATE(),-2))
AND join_date < EOMONTH(GETDATE(),-1)

SQL Server: Count number of records on weekly basis (Week = Thursday to Wednesday)

I need some help in writing an SQL in SQL Server where I need to count number of rows group by weeks. There is a tricky description of week which is following
- For any date before 08/13/2015 the week is of 7 days (i.e. from Thu through Wed)
- For date 08/13/2015 the week is consider a 9 day week (i.e. from Thursday through Friday so its between 08/13/2015 through 08/21/2015)
- For date 08/22/2015 the week is back to 7 days (i.e. Sat through Friday)
Now having said all the above the result I want to see in my report is the following way . NOTE: WE column in the below attached image is the last day of the week for the range.
Sample Result Image
Just write a case statement for the 3 different options. You can find the start day with something like this:
DATEADD(week, DATEDIFF(day, 3,getdate()) / 7, 3) -- Thursdays
DATEADD(week, DATEDIFF(day, 5,getdate()) / 7, 5) -- Saturdays
The numbers 3 and 5 come from the fact that day 0 (=1.1.1900) is Monday.
If you use this a lot, it might be a good idea to write a inline table valued function to return the dates you need.

Skip weekends Business day tasks NetSuite

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

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