AlanLSmith (6/11/2013)
Suggest that master dates table is best.But if it's not available, here's a query that will give you the dates for a given year and month.
Still working on how to get the cross-tab/pivot/matrix working to have these in columns, but this is a start:
with daylist as
(
select 1 as daynum
union all
select dl.daynum + 1 as daynum
from daylist dl
where dl.daynum + 1 <=
day( DATEADD( s, -1, DATEADD( mm, DATEDIFF( m, 0,
CAST(@yearnum + '-' + @monthnum + '-01' AS datetime))+1,0)))
)
select daynum from daylist order by daynum;
Alan,
Please see the article at the following link. You might change your mind about using rCTE's (even small ones) that count in the future.
http://www.sqlservercentral.com/articles/T-SQL/74118/
--Jeff Moden
Change is inevitable... Change for the better is not.