there is a table having two columns no and name
and the data is
1 A
2 B
3 C

write a query that will result a horizontal output
A,B,C

Answers were Sorted based on User's Feedback



there is a table having two columns no and name and the data is 1 A 2 B 3 C write a query t..

Answer / utsav

declare @tst varchar(max)
set @tst=''
select @tst=@tst+col+',' from table
select @tst

there is a twist, if col contains any null value then this
query will return null. use isnull(col,'') in that case

Is This Answer Correct ?    5 Yes 1 No

there is a table having two columns no and name and the data is 1 A 2 B 3 C write a query t..

Answer / kumar.t

1. First To create the following table.

Create table NewNew(sno int identity(1,1) primary key,
names varchar(100))

2. To INsert the record one by one

Insert Into NewNew Values ('A')
Insert Into NewNew Values ('B')
Insert Into NewNew Values ('C')
Insert Into NewNew Values ('D')
Insert Into NewNew Values ('F')

3. To view the records first

Select * From NEwNew

4. To create the following procedure

Create Proc New1
As
Begin
Declare @names varchar(100)
Declare @sQuery Varchar(7000)
Declare @sQuery1 Varchar(1000)
Declare @ans varchar(8000)
Declare @i int
Declare cur cursor for select names from newnew
Open cur
Set @i = 1
Set @sQuery1 = ''
Set @sQuery = ''
fetch next from cur into @names
While @@fetch_status=0
Begin
Set @sQuery1 = '(Select Top 1 names
from newnew where names in (select top '+ convert(varchar
(3),@i) +' names From newnew order by 1 Asc) order by 1
Desc) As names,'
Set @sQuery = @squery + @squery1
Set @i = @i + 1
Fetch next from cur into @names
End
Set @ans = Convert(varchar(6),'Select ') +
Left(@sQuery,Len(@sQuery)-1)
Exec (@ans)
Close cur
deallocate cur
End

5. To execute the procedure
now to view the output Ok

Exec New1


By
Kumar.T

Is This Answer Correct ?    2 Yes 1 No

there is a table having two columns no and name and the data is 1 A 2 B 3 C write a query t..

Answer / miller

try using Pivot. for details follow the link
http://msdn.microsoft.com/en-us/library/ms177410.aspx link

Is This Answer Correct ?    1 Yes 0 No

there is a table having two columns no and name and the data is 1 A 2 B 3 C write a query t..

Answer / rajesh.a

declare @res varchar(max)

set @res=''

select @res=@res+name+',' from table

select @res=substring(@res,1,len-1)

print @res

Is This Answer Correct ?    1 Yes 1 No

there is a table having two columns no and name and the data is 1 A 2 B 3 C write a query t..

Answer / piyush sachan

DECLARE @List VARCHAR(8000)
SELECT @List = ISNULL(@List + ',', '') +names
FROM NewNew

SELECT @List

Orrrrrr

DECLARE @List VARCHAR(8000)
SELECT @List = COALESCE(@List + ',', '') +names
FROM NewNew

SELECT @List

Is This Answer Correct ?    0 Yes 0 No

there is a table having two columns no and name and the data is 1 A 2 B 3 C write a query t..

Answer / senthil kumar murugan

;WITH ABC (no, name) AS
(
SELECT 1, CAST('' AS VARCHAR(8000))
UNION ALL
SELECT B.no + 1, B.name + A.name + ', '
FROM (
SELECT Row_Number() OVER (ORDER BY no) AS no, name FROM
emp1) A
INNER JOIN ABC B ON A.no = B.no
)
SELECT TOP 1 name FROM ABC ORDER BY no DESC

Is This Answer Correct ?    1 Yes 2 No

there is a table having two columns no and name and the data is 1 A 2 B 3 C write a query t..

Answer / kumar.t

Hello Mr.B

In my table contain 100 record is there. at present what
you are doing?

Is This Answer Correct ?    0 Yes 2 No

there is a table having two columns no and name and the data is 1 A 2 B 3 C write a query t..

Answer / b

Select case when 1 then 'A'
when 2 then 'B'
when 3 then 'C' end as Name from tablename

Is This Answer Correct ?    0 Yes 3 No

there is a table having two columns no and name and the data is 1 A 2 B 3 C write a query t..

Answer / narendra biyani

select list(name) from table_name

Is This Answer Correct ?    0 Yes 5 No

Post New Answer

More SQL Server Interview Questions

How many types of triggers in sql server?

0 Answers  


logshipping is Any difference 2000 and 2005?

2 Answers  


What is the use of set nocount on/off statement?

0 Answers  


What is an active database?

0 Answers   HCL,


What is the difference between inner join and equi join?

0 Answers  






What is the sql case statement used for? Explain with an example?

0 Answers  


What is sub query and its properties?

0 Answers  


can any one say how to update the following senario I have a table <srabank> in which the table structure is as follows ANAME ACCNO LOCATION ACCTYPE BAL SBanuPrakash 31518746291 Punganur deposit 4000 Sreenivas 31518746292 mahoobnagar current 14000 Ranjith 31518746293 Karimnagar Savings 2000 Giresh 31518746294 Chennai deposit 40000 Boo 31518746295 Chennai Savings 20000 Jay 31518746296 Valachari Savings 1000 tirumalraj 31518746297 Vellore Savings 8000 The senario is We need to select one account number and check the balance after checking the balance if the balance exist we need to transfer to another account . in the from account the amount need to be reduced and in the to account the amount needed to be added. for example for the <accountno> <31518746291> the balance is <4000> for the <accno> <31518746292> the balance is <14000> after transferring the balance the details will look as follows <accno><31518746291> <bal> <2000> <accno><31518746292> <bal> <16000> the above mentioned two statment will come under the final result.

3 Answers  


What is public role in sql server?

0 Answers  


What are the different types of backups avaialabe in sql server 2005?

0 Answers  


What's the difference between DELETE TABLE and TRUNCATE TABLE commands?

9 Answers  


What command do we use to rename a db, a table and a column?

0 Answers  


Categories