question

davidben13 avatar image
davidben13 asked

Split & Pre-Pend Values To Comma Delimited String

Here’s what i’m trying to do! Working with a 26 comma separated value string. Trying to build our part # configuration from this. Database Field BOM_Item 02,03,01,02,01,,,01,,,,,,,,,,,,,,,,,,1 There are 26 sections of comma delimited values Each is assigned a letter A-Z, alphabetically So, it reads, A2,B3,C1,D2,E1,H1,Z1 Final Part # Should Read A2B3C1D2E1H1Z1 Just works from left to right, removing the extra zero (could have 2 character number) Can someone help me add a column to this table, that would take the comma separated string and designate the letters and put it with the number? I’m pretty savvy, but not a DBA - just hoping to add a column to my database view. Much thanks! -David
comma separated
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered
This approach uses a number of useful techniques. First we are going to use a tally table to split the comma separated string into each value - we treat these as integers so any leading '0' is stripped off. This will give us 26 values, and using row_number() function we can allocate a value 1-26 to each row. Add that value to 64 and you get the ASCII values A-Z. If you change the last query to `select * from CodedItems` you 'll see the values that you end up with. Finally use the 'xml concatenation' trick to bundle it all back together. declare @BOM_Item varchar(max) set @BOM_Item = '02,03,01,02,01,,,01,,,,,,,,,,,,,,,,,,1'; set @BOM_Item = ','+@BOM_Item+',' --first split the string in to proper data, i.e. a column ;WITH Nbrs_4( n ) AS ( SELECT 1 UNION SELECT 0 ), --2 Nbrs_3( n ) AS ( SELECT 1 FROM Nbrs_4 n1 CROSS JOIN Nbrs_4 n2 ), --2*2 = 4 Nbrs_2( n ) AS ( SELECT 1 FROM Nbrs_3 n1 CROSS JOIN Nbrs_3 n2 ), --4*4 = 16 Nbrs_1( n ) AS ( SELECT 1 FROM Nbrs_2 n1 CROSS JOIN Nbrs_2 n2 ), --16*16 = 256 Nbrs_0( n ) AS ( SELECT 1 FROM Nbrs_1 n1 CROSS JOIN Nbrs_1 n2 ), --256*256 = 65,536 Nbrs ( n ) AS ( SELECT 1 FROM Nbrs_0 n1 CROSS JOIN Nbrs_0 n2 ) --65536*65536 = 4,294,967,296 , tally_cte as ( select n FROM ( SELECT ROW_NUMBER() OVER (ORDER BY n) FROM Nbrs ) D ( n ) where n < 500 ) , Items as ( select row_number()over(order by N) as posn, cast(substring(@BOM_Item,N+1,charindex(',',@BOM_Item,N+1)-N-1) as int) as Value from tally_cte where N < len(@BOM_Item) and substring(@BOM_Item,N,1) = ',' ) , CodedItems as ( select char(64+posn) + cast(value as varchar) as LetterNumber from Items where value 0 ) -- select * from CodedItems select (SELECT '' + isnull(LetterNumber , ' ') FROM CodedItems for xml path(''), type ).value('.','varchar(max)') returns ------------------------------ A2B3C1D2E1H1Z1 (1 row(s) affected)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.