Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Please Help me!

Author  Topic 

soori457
Yak Posting Veteran

85 Posts

Posted - 2008-03-07 : 06:41:47
Hai All

Names(table name)
value(col name)
A
B
C
D

with sql statement, I have to display data in horizontal. i.e.,

A B C D.
Is it possible with sql statement.Any one tell me
Thanks in Advance


Suresh 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
Go to Top of Page

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 reports

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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
Go to Top of Page

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
Go to Top of Page

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.
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-03-08 : 01:47:05

select distinct stuff((select ' ' + ColName from Table1
FOR XML PATH('')),1, 1,'') ColName from Table1

now we can get the correct output
Go to Top of Page

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_CURSOR

PRINT @FNAME
Go to Top of Page

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.

Go to Top of Page

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_CURSOR

PRINT @FNAME


declare @sql varchar(8000)
select @sql=isnull(@sql+' ','')+col from table
print @sql

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -