Author |
Topic |
alexjamesbrown
Starting Member
48 Posts |
Posted - 2014-03-23 : 18:58:04
|
I have a table that looks like this:ID | Categories1 | ,4,5,62 | ,24,62,643 | ,41,5,64I'm trying to write a query that would output this as:ID | Category1 | 41 | 51 | 62 | 242 | 622 | 643 | 413 | 53 | 64Is this possible? |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-03-24 : 03:03:05
|
[code];WITH aCTEAS (select 1 as ID,',4,5,6' as Categories Union all select 2 , ',24,62,64' union all select 3 , ',41,5,64'),cteXMLAS( select ID ,CAST('<Categories><categ>' + replace(right(Categories,len(Categories)-1),',','</categ><categ>')+'</categ></Categories>' as XML) as Categoriesfrom aCTE)select ID ,t.u.value('.','varchar(50)') as Category from cteXML cross apply Categories.nodes('Categories/categ') t(u)[/code]output[code]ID Category1 41 51 62 242 622 643 413 53 64[/code]SsabinWeb MCP |
|
|
alexjamesbrown
Starting Member
48 Posts |
Posted - 2014-03-26 : 20:18:17
|
thanks...how can i replace the hardcoded values with my table? |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-03-28 : 01:51:46
|
[code]select 1 as ID,',4,5,6' as Categories Union all select 2 , ',24,62,64' union all select 3 , ',41,5,64'[/code]will be replace by your select like this:[code]SELECT ID, CategoriesFROM yourTable[/code]sabinWeb MCP |
|
|
alexjamesbrown
Starting Member
48 Posts |
Posted - 2014-04-01 : 03:57:56
|
I getInvalid length parameter passed to the RIGHT function.I guess one of my "products" has no categories? |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-04-01 : 04:02:09
|
SELECT ID, CategoriesFROM yourTableWHERE Categories IS NOT NULLsabinWeb MCP |
|
|
alexjamesbrown
Starting Member
48 Posts |
Posted - 2014-04-01 : 16:49:55
|
Perfect, thanks |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-04-02 : 01:18:12
|
Welcome!sabinWeb MCP |
|
|
|
|
|