How to simulate string_agg function ?
I need get this
[value]
1
2
3
into this
1,2,3
I tried following
CREATE TYPE stringArray AS TABLE ([value] nvarchar(255))
GO
CREATE FUNCTION dbo.ufn_join
(
@table stringArray readonly,
@separator nvarchar(5) = ','
)
RETURNS nvarchar(max)
AS
BEGIN
RETURN stuff((select @separator + value from @table for xml path('')), 1, 1, '')
END
GO
SELECT dbo.ufn_join(
(
SELECT cast(1 as nvarchar(255)) as value
UNION
SELECT cast(2 as nvarchar(255)) as value
UNION
SELECT cast(3 as nvarchar(255)) as value
)
, DEFAULT
)
but I am getting an error
-- Error: Operand type clash: nvarchar is incompatible with stringArray
Only condition is that i do not want to use any kind of variables. CLR function is also totally fine, but there i have the same issue, how to insert return of select as a parameter to the function.