Executing a sql script every month for the first and last 16 days. - sql-server

I have a script that I have had to ran manually a few times but now it looks like it will need to be ran twice a month. The problem is the dates are dynamic and the job needs to run on the 1st and 16th of every month but it will need to pull all data from the last 16 days. So for example, if we run it on the first of this month (Jan) the dates would be >= 2016-12,16 and <= 2016-12-31
I have been looking at the date function sql server has but I am not really sure if it will work for what I need.

I would look at DateAdd, and add a negative number of dates to the value to go back 16 days.
DATEADD(DAY, -16, GETDATE()).
I would personally just run a script with cron and set it up to run on the 1st and the 16th of every month.

Related

Cron Script to execute a job every 14 days from a given date in specific time zone

I want to execute a Job in CRON for every 14 days from a specific date and timezone.
As an e.g. from JUNE 24TH every 14 days in CST time zone.
Run job every fortnight
The easy way
The easiest way to do this is simply to create the task to run every 14 days from when you want it to first run like:
CREATE TASK mytask_fortnightly
WAREHOUSE = general
SCHEDULE = '20160 MINUTE'
AS
SELECT 'Hello world'
How it works
As there are 60 minutes in an hour, 24 hours in a day and 14 days in a fortnight, ergo that's 20,160 minutes.
Caveat
The above solution does not run the task every fortnight from a given date/time, but rather every fortnight from when the task is created.
Even though this is the simplest method, it does require you to be nominally present to create the task at the exact desired next scheduled time.
As a workaround however, you can create a one-shot task to do that for you the very first time at the exact correct date/time. This means you don't have to remember to be awake / alert / present to do it manually yourself, and you can clean up the creation task afterwards.
The harder way.
Other solutions will require you to create a task which gets run every Thursday (since 2021-06-24 is/was a Thursday, each subsequent Thursday will either be the off-week, or the fortnight week)
e.g. SCHEDULE = 'USING CRON 0 0 * * THU'
Then you will add specific logic to it to determine which one the correct fortnight is.
Using this method will also incur execution cost for the off-week as well to determine if it's the correct week.
Javascript SP
In javascript you can determine if it's the correct week or not by subtracting the start date from the current date and if it's not a mutiple of 14 days, use this as a conditional to short circuit the SP.
const deltaMs = (new Date) - (new Date('2021-06-24'));
const deltaDays = ~~(deltaMs / 86400000);
const run = deltaDays % 14 === 0;
if (!run) return;
// ... continue to do what you want.
SQL
You can also check if it's a fortnight using the following SQL condition in a WHERE clause, or IFF / CASE functions.
DATEDIFF('day', '2021-06-24', CURRENT_DATE) % 14 = 0

Group by 24hr Period starting at 7:00 AM

I have some high frequency data that I need to group by 24hr period starting at 7:00 AM. How can I do that?
For example, there is a timestamp every 5 minutes and I want to group it by day, but each day starts at 7:00 AM.
How would I go about doing that?
SQL Server 2016.
I am going to presume your time is probably in UTC.
I would look at converting the time to the timezone you need. Then it gives you more options of how you want to deal with the data, also you could customize the report for people in different zones. For some info about timezones I found this stackoverflow post.
Convert datetime value from one timezone to UTC timezone using sql query
Something like this in an group by clause should work. The code below says if the hour is under 8 then it's still yesterday, else it's really today. You would need to replace the getdate() with the correct column. If I Am correct about that the times are in UTC/GMT I would recommend looking at just converting it to the timezone you want.
case when datepart(hh, getdate())<8 then
cast((DATEADD(day, -1, getdate())) as date)
else
cast(getdate() as date)
end

Limiting datediff by Hours and Days

Long time lurker and now i have my first question:
I'm designing a SQL Report. One Task is to calculate the amount of minutes between two Times. They can be the same day or on different days. In the Database there are 4 Columns given
The Start Date (as Datetime e.g. 24.10.2017 00:00:00)
The Start Time (as Datetime e.g. 01.01.1899 11:25:00)
The End Date (formated as above)
The End Time (formated as above)
I'm calculating three Filds, all in Minutes
Days between: =DateDiff("n", Fields!StartDatum.Value,Fields!QualDatum.Value)
Minutes between: =DateDiff("n",Fields!StartZeit.Value,Fields!QualZeit.Value)
Adding those up: =Fields!QualiZeitTage.Value+Fields!QualiZeitMinuten.Value
All of this is working great and produces the desired output.
My Problem is, that i don't need the full time between those events. I only want to count minutes that are between 7:00 am and 8:00 pm. Also, i want to exclude Saturdays and Sundays. How would i go about limiting the datediff function to my desired times?
Second Problem: The Endtime and Date are only written when the event actually is finished. If it's still ongoing those Fields are empty producing a negative number (-1060764480 for example). Since i'm only using those to produce Boolean output on surpassing a certain length, it's no problem. I would like to handle that more "cleanly" though. Any thoughts?

How to get the last 7 Days Dates in SSRS

I'm having a Report where I need to display the dates of last 7 days.
As shown below
Sun, Mon etc... are hard coded and the Dates are written in Expression
For example,
If today is Wednesday I need to show the Dates till last Tuesday.
If it is Thursday I need to show the Dates till last Wednesday.
How to retrieve the dates information and display below the corresponding days.
To get last seven days date you can do something like below in each expression,
To get Days in Header
=WeekdayName(weekday(Parameters!TodaysDate.Value)) --Tuesday
=WeekdayName(weekday(DateAdd("d",1,Parameters!TodaysDate.Value))) --Wedneday
Same for others too... Just by increment/decrement by 1.
To get Days Date in Data
=Format(Parameters!TodaysDate.Value,"dd-MMM-yyyy")
=Format(DateAdd("d", 1, Parameters!TodaysDate.Value),"dd-MMM-yyyy")
Same for others too... Just by increment/decrement by 1.
Here TodaysDate would be parameter date or Now() Date.
I have given demo of incremental one, you can change it as per your condition. I think you need to use decrement here. So Instead of 1 you need to use -1.
This will give you output like below,

Setting Report date parameters

I have a report written in visual studio running on a SQL server 2005 DB.
I want it to automatically apply the day before's date
- start at 00:00:00 and end 23:59:59
From my pic the code I use is
=DateAdd(Dateinterval.day, -1, Now()) for the start date
and for end date I use
=Now()
These values get me a report for the last 24 hours but I want to get the report to run just for the 24 hours of the previous day. How would I do this?
I think you should be using Today() instead of Now().
Both will give today's date, but Today() will not include any time portion.
You should only use one date paramater being yesterday =DateAdd(DateInterval.Day, -1, Today()).
Then to use this your data should be truncated to the date only and filtered to equal this parameter getting yesterday only irrispective of the time.

Resources