| Author |
Topic |
|
soori457
Yak Posting Veteran
85 Posts |
Posted - 2008-03-07 : 06:41:47
|
| Hai AllNames(table name)value(col name)ABCDwith sql statement, I have to display data in horizontal. i.e.,A B C D.Is it possible with sql statement.Any one tell meThanks in AdvanceSuresh Kumar |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-03-07 : 07:45:06
|
| Yes,Look up CASE statements in Books On Line.Jim |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-03-07 : 07:59:12
|
| Where do you want to show the data?Read about Cross tab reportsMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-07 : 09:41:55
|
| Also have a look at PIVOT feature if you're using SQL 2005 |
 |
|
|
zubamark
Starting Member
23 Posts |
Posted - 2008-03-07 : 12:25:19
|
| Try this: select distinct stuff((select ' ' + ColName from Table1 FOR XML PATH('')),1, 2,'')ColName from Table1 |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-03-08 : 00:36:50
|
quote: Originally posted by zubamark Try this: select distinct stuff((select ' ' + ColName from Table1 FOR XML PATH('')),1, 2,'')ColName from Table1
No,first letter of the first value is removed in the output with the above query. |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-03-08 : 01:47:05
|
| select distinct stuff((select ' ' + ColName from Table1FOR XML PATH('')),1, 1,'') ColName from Table1now we can get the correct output |
 |
|
|
sign_seventh
Yak Posting Veteran
66 Posts |
Posted - 2008-03-09 : 20:59:29
|
| This one is kinda long, but can be an option.DECLARE @Name AS VARCHAR(50)DECLARE @FName AS VARCHAR(1024)SET @FNAME = '' declare NAME_CURSOR CURSOR FOR SELECT ColName FROM Table1 open NAME_CURSOR fetch next from NAME_CURSOR into @Name while @@fetch_status = 0 begin SET @FNAME = @FNAME+' '+@Name fetch next from NAME_CURSOR into @Name end close NAME_CURSOR deallocate NAME_CURSORPRINT @FNAME |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-09 : 21:02:34
|
quote: Originally posted by sign_seventh This one is kinda long, but can be an option.
A cursor is an option for what can be done with a single select or PIVOT? Sorry...in most cases, cursors should be avoided if possible. In the event the user needed to select the results of that as output...it would then also have to be dynamic SQL...something else to be cautious about. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-03-13 : 06:16:25
|
quote: Originally posted by sign_seventh This one is kinda long, but can be an option.DECLARE @Name AS VARCHAR(50)DECLARE @FName AS VARCHAR(1024)SET @FNAME = '' declare NAME_CURSOR CURSOR FOR SELECT ColName FROM Table1 open NAME_CURSOR fetch next from NAME_CURSOR into @Name while @@fetch_status = 0 begin SET @FNAME = @FNAME+' '+@Name fetch next from NAME_CURSOR into @Name end close NAME_CURSOR deallocate NAME_CURSORPRINT @FNAME
declare @sql varchar(8000)select @sql=isnull(@sql+' ','')+col from tableprint @sqlMadhivananFailing to plan is Planning to fail |
 |
|
|
|