CodePudding user response:
declare @ dt date, @ first date, @ days int, @ SQL nvarchar (Max)
The set @ dt='2019-6-8'
Select @ first=dateadd (d - datepart (d, @ dt) + 1, @ dt), @ days=31 - datepart (d, dateadd (d, 31, @ first))
Select @ SQL='select * from (select dateadd (d, number, @ first) as dt, number + 1 as dayNumber from master.. Spt_values where type='p' and number<=@ days) a pivot (Max (dt) for dayNumber in ('
+ stuff ((
Select '[' + convert (varchar, number + 1) +'] 'from the master.. Spt_values where type='p' and number<=@ days for XML path (' ')
), 1, 1 ') + ')) p '
The exec sp_executesql @ SQL, N '@ first date, @ days int' @ first=@ first, @ days=@ days