Home > database >  A stored procedure in the incoming (such as the 2019-06), traversing a every day of the month, tied
A stored procedure in the incoming (such as the 2019-06), traversing a every day of the month, tied

Time:10-31

A stored procedure in the incoming (such as the 2019-06), traversing a every day of the month, tied for a career change

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
  • Related