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
 T+3

Author  Topic 

avmreddy17
Posting Yak Master

180 Posts

Posted - 2008-07-31 : 22:25:40
I have a table with holiday date as Column

Holiday Date
2008-08-04 00:00:00.000
2008-09-01 00:00:00.000
2008-10-13 00:00:00.000
2008-12-25 00:00:00.000

I just want to pass the current date (Say T) and get a date (T+3) skipping Sunday,Saturday and any days in the Holiday table.

I have a Stored Proc which does this, but just wandering if there is
way to just write it with a simple select staement

If I run today ( JULY 31 ) , T+3 shud return 08/06/2008 Skiping Sat and Sunday and skipping 08/04 from the Holiday table also look into

Thx

ranganath
Posting Yak Master

209 Posts

Posted - 2008-08-01 : 02:01:42
Hi,

Try with this. May be this will help to u .


Declare @StartDate datetime ,@EndDate datetime , @Fromdate Datetime
Select @FromDate = '08/1/2008'


Select @StartDate = CONVERT(varchar(10),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),120)
SELECT @EndDate = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))


SELECT DATEADD(day,number,@StartDate) as 'Dates', datename( dw,DATEADD(day,number,@StartDate)) 'DayName' into #Temp
FROM master..spt_values
WHERE type='p' AND number BETWEEN 0 AND DATEDIFF(day,@StartDate,@EndDate)
ORDER BY 1

Declare @Temp Table (Dates Datetime)
Insert into @Temp
Select Dates From #Temp where DayName <> 'Sunday' and DayName <> 'Saturday'



Declare @Table Table (HolidayDate datetime)
Insert into @Table
Select '2008-08-04 00:00:00.000' Union All
Select '2008-09-01 00:00:00.000' Union All
Select '2008-10-13 00:00:00.000' Union All
Select '2008-12-25 00:00:00.000'


Select Dates
From
(
Select Dates, ROW_NUMBER()OVER ( ORDER BY DATES) AS 'ROW' From @Temp
where Dates Not in (Select HolidayDate From @Table)
AND Dates > @FromDate ) D
where Row = 3



Drop Table #Temp
Go to Top of Page
   

- Advertisement -