Site Search:
Sign in | Join | Help
4Penny.net

SQL Server (T-SQL)

Comments and notes on SQL Server 2000, 2005, and T-SQL

Semicolon separated contatoncated output in SQL Server

This script will give you a comma separated (or semicolon separated) list in SQL Server 2005

SELECT c.sopnumbe,
  STUFF(
    (SELECT ',' + CAST(itemnmbr AS VARCHAR(MAX)) AS [text()]
     FROM (SELECT DISTINCT rtrim(itemnmbr) as itemnmbr
           FROM sop10200 AS O
           WHERE O.sopnumbe = C.sopnumbe) AS D
     ORDER BY itemnmbr
     FOR XML PATH('')),
    1, 1, '') AS ArrEmps
FROM sop10100 AS C;

Ok, I admit it, that's kind of complicated. Here's a different way:

  declare @locncode as varchar(max)
 select @locncode = ''
 select @locncode = @locncode + ',' + locncode
  From _UnitUser
  where UserID = @UserID


 select stuff(@locncode,1,1,'') as locncode

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.