Splitting string in SQL Server was always challenging, we need to create a complex user defined function, using multiple SQL Server built in functions in order to achieve it. I have written an article back in 2012 regarding this. In SQL Server 2016, splitting string became so easy, SQL Server introduced a function to split string namely STRING_SPLIT()
, it requires the compatibility level to be at least 130.
Let me create a sample to demonstrate STRING_SPLIT()
functionality. In given below sample, I have multiple email addresses separated by a semicolon delimiter against each employee as shown below.
Sample :
USE tempdb GO CREATE TABLE #Employee ( EmployeeID INT IDENTITY(1,1), EmployeeName VARCHAR(100), EmailAddresses VARCHAR(500) ); GO INSERT INTO #Employee(EmployeeName, EmailAddresses) VALUES ('John', 'John_1@gmail.com;John_2@gmail.com;John_3@hotmail.com'), ('Sarah', 'Sarah_1@gmail.com;Sarah_2@hotmail.com;Sarah_1@gmail.com'), ('Aaron', 'aaron_1@gmail@com;aaron_2@hotmail.com'), ('Ackerman', 'ackerman_1@gmail.com;ackerman_2@hotmail.com'); GO SELECT * FROM #Employee GO --OUTPUT

Now sample is ready, let’s apply STRING_SPLIT()
function to see how it works and how easy it is to implement it.
Example 1:
Given below is an example where I used sample created above and split email addresses based on delimiter using STRING_SPLIT()
function.
USE tempdb GO SELECT EmployeeID , EmployeeName , value AS EmailAddress FROM #Employee CROSS APPLY STRING_SPLIT(EmailAddresses,';') GO --OUTPUT

In above records, we have successfully split email addresses and now we have one email address per row. In the above records, though we have successfully split email addresses, still we have duplicate email (Sarah_1@gmail.com) in email address column.
Let’s fix duplicate issue in below example.
Example 2: Removal of duplicate data
In order to remove duplicate email address, we need to use GROUP BY function along with STRING_SPLIT()
function in the given below example.
USE tempdb GO SELECT EmployeeID , EmployeeName , value AS EmailAddress FROM #Employee CROSS APPLY STRING_SPLIT(EmailAddresses,';') GROUP BY EmployeeID , EmployeeName , value; GO --OUTPUT

As you can see above, duplication in email address column has been removed.
Conclusion:
I found STRING_SPLIT()
function very useful and we can easily split string without even writing a lot of scripts. You can also implement it wherever it is required without any complexity. Do let me know if you face any issues.
Hello Mr. Imran:
As always, pretty good write-up. The new String_Split function with the enable_ordinal parameter in SQL Server 2022 is very important addition and it is worth to be mentioned in your article with a sample. This enable_ordinal parameter provides a way to guarantee the sequence of the result with ordinal values in result set.
Hi Jingyang, thank you so much. This is a very good point. I have already written an article on it.
Here is the link. https://raresql.com/2022/12/09/sql-server-2022-tsql-enhancement-string_split-function/
Thanks Muhammad. I saw it later.
An added link to your other blog may help one time readers to get to every good things you are writing.
We miss your update on your site for sometime. I will go through your new postings with newest content.
Your All about object_id posting has very unique content so I remember your site since.
Happy holidays.
Jingyang