sql - How to include string text in a STUFF function (XML Path)? -
code:
isnull(ltrim(stuff( (select ', ' + pl.pipe_product reporting.pipelineinformation pl pl.project_id = ti.project_id xml path('')), 1, 1, '')),'___________') pipe_product
how can this?
another approach can add row_number sub-query decide whether comma or , should inserted. should faster since don't have separate queries against same pipelineinformation
table both of correlated sub-queries.
declare @pl table (pipe_product varchar(50), project_id int) insert @pl values ('gas', 1),('oil', 1),('orange juice', 1), ('milk', 2), ('honey', 2) select ti.project_id, substring(( select case when rownum = 1 ' , ' else ', ' end + t.pipe_product [text()] ( select pipe_product, project_id, row_number() on (order pipe_product desc) rownum @pl project_id = ti.project_id ) t order rownum desc xml path('') ), 3, 4000) [pipe_product] (select 1 union select 2) ti (project_id)
the sample data above outputs:
project_id pipe_product ----------- ----------------------------- 1 gas, oil , orange juice 2 honey , milk
Comments
Post a Comment