How to find the date collection between two dates in SQL Server for mail delinquency?

Mathew Baker

I have to get the date collection of every month between two dates. I have no more idea in detail for SQL Server.

Example 1:

If my StartDate is '01/20/2017' (MM/dd/yyyy) and EndDate is '12/20/2017' (MM/dd/yyyy) than the expected result should be as given below.

2017-01-20
2017-02-20
2017-03-20
2017-04-20
2017-05-20
2017-06-20
2017-07-20
2017-08-20
2017-09-20
2017-10-20
2017-11-20
2017-12-20

Example 2:

If my StartDate is '01/30/2017' (MM/dd/yyyy) and EndDate is '12/20/2017' (MM/dd/yyyy) than the expected result should be as given below.

In this example StartDate is '01/30/2017' therefor in February month there is no 30th date in calandar so I need last date of this month. If any leap year will come in the range of these given dates than 29th date will be in result set.

Expected result

2017-01-30
2017-02-28
2017-03-30
2017-04-30
2017-05-30
2017-06-30
2017-07-30
2017-08-30
2017-09-30
2017-10-30
2017-11-30

Thanks in advance.

Dipak Delvadiya

Please try the below solution. Just set the MinDate and MaxDate as per your requirement. Also please check for the February month date in case of MinDate will be 30 or 31.

DECLARE @MinDate datetime,
        @MaxDate datetime

SELECT  @MinDate = '01/30/2017',
        @MaxDate = '12/17/2020'


DECLARE @FilteredDate DATETIME = GETDATE(); 

;WITH CTE AS
(
 SELECT  TOP (DATEDIFF(MONTH, @MinDate, @MaxDate) + 1) DATEADD(MONTH, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate) AS CurrentDate
 FROM    sys.all_objects a
 CROSS JOIN sys.all_objects b
)
SELECT * FROM CTE
WHERE CurrentDate > CONVERT(DATE, @FilteredDate) AND
CurrentDate <= CONVERT(DATE, @MaxDate)
ORDER BY CurrentDate ASC

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find if a specific date is between two different row dates in SQL Server?

SQL Server Find record between two dates

SQL Server: Find records between two dates

Get the Date Difference Between two dates excluding fridays in SQL Server

SQL Server - Check date field between two dates in where clause

SQL Server Sum data between two dates group by date

Find the days difference between two dates per month for employee with same id and different start and end date in sql server 2008

how to Difference between two date SQL server

JPA find by date between two dates

Find missing date ranges between two dates

How to get dates between a date range given a day in SQL Server

How to get seconds between two dates in SQL Server?

How to Sum total between two given dates in sql server 2008

How to get a list of months and year between two dates in SQL Server

years between two dates in sql server with starting and end date of each of them in sql server

How to find the difference between two date cells of different rows in sql server 2012?

SQL: Verify if date is between two dates

How to find dates between two dates excluding start and end date using python

Sql query to find the dates between two

Dates in between the Date Range - SQL Server

Date between said dates in sql server with null

Calculate SUM between two dates SQL SERVER

Days between two dates In SQL Server

SQL query to find all Data by date range which falls between two dates

How to check if date is between two dates in python

How to check a date exists in between two dates?

PHP - How to find if a date is between 2 dates

Sql Server : Select Date between two dates and Display all results including duplicate

How to find data between two dates