Get all the dates between two dates in SQL Server

CREATE FUNCTION [dbo].[GetDatesInBetween](@FromDate DATETIME, @ToDate DATETIME, @IncludeWeekends BIT )

RETURNS @DateTable TABLE(DATE DATETIME)
AS
BEGIN
INSERT INTO @DateTable (DATE) VALUES (@FromDate)
WHILE @FromDate < @ToDate
BEGIN
SELECT @FromDate = DATEADD(D, 1, @FromDate)
IF @IncludeWeekends = 1
INSERT INTO @DateTable (DATE) VALUES (@FromDate)
ELSE IF DATENAME(dw, @FromDate) <> 'Thursday' AND DATENAME(dw, @FromDate) <> 'Friday'
INSERT INTO @DateTable (DATE) VALUES (@FromDate)
END
RETURN
END

Comments

Popular posts from this blog

മലയാളത്തില്‍ മൊത്തം എത്ര അക്ഷരങ്ങള്‍ ഉണ്ട്?

Convert Number To Words in SQL Sever

Crystal reports load report failed: Could not load file or assembly CrystalDecisions.Web, Version=10.2.3600.0