How will you concatenate data in Sql Server without using a variable and any RBAR approach?

 Posted by Niladri.Biswas on 7/11/2012 | Category: Sql Server Interview questions | Views: 1995 | Points: 40
Answer:

Suppose, we have some data in a table as shown under

Data
----------
Hello,
How
Are
You

We need to write a SQL Query to bring the following output

ConcatenateData
-----------------------
Hello,How Are You

Solution


DECLARE @t TABLE(Data Varchar(20))

INSERT @t SELECT 'Hello,' UNION ALL SELECT 'How' UNION ALL SELECT 'Are' UNION ALL SELECT 'You'

SELECT
ConcatenateData
FROM
(
SELECT ' ' + CAST(Data AS varchar(8000))
FROM @t
FOR XML PATH ('')
) X(ConcatenateData)


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Jasminej on: 7/11/2012 | Points: 10
Are u joining Words here ?
I think you have concatenated/constructed the data.. correct ?
Posted by: Jasminej on: 7/12/2012 | Points: 10
Thanks for the Update!

Login to post response

More Interview Questions by Niladri.Biswas