Multiple SQL SERVER Queries to get all dates between two dates
In this Post We Will Explain About is Multiple SQL SERVER Queries to get all dates between two dates With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Select Date Range Between Two Column and Two Dates Example
In this post we will show you Best way to implement SQL Server Function to return a range of dates, hear for generate list dates between date range – SQLServer with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.
How to search between two dates and get all records
Using Common Table Expression (CTE)
I have mentioned some two ways using simple CTE . We can use simple any of the two. Both some works fine.
SQL SERVER Query 1:
DECLARE @FirstDate AS DATE='2014-12-17'; DECLARE @SecodDate AS DATE='2014-12-25'; ;WITH studentReg AS ( SELECT @FirstDate AS [Date],DATENAME(DW,@FirstDate) AS [DayName] UNION ALL SELECT DATEADD(DAY,1,[Date]),DATENAME(DW,DATEADD(d,1,[Date])) AS [DayName] FROM studentReg WHERE DATE < @SecodDate ) SELECT [Date],[DayName] FROM studentReg OPTION (MAXRECURSION 0)
SQL SERVER Query 2:
DECLARE @FirstDate AS DATE='2014-12-17'; DECLARE @SecodDate AS DATE='2014-12-25'; ;WITH studentReg AS ( SELECT CAST(@FirstDate AS DATETIME) AS iDate ,DATENAME(DW,@FirstDate) As iDayName UNION ALL SELECT iDate + 1 ,DATENAME(Dw,iDate + 1) As iDayName FROM studentReg WHERE iDate + 1 <= @SecodDate ) SELECT CONVERT(VARCHAR(10),iDate,120) AS [Date] , iDayName AS[DayName] FROM studentReg OPTION (MAXRECURSION 0)
Using Table Variable
DECLARE @FirstDate AS DATE='2014-12-17'; DECLARE @SecodDate AS DATE='2014-12-25'; DECLARE @DateList TABLE (iDate DATE,iDayName VARCHAR(10)) WHILE (@FirstDate<=@SecodDate) BEGIN INSERT @DateList(iDate,iDayName) VALUES(@FirstDate,DATENAME(DW,@FirstDate)) SET @FirstDate=CAST(DATEADD(DAY,1,@FirstDate) AS DATE) END SELECT iDate AS [Date], iDayName AS [DayName] FROM @DateList
SQL SERVER Using Temporary Table
DECLARE @FirstDate AS DATE='2014-12-17'; DECLARE @SecodDate AS DATE='2014-12-25'; CREATE TABLE #DateList(iDate DATE,iDayName VARCHAR(10)) WHILE (@FirstDate<=@SecodDate) BEGIN INSERT #DateList(iDate,iDayName) VALUES(@FirstDate,DATENAME(DW,@FirstDate)) SET @FirstDate=CAST(DATEADD(DAY,1,@FirstDate) AS DATE) END SELECT iDate AS [Date], iDayName AS [DayName] FROM #DateList
You are Most welcome in my youtube Channel Please shubscibe my channel. and give me feedBackMore Details……
Angularjs Example
I hope you have Got What is How to search between two dates and get all records And how it works.I would Like to have FeaeBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.