Author |
Topic |
Lesombrero
Starting Member
43 Posts |
Posted - 2013-04-14 : 04:38:23
|
Hello everybody,I have this problem:Table Catid | name----- | -------1 | AAAAA2 | BBBBB3 | CCCCC4 | DDDDD5 | EEEEE6 | FFFFFTable Prodid_prod | id_cat------- | -------1 | 1,52 | 3,2,63 | 2,54 | 4,65 | 2,4,66 | 2,4I expect the result as follow:Category--------AAAAA, EEEEECCCCC, BBBBB, FFFFFBBBBB, EEEEE....I tried with stuff and xml path, but can't get what I want!Thanks in advance for your help. |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-04-14 : 13:26:25
|
quote: Originally posted by Lesombrero Hello everybody,I have this problem:Table Catid | name----- | -------1 | AAAAA2 | BBBBB3 | CCCCC4 | DDDDD5 | EEEEE6 | FFFFFTable Prodid_prod | id_cat------- | -------1 | 1,52 | 3,2,63 | 2,54 | 4,65 | 2,4,66 | 2,4I expect the result as follow:Category--------AAAAA, EEEEECCCCC, BBBBB, FFFFFBBBBB, EEEEE....I tried with stuff and xml path, but can't get what I want!Thanks in advance for your help.
select stuff((select ',' + name from cat c where ','+p.id_cat+',' like '%,'+cast(c.id as varchar(32)) + '%,' for xml path('')),1,1,'') as Categoryfrom prod p |
 |
|
Lesombrero
Starting Member
43 Posts |
Posted - 2013-04-15 : 02:57:14
|
Thank you so much James. This is perfect! |
 |
|
Lesombrero
Starting Member
43 Posts |
Posted - 2013-04-15 : 03:33:54
|
Ouups! Sorry!I just noticed something. 11 is treated as 1,11 !!!Any suggestion? |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-04-15 : 03:41:03
|
small typo is there...stuff((select ',' + name from @Cat c where ','+p.id_cat+',' like '%,'+cast(c.id as varchar(32)) + ',%' --Chandu |
 |
|
Lesombrero
Starting Member
43 Posts |
Posted - 2013-04-15 : 03:48:21
|
Of Course! Thanks Chandu. |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-04-15 : 04:36:16
|
quote: Originally posted by Lesombrero Of Course! Thanks Chandu.
Welcome --Chandu |
 |
|
|
|
|