I'm trying to perform string aggregation on an Address in T-SQL so that any of the Address's fields which are not NULL are used in a comma-separated string. I know how I would do this in C# but am lost with SQL.
Here's what I have so far (I need to convert the C# part into SQL):
SELECT STRING_AGG
(
--Start of C# (I don't know how to convert this into SQL).
new[]
{
Addresses.Line1,
Addresses.Line2,
Addresses.Line3,
Addresses.City,
Addresses.County,
Addresses.State,
Countries.Name,
Addresses.Postcode
}
.Where(data => data != null)
--End of C#
,
', '
) Address
FROM Addresses
JOIN Countries ON Countries.Id = Addresses.CountryId