DECLARE @date datetime;
Set @date = GETDATE ()
--the number of days between the first day of the month and the first of the following month
Select DateDiff (Day,dateadd (Month,datediff (month,0, @date), 0), DateAdd (Month,1+datediff (month,0, @date), 0)
--Using variable simplification
Select @date = DATEADD (Month,datediff (month,0, @date), 0)--first day of the month
Select DateDiff (Day, @date, DateAdd (month,1, @date))
--Another way of thinking: the date of the last day of a given month, as the number of days of the month
Select Day (DateAdd (Month,datediff (month,-1, @date),-1))
Go
--Calculates the number of days in the quarter of the given date
DECLARE @date datetime;
Set @date = GETDATE ()
--the number of days between the first day of the quarter and the first of the next quarter
Select DateDiff (Day,dateadd (Quarter,datediff (quarter,0, @date), 0), DateAdd (Quarter,1+datediff (quarter,0, @date), 0) )
--Using variable simplification
Select @date = DATEADD (Quarter,datediff (quarter,0, @date), 0)--first day of the quarter
Select DateDiff (Day, @date, DateAdd (quarter,1, @date))
Go