I have a table with 4 fields .Start-date, Duration, End_date, Actual_date.
I am providing the start_date and duration as the input to calculate end-date.
While calculating the end_date the Saturday and Sundays are not considered.
Also if the actual_date is having value that value is saved as end_date.
Example : Start_date = 18/05/2020 , Duration : 10. So the End_date should be calculated by excluding 23/05/2020 and 24/05/2020.
I had tried different ways to archive this.
The below is the alter query for the column end_date.
This is meeting half of my requirement but dont know how to exclude the weekends (Saturday and Sunday) from the start_date and duration.
ALTER TABLE [DBName].[dbo].[TestDate] ADD End_Date as CAST(
CASE
WHEN (Actual_Date='' or Actual_Date IS NULL) then DATEADD(day, Duration, Start_Date)
ELSE Actual_Date
END as DATE
)
GO
Please help me on this.
Best Answer
Iterative solution:
fiddle
PD. It is optimal hardly... for large
duration
values you may int. divide it by 7 and add according weeks (5 days per each) then iterate by int. reminder only.