sql server - SQL - Convert results into comma separated string -


using article on stackoverflow (how split date ranges based on months in sql server 2005) have run select statement splits date range month , returns 2 columns (datefrom, dateto) in sql.

declare @sdate date = '2012/08/01' declare @edate date = '2013/09/01'  select      dateadd(month, n.number, @sdate) datefrom,      dateadd(day, -1, dateadd(month, n.number, dateadd(year,-1,@edate))) dateto      master.dbo.spt_values n      n.number < datediff(month, @sdate, @edate)     , n.type = 'p' 

i need add each of returned rows string separating columns | (dateto|datefrom) , each row ,.

for example if run code above result (just example im using first 4 rows need of them in 1 string):

r | date  |  date 1 | 2012-08-01 | 2012-08-31 2 | 2012-09-01 | 2012-09-30 3 | 2012-10-01 | 2012-10-30 4 | 2012-11-01 | 2012-11-30 

code:

declare @stralldates varchar(max) /* set @stralldates = insertcode */ print @stralldates 

what need print return:

2012-08-01|2012-08-31,2012-10-01|2012-10-30,2012-10-01|2012-10-30,2012-11-01|2012-11-30 

i have tried several suggestions other similar questions on stackoverflow (such concat) no success.

any or suggestions appreciated.

set @stralldates =    stuff((select ','+convert(char(10), sdate, 121)+'|'+convert(char(10), dateadd(day, -1, dateadd(month, 1, sdate)), 121)          (               select dateadd(month, n.number, @sdate) sdate               master..spt_values n               n.number < datediff(month, @sdate, @edate) ,                     n.type = 'p'               ) t          order sdate          xml path('')), 1, 1, '') 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -