Author |
Topic |
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-09 : 01:29:54
|
Hi I want to be able to concatenate some fields together. I think I can do this with a CASE statement but not quite sure how to add the comma and space between each worde.g. from the table below I want to create a result that goes like thiscol1 Col2T401, Bread, Cheese, TomatoT402, HamT403, Sausages, bread, CheeseT404, Beer, NutsT405, Bread, olive OilT406, Steak, Cheese, fruit, CakeT407, TomatoT408, ice cream, pasta, oilcreate table T ( recid varchar(10) null, Food1 varchar(10) null , Food2 varchar(10) null , Food3 varchar(10) null , Food4 varchar(10) null );insert into T values ('T401', 'Bread', 'Cheese', 'Tomato', null);insert into T values ('T402', null, 'Ham', 'null', null);insert into T values ('T403', 'Sausages', null, 'Bread', 'Cheese');insert into T values ('T404', 'Beer', 'Nuts', null, null);insert into T values ('T405', 'Bread', null, 'olive oil', null);insert into T values ('T406', 'Steak', 'Cheese', 'Fruit', 'Cake');insert into T values ('T407', null, null, 'Tomato', null);insert into T values ('T408', 'ice cream', null, 'pasta', 'oil');I tried the following query select recid, case when isnull(food1,'')>'' then food1 +', ' else '' end +case when isnull(food2,'')>'' then food2 +', ' else '' end +case when isnull(food3,'')>'' then food3 +', ' else '' end +case when isnull(food4,'')>'' then food4 else '' endfrom t But the problem is that if column4 is not he last column the results end with a comma in the wrong placeT401 Bread, Cheese, Tomato, T402 Ham, null, T403 Sausages, Bread, CheeseT404 Beer, Nuts, T405 Bread, olive oil, T406 Steak, Cheese, Fruit, CakeT407 Tomato, T408 ice cream, pasta, oil |
|
Sachin.Nand
2937 Posts |
Posted - 2010-08-09 : 02:59:42
|
select recid, reverse(stuff(reverse(case when isnull(food1,'')>'' then food1 +',' else '' end +case when isnull(food2,'')>'' then food2 +',' else '' end +case when isnull(food3,'')>'' then food3 +',' else '' end +case when isnull(food4,'')>'' then food4 +',' else '' end),1,1,''))from tLimitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
PavanKK
Starting Member
32 Posts |
Posted - 2010-08-09 : 03:09:19
|
small modification(reverse is not required)select recid, STUFF(case when isnull(food1,'')>'' then ', '+food1 else '' end +case when isnull(food2,'')>'' then ', '+food2 else '' end +case when isnull(food3,'')>'' then ', '+food3 else '' end +case when isnull(food4,'')>'' then ', '+food4 else '' end,1,1,'')from T KK :) |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-08-09 : 03:17:15
|
quote: Originally posted by PavanKK small modification(reverse is not required)select recid, STUFF(case when isnull(food1,'')>'' then ', '+food1 else '' end +case when isnull(food2,'')>'' then ', '+food2 else '' end +case when isnull(food3,'')>'' then ', '+food3 else '' end +case when isnull(food4,'')>'' then ', '+food4 else '' end,1,1,'')from T KK :)
Thanks don't know what I was thinking Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-09 : 16:30:49
|
Thanks guys |
 |
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-08-09 : 17:13:07
|
No need CASE.select recid,stuff(coalesce(', ' + food1, '') +coalesce(', ' + food2, '') +coalesce(', ' + food3, '') +coalesce(', ' + food4, ''), 1, 2, '') as listfrom t ______________________ |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-13 : 16:11:57
|
Can I ask a question so that I can understand.I have tested the query below and it works.select recid, STUFF(case when isnull(food1,'')>'' then ', '+food1 else '' end +case when isnull(food2,'')>'' then ', '+food2 else '' end +case when isnull(food3,'')>'' then ', '+food3 else '' end +case when isnull(food4,'')>'' then ', '+food4 else '' end,1,1,'')from Thowever if i look at the query it looks like it says.. if food1 is not empty then ", " + food1It doesn't seem to make sense can someone explain for methanks |
 |
|
|
|
|