Count No Of Days in SQL Server

Declare @startdate datetime
Declare @enddate datetime
Declare @count int
Declare @NameOfDay varchar(15)

Declare @day int
set @count=0
set @startdate='2013-10-01'
set @enddate='2013-10-30'
Set @NameOfDay='Tuesday'

 if(@NameOfDay='Sunday')
 Begin
 set @day=1
 End
 Else  if(@NameOfDay='Monday')
 Begin
 set @day=2
 End
 
 Else  if(@NameOfDay='Tuesday')
 Begin
 set @day=3
 End
 Else  if(@NameOfDay='Wednesday')
 Begin
 set @day=4
 End
 Else  if(@NameOfDay='Thursday')
 Begin
 set @day=5
 End
 Else  if(@NameOfDay='Friday')
 Begin
 set @day=6
 End
 Else  if(@NameOfDay='Saturday')
 Begin
 set @day=7
 End
    while @startdate<=@enddate
    Begin
        IF DatePart(WEEKDAY,@startdate)= @day
            SET @count=@count+1
        SET @startdate=DateAdd(d,1,@startdate)
    END
select @count

Ebook Download
View all
Learn
View all