We are creating a SQL Server Table-value function to get weekdays between two dates. Logic is simple as DatePart(dw,Getdate()) returns integer value of the day. Sunday is equivalent to 1 and Monday is equivalent to 2 and so far. As we know weekdays are from Monday to Friday. So all the days between 2 and 6(including these two) are weekdays. Here is the code for the same.
Create function dbo.getWeekDays(@startdate date, @enddate date) returns @temptable TABLE (weekDays varchar(100),Name Varchar(100)) As Begin Declare @weekday date While @startdate <= @enddate Begin IF DATEPART(dw,@startdate) between 2 and 6 Begin Insert into @temptable(weekDays,Name) Values(@startdate,DATENAME (DW, @startdate)) End SET @startdate = DATEADD(DAY,1,@startdate) End Return End
Query to get weekdays
SELECT weekDays,Name from dbo.getWeekDays ('01/01/2016', '01/31/2016')