Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Working days only date

Author  Topic 

ranvir_2k
Posting Yak Master

180 Posts

Posted - 2015-01-29 : 06:34:09
Hi all,

I need to calculate a date.

For example it needs to be 20 working days ago compared to today so that means it needs to not include any Saturday or Sunday in between


declare @start_date datetime
declare @end_date datetime

declare @working_days int


set @working_days = 20

set @end_date = getdate()

set @start_date = @end_date - @working_days

select @start_date



So I need to calculate @start_date but it needs to exclude any weekend days.

@working_days is the number of working day I'm interested in.

Thanks.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-29 : 07:03:33
the best way to tackle this sort of problem is to create a special table called a date dimension is pretty easy to do and you can flag days as working days and not working days in the table then you simply join on that table to get the results you're looking for. It tends to be much easier and much more reliable than that tempting to calculate the working days.
Go to Top of Page
   

- Advertisement -