December 10, 2009 at 1:57 am
I have from date and to date. I want to find the number of days between these two dates removing the weekends. Can anyone help?
Planning to write something like as follows:
DECLARE @TotalDays INT
DECLARE @FromDate VARCHAR(15)
DECLARE @ToDate VARCHAR(15)
SET @FromDate = '12/01/2009'
SET @ToDate = '12/12/2009'
IF(datediff(dd,@FromDate,@ToDate)<6)
BEGIN
IF(DAtepart(DW,@FromDate)=1)
BEGIN
PS: This is not complete code.
December 10, 2009 at 2:18 am
Use a calendar table.
December 10, 2009 at 2:36 am
I had to do this recently..
This is what i found:
DECLARE @DateOld datetime, @DateNew datetime SET @DateOld = '23-Nov-2009' SET @DateNew = GETDATE()
SET DATEFIRST 1
SELECT DATEDIFF (day, @DateOld, @DateNew) -
(2 * DATEDIFF(week, @DateOld, @DateNew)) -
CASE WHEN DATEPART(weekday, @DateOld + @@DATEFIRST) = 1 THEN 1 ELSE 0 END -
CASE WHEN DATEPART(weekday, @DateNew + @@DATEFIRST) = 1 THEN 1 ELSE 0 END
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply