Here is another interesting question, I received the other day.
“How to list the name of the months between two date ranges?”
Very interesting question. I had no script ready for it so I asked my friend who used to my co-worker earlier and he has sent me the script which is listed below.
DECLARE @StartDate DATETIME,
SELECT @StartDate = '20140301' -- March
,@EndDate = '20140901'; -- September
SELECT DATENAME(MONTH, DATEADD(MONTH, nos.monthnos, @StartDate)-1) AS MonthName
FROM (SELECT 1 monthnos UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6
UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) nos
WHERE nos.monthnos <= DATEDIFF(MONTH, @StartDate, @EndDate)+1;
Above script does return the name of the months between date ranges. Let me know if there is any other way to achieve the same.
Please note: This blog post is modified based on the feedback of SQL Expert Sanjay Monpara. Thank you Sanjay!
Reference: Pinal Dave (http://blog.sqlauthority.com)