Just Code‎ > ‎

TSQL - Concat values from a column into a single string

posted Feb 14, 2010, 3:51 AM by Peter Henell

CREATE Function GetOvrigaBranscherAsString (@anlaggningID INT)
RETURNS varchar(1000)
AS
BEGIN
DECLARE @OvrigaBranscher varchar(1000)
SET @OvrigaBranscher = ''
Select @OvrigaBranscher = @OvrigaBranscher + RTRIM(ap.branschkod) + ', '
    FROM smp.anlaggprovning ap
    WHERE ap.anlaggningID = @anlaggningID AND huvudbransch = 'Nej'

IF @OvrigaBranscher <> '' SET @OvrigaBranscher = LEFT(@OvrigaBranscher, LEN(@OvrigaBranscher) - 1)

RETURN (@OvrigaBranscher)
END
GO
Comments