Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql query needs to include bank holidays

Tags:

sql

t-sql

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.

like image 248
Ross Avatar asked Apr 02 '26 16:04

Ross


1 Answers

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

like image 129
Stuart Ainsworth Avatar answered Apr 04 '26 06:04

Stuart Ainsworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!