SQL string split

Posted by John Liu on Monday, November 15, 2021

There are different ways to split string in SQL query. One way I came across is using openjson function.

DECLARE @String AS VARCHAR(100) = '1,2,3,4'

SELECT value FROM OPENJSON('[' + @String + ']')

This method assumed the string using comma as the delimitor. If your string using other character as the delimitor, consider using REPLACE() function to change it to comma first.

Another simple method would be using STRING_SPLIT() function in SQL2016+

DECLARE @String AS VARCHAR(100) = '1,2,3,4'

SELECT value FROM string_split(@String,',')