Hi nauna,
Refer below sample query.
SQL
DROP FUNCTION GetCommaSeparatedString
GO
CREATE FUNCTION GetCommaSeparatedString(@City NVARCHAR(15))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Output VARCHAR(MAX)
SELECT @Output = COALESCE(@Output + ',', '') + CAST(EmployeeId AS VARCHAR(5))
FROM Employees
WHERE City = @City
RETURN @Output
END
GO
SELECT dbo.GetCommaSeparatedString('London') AS Ids