In SQL Server how to write the query to get the month in between the startdate and enddate, days(in month) for the Month , actual number of days in the month
startdate and enddate is passed as a input parameter , i need to know the number of months, actual days and the count of days in the month
eg :
Start date : 2012-04-02
End date : 2012-08-23
i need to get the result as,
Month Days ActualDays _ inMonth
----------------------------------------------
04 29 30 - (The no of days on Apr'12 )
05 31 31
06 31 31
07 31 31
08 31 31
04 -month
29 -(Frm startdate 2-Apr to till 30-Apr'12(Apr End))
30 - (The no of days on Apr'12 )
Here's a procedure to calculate the number of days in a month. I do not know what you mean by "actual days" or how that could be different.
if object_id('spMonthList') is not null
drop procedure spMonthList
go
create procedure dbo.spMonthList(
@start date
, @end date)
as
; with Months as
(
select dateadd(month, datediff(month, 0, @start), 0) as MonthStart
union all
select dateadd(month, 1, MonthStart)
from Months
where MonthStart < @end
)
select datepart(month, MonthStart) as Month
, datediff(day, MonthStart, dateadd(month, 1, MonthStart)) as NumberOfDays
from Months;
go
exec spMonthList '2012-04-01', '2012-08-01'
-->
Month NumberOfDays
4 30
5 31
6 30
7 31
8 31
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.