Return sql results as a single string

1.6k views Asked by At

I have the following query which returns more than 1 result (max is about 10rows not more!), how can I return all results as a single string?

SELECT DISTINCT(e.Username)
FROM TblLeaveEmployee l
JOIN TblLeaveApprovalsBy a on l.EmployeeId = a.UserID
JOIN TblEmployee e on l.EmployeeId = e.id
where l.EmployeeId IN 
  (select UserID 
   FROM TblLeaveApprovalsBy 
   WHERE ApprovalUserID IN
     (SELECT ApprovalUserID from TblLeaveApprovalsBy where UserID = 77))

Have tried using STUFF as follows but its not working:

DECLARE @CodeNameString varchar(100)
SELECT 
   @CodeNameString = STUFF(
SELECT DISTINCT(e.Username)
FROM TblLeaveEmployee l
JOIN TblLeaveApprovalsBy a on l.EmployeeId = a.UserID
JOIN TblEmployee e on l.EmployeeId = e.id
where l.EmployeeId IN 
  (select UserID 
   FROM TblLeaveApprovalsBy
   WHERE ApprovalUserID IN
     (SELECT ApprovalUserID from TblLeaveApprovalsBy where UserID = 77)))
4

There are 4 answers

3
Jayvee On BEST ANSWER
   DECLARE @CodeNameString VARCHAR(MAX)=NULL
   SELECT @CodeNameString = COALESCE(@CodeNameString+',' ,'') + Username 
   FROM
   (
   SELECT DISTINCT(e.Username) Username
   FROM TblLeaveEmployee l
   JOIN TblLeaveApprovalsBy a on l.EmployeeId = a.UserID
   JOIN TblEmployee e on l.EmployeeId = e.id
   where   l.EmployeeId IN (select UserID FROM TblLeaveApprovalsBy
              WHERE ApprovalUserID IN(SELECT ApprovalUserID from TblLeaveApprovalsBy    where UserID = 77))) x
   SELECT @CodeNameString
2
Jade On

This will be a problem if you have huge amount of row.

Try This

DECLARE t CURSOR FOR  
select Col1 from [Table] where crite1 = ???

open t

declare @v varchar(255)
declare @Res varchar(max)
 set @Res = ''
 set @v = ''
FETCH NEXT FROM t into @v
while (@@FETCH_STATUS = 0) begin
    set @Res = @Res + @v + '|'
    FETCH NEXT FROM t into @v
end
print @Res

close t
deallocate t
0
Jade On
DECLARE t CURSOR FOR  
SELECT DISTINCT(e.Username)
FROM TblLeaveEmployee l
JOIN TblLeaveApprovalsBy a on l.EmployeeId = a.UserID
JOIN TblEmployee e on l.EmployeeId = e.id
where l.EmployeeId IN 
(select UserID 
   FROM TblLeaveApprovalsBy 
   WHERE ApprovalUserID IN
     (SELECT ApprovalUserID from TblLeaveApprovalsBy where UserID = 77))

open t

declare @v varchar(255)
declare @Res varchar(max)
 set @Res = ''
 set @v = ''
FETCH NEXT FROM t into @v
while (@@FETCH_STATUS = 0) begin
    set @Res = @Res + @v + '|'
    FETCH NEXT FROM t into @v
end
print @Res

close t
deallocate t
0
Bùi Vũ Minh-Anh On

The STRING_AGG is new function is available from SQL Server 2017 onwards.

SELECT STRING_AGG( ISNULL(ColumnName, ' '), ',') As ColumnABC
       From TableName

The simplest and straight forward way to concatenate rows into a string value is to use FOR XML PATH in a select query. This FOR XML PATH method can be used in SQL Server version 2005 and higher.

SELECT SUBSTRING((
            SELECT ',' + DISTINCT (e.Username) AS 'data()'
            FROM TblLeaveEmployee l
            JOIN TblLeaveApprovalsBy a ON l.EmployeeId = a.UserID
            JOIN TblEmployee e ON l.EmployeeId = e.id
            WHERE l.EmployeeId IN (
                    SELECT UserID FROM TblLeaveApprovalsBy
                    WHERE ApprovalUserID IN (SELECT ApprovalUserID FROM TblLeaveApprovalsBy WHERE UserID = 77)
                    )
            ) FOR XML path(''))
    ,2
    ,999 ) AS CodeNameString