Coalesce in sql server to concatenate many rows into a single string

Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.

DECLARE @str NVARCHAR(256)

       SELECT @str = COALESCE(@str + ', ', '') + cast(CatID AS NVARCHAR)
       FROM TableName
       WHERE ID = 1

       SELECT @str

END


Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form