SQL string into a temporary table

CREATE FUNCTION Split (@Text NVARCHAR (4000), @ Sign NVARCHAR (4000))
RETURNS @tempTable TABLE (id INT IDENTITY (1,1) PRIMARY KEY, [VALUE] NVARCHAR (4000))
AS
BEGIN
DECLARE @StartIndex INT- The location to start the search
DECLARE @FindIndex INT-The location found
DECLARE @Content VARCHAR (4000)-The value found--
Initialize some variables
SET @StartIndex = 1-The search position of the string in T-SQL starts from 1 of
the SET = 0 the FindIndex @

- cycle begins the search string comma
the WHILE (@StartIndex <= LEN (@Text))
the bEGIN
- Find a string functions CHARINDEX first parameter is a string looking for
- second parameter Where to find this string
-the third parameter is where to start the search
-the return value is where the string is found
SELECT @FindIndex = CHARINDEX (@ Sign, @ Text, @ StartIndex)-judge
whether it was found or not Find and return 0
IF (@FindIndex = 0 OR @FindIndex IS NULL)
The BEGIN
- if not found expressed by looking over
the SET @FindIndex = LEN (@Text) + 1'd
the END
- interception string function SUBSTRING first parameter is the string to be taken
- a second parameter is the start position
- -The third parameter is the length of the interception-
@ FindIndex- @ StartIndex indicates the location to find-the location to start searching = the length to be intercepted--
LTRIM and RTRIM are the functions to remove the spaces on the left and right of the string
SET @Content = LTRIM (RTRIM (SUBSTRING (@ Text, @ StartIndex, @ FindIndex- @ StartIndex)))
-Initialize the position of the next search
SET @StartIndex = @ FindIndex + 1
-Insert the value found into the Table type to be returned Medium
INSERT INTO @tempTable ([VALUE]) VALUES (@Content)
END
RETURN
END

SELECT * FROM dbo.Split(replace('''JB0051654'',''JB0331768'',''JB0011698''','''',''),',')

Guess you like

Origin www.cnblogs.com/linjincheng/p/12671941.html