I have two date for example 08/08/2013 and 11/11/2013 and I need last date of each month starting from August to November in a table so that i can iterate over the table to pick those dates individually.
I know how to pick last date for any month but i am stucked with a date range.
kindly help, it will be highly appreciated.
Note : I am using Sql 2008 and date rang could be 1 month , 2 month or 6 month or a year or max too..
try this the last row(where) is optional for date filtering
The Example Below is for all dates
Following script demonstrates the script to find last day of previous, current and next month.
If you want to find last day of month of any day specified use following script.
Source - SQL Server Central.
No need to use a common table expression or anything like that - this simple query will do it:
Output:
Example: 1
Example: 2
You can use CTE for getting all last days of the month within the defined range
Although the question is about the last day which @bummi has already answered.
But here is the solution for the first date which might be helpful for someone.
Get the first dates of all the months in-between the
@FromDate
and@ToDate
.Here is the result