I have the follwoing query:
set language 'english'
DECLARE #MyDate DATETIME
SET #MyDate = dateadd(dd,-1,dateadd(mm,datediff(mm,0,getdate()),0))
SELECT ReportEndDate = DATEADD(dd, CASE
WHEN DATENAME(weekday,#MyDate) = 'Saturday' THEN 5
WHEN DATENAME(weekday,#MyDate) IN ('Monday','Sunday') THEN 4
ELSE 6 END, #MyDate)
Which as you can see retuns the month end day + 4 working days.
I need to expand this to include Christmas and New Year. So that the above query takes those bank holidays into consideration when working out the 4th working day of the new month.
Any pointers would be much apreciated.
Build a calendar table.
http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html
Related
Consider the date "2022-07-02"
For the month July first week only have 3 days in it.
I need to find the number of days in the week for the given date.
In above date the week has 3 days where "2022-07-02" day reside.
Example 2 :
For month June in 2022 first week has 5 days in the week
Therefore if i declare a date as "2022-06-03" it should pass the number of days in the week as 5
I need a query to find the number of days for the specific week.
set datefirst 1 -- assumes Monday is set as start of week
declare #myDate date = getdate();
-- calculate the next last day of week
declare #nextSunday date = dateadd(day, 7 - datepart(weekday, #myDate), #myDate);
select case
-- advancing into the next month implies a partial week
-- datediff(month, #myDate, nextSunday) = 1 would be equivalent
when day(#nextSunday) < day(#myDate) then 7 - day(#nextSunday)
-- else see if still within first week
when day(#nextSunday) < 7 then day(#nextSunday)
else 7 end;
Within a query you might use it this way:
select case
when day(nextSunday) < day(dateColumn) then 7 - day(nextSunday)
when day(nextSunday) < 7 then day(nextSunday)
else 7 end
from
myData cross apply (
values (dateadd(day, 7 - datepart(weekday, dateColumn), dateColumn))
) v(nextSunday);
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=ee5bfb52dabe31dd619cfd136689db59
If you don't want the shorthand form then just replace every instance of nextSunday in the final step with its full expression.
There's nothing in the logic that prevents this from working with another first day of week. I just chose a variable name that helped ellucidate this particular problem.
I am trying to write a SQL query that shows the count of the days and date depending upon the financial period it falls in. The financial period starts 5 days before the month end eg march 27th to 26th April
For the above mentioned period if the day is 29th march, the count of the day should be 3 and the date should be 2020-04. The date should adjust depending upon the period it falls in.
I tried to adress the second part of this query by writing the below script but it does not bring any result
declare #date datetime
set #date = getdate()
SELECT format (date,'yyyy-MM-dd') as date
where #date
between dateadd(day,-5,EOMONTH(getdate(),-1)) and dateadd(day,-5,EOMONTH(getdate()))
updated to include 5 days from previous month.
Is this what you are looking for the second part?
Edit: Modifying the query. I guess this should give you what you need for both the parts.
you can try changing the dates in set #date.
Please note that the -4 instead of -5 is done intentionally as you said the financial month starts 5 days earlier. For March, 31 - 5 would give 26, but it should start on 27 right? so that on 29th the no. of days should be 3 including 27 and 29. Anyways, the query should be self explanatory, might just need to change the number depending on your requirement.
declare #date datetime
--set #date = getdate()
set #date = '2020-02-14'
SELECT format(#date,'yyyy-MM-dd') as date,
case WHEN #date < dateadd(day,-4,EOMONTH(#date)) THEN format(EOMONTH(#date),'yyyy-MM')
ELSE format(EOMONTH(#date, 1),'yyyy-MM') END
AS FinancialMonth,
CASE WHEN #date < dateadd(day,-4,EOMONTH(#date)) THEN DATEDIFF(day,dateadd(day,-5,EOMONTH(#date,-1)), #date)
ELSE DATEDIFF(day, dateadd(day,-5,EOMONTH(#date)), #date) END
AS CountDays
The following code will calculate what you need. Please read the comments in the code itself.
-- First, lets declare all the variables we need.
-- I tried to name them so they are selfexplanatory.
declare #inputdate DateTime,
#financialPeriodStartDate DateTime,
#EndOfTheMonth DateTime,
#nextFinantialMonthYear DateTime,
#previousFinancialPeriodStartDate DateTime
-- Now we calculate some of the values
set #inputdate = GETDATE() -- Let's use Today's date, but you can change this to any date.
set #EndOfTheMonth = EOMONTH(#inputdate) -- This is the end of the month for the input date
set #financialPeriodStartDate = DATEADD(DAY,-5,EOMONTH(#inputdate)) -- This is you Financial Period Start Date for the giving input date.
set #previousFinancialPeriodStartDate = DATEADD(DAY,-5,EOMONTH(DATEADD(MONTH, -1, #inputdate))) -- Also calculates the Start Date of the precious financial period.
set #nextFinantialMonthYear = DATEADD(MONTH, 1,#financialPeriodStartDate) -- This is the start date of the next financial period.
-- In the following Select, it calculates the values you need, based in the previous variables.
select
#inputdate as Today,
#EndOfTheMonth as EndOfMonth,
#previousFinancialPeriodStartDate as PreviousFinancialPeriodStartDate,
#financialPeriodStartDate as FinacialPeriodStartDate,
CASE WHEN DATEDIFF(DAY , #financialPeriodStartDate, #inputdate + 1 ) < 0
THEN DATEDIFF(DAY , #previousFinancialPeriodStartDate, #inputdate + 1 )
WHEN DATEDIFF(DAY , #financialPeriodStartDate, #inputdate + 1 ) >=0
THEN DATEDIFF(DAY , #financialPeriodStartDate, #inputdate + 1 )
END as DaysFromFPStartDate,
STR(YEAR(#nextFinantialMonthYear)) +'-'+LTRIM(RTRIM(STR(MONTH(#nextFinantialMonthYear)))) as NextFinantialPeriodMonthAndYear
I hope it helps.
I dont know about other countries but in South Africa we have a public holiday on a Monday if the Sunday was a Public holiday. I need to write an Update statement that will return the date after x number of days and should the date be a monday after a public holiday it should add an additional day. My simple update statement looks like:
UPDATE tbl_ProjectTracker
set Predicted_Date = DATEADD(DAY, 20, Actual_Eng_Start_date)
I already have a table for referencing but I am not sure if I should determine the additional day on the table or on the Update statement.
Please assist
You should have a PublicHoliday table
Then you could Update predicted_date like that
DECLARE #PublicHoliday AS TABLE
(
HolidayDate date
)
DECLARE #NumberDaysPredict int = 20
UPDATE pt
set pt.Predicted_Date = DATEADD(DAY, #NumberDaysPredict + hl.NumberHolidaysOnSunday, pt.Actual_Eng_Start_date)
FROM tbl_ProjectTracker pt
CROSS APPLY
(
SELECT Count(*) AS NumberHolidaysOnSunday
FROM #PublicHoliday ph
WHERE ph.HolidayDate BETWEEN pt.Actual_Eng_Start_date AND DATEADD(DAY, #NumberDaysPredict, Actual_Eng_Start_date)
AND Datepart(dw,ph.HolidayDate) = 1 -- Sunday
) hl
A simple way would be a case construct for the number of days:
UPDATE tbl_ProjectTracker
set Predicted_Date = DATEADD(
DAY,
CASE WHEN Actual_Eng_Start_date IN
(select day from public_holidays where datepart(dw, day) = 1) THEN 21 ELSE 20 END,
Actual_Eng_Start_date)
Unfortunately SQL Server's date / time functions are weak to say the least. The result of DATEPART(dw, ...) depends on a setting (DATEFIRST), so the query is kind of unreliable. Always make sure the setting is correct when you run it. It has been requested to extend DATEPART such as to accept an optional parameter for DATEFIRST so you'd get a consistent query, but Microsoft has closed this as "unsolvable" (https://connect.microsoft.com/SQLServer/feedbackdetail/view/432303/datepart-dw-date-should-allow-optional-date-first-Parameter).
So, pilots have to get so many flight hours and flight sorties within every 6 months (semi annual) as well as every year (annual). The biggest pain is that the start and ending dates of these periods are based upon their birth month.
You can find more information about my schema here: Design decision: Table schema for partial dates in order to calculate time spans (SQL Server)
DECLARE #date Date
SET #date = '1985-04-12'
DECLARE #diffInYears INT
SET #diffInYears = DATEDIFF(yy, #date, GETDATE())
DECLARE #currentBirthDate Date
SET #currentBirthDate = (SELECT dateadd(yyyy, #diffInYears, #date))
SELECT DATEADD(dd, -DAY(DATEADD(m,1,#currentBirthDate)), DATEADD(m,7,#currentBirthDate)) semiAnnualDateEnd,
DATEADD(dd, -DAY(DATEADD(m,1,#currentBirthDate)), DATEADD(m,13,#currentBirthDate)) annualDateEnd
RESULTS:
semiAnnualDateEnd - annualDateEnd
2012-10-31 - 2013-04-30
Now, this is great, these are the dates I want for this particular example.
However, when we come to November 1st, 2012, I want the semiAnnualDateEnd to become 2013-04-30.
Also, when 2013 comes around (January 1st, 2013), the annualDateEnd is going to become 2014-04-30, when I want it to stay 2013-04-30 until 2013-05-01 comes around, and then for it to become 2014-04-30 (similar situation for the semiAnnual).
I don't want to keep these dates statically associated with a particular pilot. That is, I don't want to keep a couple fields in the Pilot table that have these. However, I want to use these for displaying and calculations. For example, going to need to display each pilot's current semiAnnual and annual sorties and flight hours, as well as displaying a snapshot their "current" stats at any particular point in time.
EDIT: I'm using SQL Server 2008 Express RC
EDIT 2: I'm thinking I should change the #currentBirthDate to (SELECT DATEADD(yyyy, #diffInYears - 1, #date). Then, I need to do a case statement below (continuing experimentation)
The rule for the semi-annual date seems to be: add five months to the month and go to the end of the month. End of the month can be a problem. So, let's change this to "add six months, got to the end of the month and subtract one day". (I am basing this logic on your example.)
The following expression does this:
select dateadd(d, -1,
cast(cast(year(bd)+(case when month(bd)+6 > 12 then 1 else 0 end) as varchar(255))+'-'+
cast(case when month(bd)+7> 12 then month(bd)+6-12 else month(bd) end as varchar(255))+'-'+
'1' as date))
from (select cast('2011-11-01' as date) bd) t
It does the date arithmetic on the year() and month() values of the date. It then gloms them back together as a string, converts to a date, and subtracts 1 day.
I think something similar will work for your year dates as well.
DECLARE #pilotID INT
SET #pilotID = 1
DECLARE #birthDate DATE
SET #birthDate = (SELECT birthDate FROM Pilot WHERE pilotID = #pilotID)
DECLARE #diffInYears INT
SET #diffInYears = DATEDIFF(yy, #birthDate, GETDATE())
DECLARE #currentBirthDate DATE
SET #currentBirthDate = (DATEADD(yyyy, #diffInYears - 1, #birthDate))
DECLARE #firstSixMonthStart DATE
DECLARE #firstSixMonthEnd DATE
DECLARE #secondSixMonthStart DATE
DECLARE #secondSixMonthEnd DATE
SET #firstSixMonthStart = (DATEADD(dd,-(DAY(DATEADD(m,1,#currentBirthDate))-1),DATEADD(m,1,#currentBirthDate)))
SET #firstSixMonthEnd = (DATEADD(dd, -DAY(DATEADD(m,1,#currentBirthDate)), DATEADD(m,7,#currentBirthDate)))
SET #secondSixMonthStart = (DATEADD(dd,-(DAY(DATEADD(m,1,#currentBirthDate))-1),DATEADD(m,7,#currentBirthDate)))
SET #secondSixMonthEnd = (DATEADD(dd, -DAY(DATEADD(m,1,#currentBirthDate)), DATEADD(m,13,#currentBirthDate)))
DECLARE #semiAnnualStart AS DATE
DECLARE #semiAnnualEnd AS DATE
DECLARE #annualStart AS DATE
DECLARE #annualEnd AS DATE
SET #semiAnnualStart = CASE
WHEN GETDATE() > (DATEADD(dd, -DAY(DATEADD(m,1,#firstSixMonthEnd)), DATEADD(m,7,#firstSixMonthEnd)))
THEN (DATEADD(yyyy, 1, #firstSixMonthStart))
WHEN GETDATE() > #firstSixMonthEnd
THEN #secondSixMonthStart
ELSE #firstSixMonthStart
END
SET #semiAnnualEnd = CASE
WHEN GETDATE() > (DATEADD(dd, -DAY(DATEADD(m,1,#firstSixMonthEnd)), DATEADD(m,7,#firstSixMonthEnd)))
THEN (DATEADD(yyyy, 1, #firstSixMonthEnd))
WHEN GETDATE() > #firstSixMonthEnd
THEN #secondSixMonthEnd
ELSE #firstSixMonthEnd
END
SET #annualStart = CASE
WHEN GETDATE() > #secondSixMonthEnd THEN (DATEADD(yyyy, 1, #firstSixMonthStart))
ELSE #firstSixMonthStart
END
SET #annualEnd = CASE
WHEN GETDATE() > #secondSixMonthEnd THEN (DATEADD(yyyy, 1, #secondSixMonthEnd))
ELSE #secondSixMonthEnd
END
SELECT #semiAnnualStart semiStart, #semiAnnualEnd semiEnd,
#annualStart annualStart, #annualEnd annualEnd,
#firstSixMonthStart firstStart, #firstSixMonthEnd firstEnd,
#secondSixMonthStart secondStart, #secondSixMonthEnd secondEnd,
COUNT(*) semiSorties, ISNULL(SUM(hours), 0) semiSortieHours
FROM PilotLog
WHERE pilotID = #pilotID
AND topLevelPosition = 'AVO'
AND flightDate BETWEEN #semiAnnualStart AND #semiAnnualEnd
RESULTS:
semiStart semiEnd annualStart annualEnd firstStart firstEnd secondStart secondEnd semiSorties semiSortieHours
2012-05-01 2012-10-31 2011-11-01 2012-10-31 2011-11-01 2012-04-30 2012-05-01 2012-10-31 1 1.7
This ended up working... Problem is, I'm going to need to do this for every pilot on the overall summary page. Additionally, I'm going to need to calculate this kind of sortie information for snapshots that she wants. She wants to go back to any particular month and see a snapshot, listing each flight as well as the sortie stats they had upon completing that flight. This SQL should work for these situations, it's just I'll have to change it up a bit here and there.
(EDIT: Why doesn't my code scroll horizontally..? I don't want it to wrap. Nevermind that's just internet explorer looks like)
I know this is quite a generic question but does anyone know a good way of checking if the date is the last monday of the month using T-SQL. I need to use it in a stored procedure to determine if the stored procedure returns data or does nothing.
Cheers!
The following select will return 1 if the current date is the last monday of the month, and 0 if not.
select
case
when datepart(dw, GETDATE()) = 2 and DATEPART(month, DATEADD(day, 7, GETDATE())) <> DATEPART(month, GETDATE())
then 1
else 0
end
datepart(dw, GETDATE()) returns the day of the week. Monday is 2. The second part adds 7 days to the current date and checks that within 7 days the month has changed (if it does not, it is not the last monday).
Change the GETDATE()'s to any date you want to check.
EDIT:
You can make it into a generic function and use it with any date you like:
CREATE FUNCTION
IsLastMondayOfMonth(#dateToCheck datetime)
RETURNS bit
AS
BEGIN
DECLARE
#result bit
SELECT #result =
CASE
WHEN datepart(dw, #dateToCheck) = 2 AND DATEPART(month, DATEADD(day, 7, #dateToCheck)) <> DATEPART(month, #dateToCheck)
THEN 1
ELSE 0
END
RETURN #result
END
Maybe something like this:
DECLARE #YourDate DATETIME='2012-02-25'
SELECT
CASE
WHEN #YourDate = DATEADD(wk, DATEDIFF(wk,0,DATEADD(month,DATEDIFF(MONTH,0,#YourDate),30)),0)
THEN 1
ELSE 0
END
Although you have already accepted another answer, an even simpler solution (in my opinion) is to use a calendar table that has a column called IsLastMondayOfMonth or something similar. Calendar tables tend to be much easier to maintain than functions because not only is the code much cleaner but also when an exception comes along ("because of an unusual public holiday this year we need to do our month-end processing one day later, I'm sure you can fix the system to do that?") you can just update a table to handle it instead of adding potentially awkward logic to your code.
This will select the date when the date is the last monday of the month regardless of firstdate. Making a function that rely on the database settings, is really not good practice.
select d
from (select '2012-02-20' d union all select '2012-02-27' union all select '2012-02-28') a
where datepart(day, dateadd(day, 7 ,d)) < 8 and datediff(day, 0, d) %7 = 0