Working with CSVs in SQL


String_Split

SQL Server 2016 (13.x) and later

	DECLARE @employeeIDs VARCHAR(100) = '32154, 32166, 32124' 	 	DROP TABLE IF EXISTS #employeeIDs 	SELECT CAST(e.value AS INT) AS employeeId 	INTO #employeeIDs 	FROM STRING_SPLIT(@employeeIDs, ',') e  	SELECT * 	FROM #employeeIDs

The XML PATH Method

DROP TABLE IF EXISTS #cityList
CREATE TABLE #cityList (city VARCHAR(20))
INSERT INTO #cityList (city)
VALUES ( 'Seattle'), ('Austin'), ('Phoenix'), ('Miami')


SELECT LEFT(cities.theCity,LEN(cities.theCity)-1) AS 'cities'		-- this line simply removes the last semicolon at the end 
FROM 
(	SELECT DISTINCT
		(
			SELECT city + ';' AS [text()]
			FROM #cityList av1
			FOR XML PATH ('')
		) AS theCity
	FROM #cityList av2
) AS cities

When you do not include DISTINCT: