Friday, August 26, 2011
How to Know the Number of Days in a Month in T-SQL
Sometimes we may need to know the number of days in a given month. For example, in calculating the number of days in a month or last day of the month or any other scenario.
Here is a small script that will give you the number of days for a month. For example, I'll pick month from the current date.
declare @numberOfDaysInMonth int;
set @numberOfDaysInMonth = DAY(DATEADD (m, 1, DATEADD (d, 1 - DAY(getdate()), getdate())) - 1);
How Do You Know the Name of the Month in the Monthly Index of T-SQL?
When working with reports, is that every time I'm looking for. Everyone gives the report date as input, and wants to show the month name in the report instead of the monthly index [1-12]. So how?
DateName(month, convert( datetime, '2007-' + cast(month(getdate()) as varchar(2)) + '-01', 120))
Here month(getdate()) returns the current month index from getdate() function. Hope this helps...