My Output is correct but I want to know how actually STUFF works.
I have the simple query which returns me total number of months between @startDate
& @endDate
.
I am storing that months into the the @cols by help of STUFF.
Query is like this :
SELECT DISTINCT ','
+ Quotename(CONVERT(CHAR(10), startdate, 120))
FROM #tempdates
- "," in the query should print before the values but it print like below O/P.
- If I remove XML Path from the stuff I am getting null value.
- How actually STUFFworks with XML path
Here's My output :
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@startdate datetime = '1-Jan-2014',
@enddate datetime = '1-Jun-2014'
;with cte (StartDate, EndDate) as
(
select min(@startdate) StartDate, max(@enddate) EndDate
union all
select dateadd(mm, 1, StartDate), EndDate
from cte
where StartDate < EndDate
)
select StartDate
into #tempDates
from cte
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(CHAR(10), StartDate, 120))
from #tempDates
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select @cols
drop table #tempDates