I'm trying to use QUOTENAME to enclose each part of the string returned in brackets but cannot seem to find the solution to enclose the first instance of the string?
CREATE TABLE dbo.Products
(
ProductID INT PRIMARY KEY,
Name NVARCHAR(255) NOT NULL UNIQUE
/* other columns */
);
INSERT dbo.Products VALUES
(1, N'foo'),
(2, N'bar'),
(3, N'kin'),
(4, N'blat');
DECLARE @Split varchar (1000)
SET @Split = ''
SELECT @Split =
CASE WHEN @Split = ''
THEN Name
ELSE @Split + coalesce(',' + QUOTENAME(Name),'')
END
FROM dbo.Products
PRINT @Split
Result :
bar,[blat],[foo],[kin]
I need 'bar' to also be enclosed with [].