SQL Server implements the split function to split strings and Its Usage example. sqlsplit
This article describes how to use SQL Server to split strings using the split function. We will share this with you for your reference. The details are as follows:
/* Function name: f_SplitToNvarchar function: function update record for split function: design idea: a string that combines nvarchar characters, separate them into a table with only one nvarchar type */create function [dbo]. [f_SplitToNvarchar] (@ SourceSql NVARCHAR (MAX), -- source separator string @ StrSeprate VARCHAR (10) -- separator) RETURNS @ temp TABLE (col NVARCHAR (MAX )) ASBEGINDECLARE @ I INTSET @ SourceSql = RTRIM (LTRIM (@ SourceSql) SET @ I = CHARINDEX (@ StrSeprate, @ SourceSql) WHILE @ I> = 1 begin insert @ temp VALUES (LEFT (@ SourceSql, @ I-1) SET @ SourceSql = SUBSTRING (@ SourceSql, @ I + 1, LEN (@ SourceSql)-@ I) SET @ I = CHARINDEX (@ StrSeprate, @ SourceSql) ENDIF @ SourceSql <> '\' INSERT @ temp VALUES (@ SourceSql) RETURNENDGO
Call example:
SELECT col FROM f_SplitToNvarchar ('1, 2, 3, 4 ',',');
:
I hope this article will help you design SQL Server database programs.