ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Software  >>  Databases  >>  SQL Server
 
 


 

 
 Oracle interview questions  Oracle Interview Questions
 SQL Server interview questions  SQL Server Interview Questions
 MS Access interview questions  MS Access Interview Questions
 MySQL interview questions  MySQL Interview Questions
 Postgre interview questions  Postgre Interview Questions
 Sybase interview questions  Sybase Interview Questions
 DB Architecture interview questions  DB Architecture Interview Questions
 DB Administration interview questions  DB Administration Interview Questions
 DB Development interview questions  DB Development Interview Questions
 SQL PLSQL interview questions  SQL PLSQL Interview Questions
 Databases AllOther interview questions  Databases AllOther Interview Questions
Question
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
 Question Submitted By :: Kumar_kisna
I also faced this Question!!     Rank Answer Posted By  
 
  Re: 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
Answer
# 1
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
Kumar.t
 
  Re: 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
Answer
# 2
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 2 No
B
 
 
 
  Re: 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
Answer
# 3
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
Kumar.t
 
  Re: 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
Answer
# 4
select list(name) from table_name
 
Is This Answer Correct ?    0 Yes 4 No
Narendra Biyani
 
  Re: 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
Answer
# 5
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 ?    4 Yes 1 No
Utsav
 
  Re: 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
Answer
# 6
;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
Senthil Kumar Murugan
 
  Re: 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
Answer
# 7
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
Rajesh.a
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
How to find the date and time of last updated table? Teledata4
How do you troubleshoot SQL Server if its running very slow?  2
what is mean by crystal repoart? ahere we will mainly use that?  1
Please give me the SP for the below scenario. I have two tables named Table1 and Table2...I need to fetch record by record from Table1 and insert the record in to table2 where the value in the sno column of the table1 is even number. Value-Labs4
How the data stores in a page?  1
What are the differences between stored procedure and functions in SQL Server 2000? TCS12
What's the difference between a primary key and a unique key? Wipro8
explain different types of jions with examples briefly? Zensar3
what is IDE,DMV in sql server? Value-Labs1
Explain DBMS, RDBMS?  4
how to connect sybase to sql server 2005?. ABC1
What is blocking and how would you troubleshoot it? HCL2
Write a query to delete duplicate records in SQL SERVER Infosys19
What is the purpose of UPDATE STATISTICS?  1
Difference between sql server 2000 and sql server 2005? Satyam4
What is database normalization? Digicel5
What are the new features in SQL Server 2005? Emphasis2
write query for fourth maximum salary from employee table Mind-Tree5
what is difference between having and where clause ?  2
Can a stored procedure call itself(recursive). If yes what level and can it be controlled.?  2
 
For more SQL Server Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com