sql server - Full invoice number + comma separated SQL list (TSQL) -
does perhaps know how comma separated list sql doesn't duplicate - it's little hard explain. let me give example.
i have list of invoices + shipment belongs in table below:
invoicenumber shipmentnumber 0180376000 1stshipment 0180376005 1stshipment 0180376003 1stshipment 0180375997 1stshipment 0180375993 1stshipment
this list needs divided main invoicenumbers followed right 2 digits of remaining invoice numbers. result should similar below.
01803760, 00, 05, 03, 01803759, 97, 93
at point can comma separated list cannot figure out how position 2 digit after each respective invoice belongs to.
any suggestion of how great!!
try this
declare @tbl table (invocenumber nvarchar(50)) insert @tbl values ('0180376000') insert @tbl values ('0180376005') insert @tbl values ('0180376003') insert @tbl values ('0180375997') insert @tbl values ('0180375993') select ( select a.invocenumber + ', ' ( select distinct left(invocenumber, len(invocenumber) - 2) invocenumber @tbl union select right(invocenumber, 2) invocenumber @tbl ) xml path ('') ) invoce
output:
01803759, 01803760, 00, 05, 03, 97, 93,
if order important.
select ( select result.invocenumber + ', ' + result.invo ( select a.invocenumber, ( select ia.invo + ', ' [text()] ( select distinct left(invocenumber, len(invocenumber) - 2) invocenumber, right(invocenumber, 2) invo @tbl ) ia ia.invocenumber = a.invocenumber xml path ('') ) invo ( select distinct left(invocenumber, len(invocenumber) - 2) invocenumber @tbl ) group a.invocenumber ) result xml path ('') ) s
output:
01803759, 93, 97, 01803760, 00, 03, 05,
Comments
Post a Comment