Learning Horizon | For Learners

ASP.NET, SQL SERVER, JQUERY,JAVASCRIPT, WEBSPHERE

Tuesday 16 April 2013

Concatenate Multiple Rows Value Into Single String Text | Sql Server

In this tutorial, we will learn how to concatenate the multiple rows for a single column value into a single string text SQL server.  I am using the Northwind database table “Customers” and the column name is “ContactName”. And here is the query


    SELECT CONTACTNAME  FROM CUSTOMERS

    DECLARE @STR NVARCHAR(MAX)
    SELECT @STR = COALESCE(@STR + ',','') + C.CONTACTNAME FROM CUSTOMERS C
    PRINT @STR
    SELECT @STR AS [STR OUTPUT]

Result:-
          
               

COALESCE() A built-in function of SQL server is used in the query which returns a first non-null expression.

To learn more about COALESCE() function click here.

No comments:

Post a Comment

Please do not enter spam links.