--table to StringStuff((Select Top - ','+ cast(QQ as varchar( -)) fromDl_qqOrder byQq forXML Path ("')),1,1,"')
--String to tableCREATE FUNCTIONSplit (@Text NVARCHAR(4000),@Sign NVARCHAR(4000)) RETURNS @tempTable TABLE(IDINT IDENTITY(1,1)PRIMARY KEY,[VALUE] NVARCHAR(4000)) as BEGIN DECLARE @StartIndex INT --where to start looking DECLARE @FindIndex INT --location found DECLARE @Content VARCHAR(4000)--the value found --initialize a number of variables SET @StartIndex = 1 --The find position of a string in T-SQL starts at 1 SET @FindIndex=0 --start loop lookup string comma while(@StartIndex <= LEN(@Text)) BEGIN --Find String function CHARINDEX The first argument is the string to find --The second parameter is where to find the string --The third parameter is where you start looking --The return value is where the string was found SELECT @FindIndex = CHARINDEX(@Sign,@Text,@StartIndex) --Judging if there's no found. return 0 IF(@FindIndex =0 OR @FindIndex is NULL) BEGIN --if we don't find them , we're done. SET @FindIndex = LEN(@Text)+1 END --Intercept string function SUBSTRING The first argument is the string to intercept --The second parameter is the starting position --The third parameter is the length of the Intercept --@[email protected] indicates the position to find-the position to start with = length to intercept --LTRIM and RTRIM are whitespace functions that remove the left and right side of a string SET @Content = LTRIM(RTRIM(SUBSTRING(@Text,@StartIndex,@FindIndex-@StartIndex))) --Initialize the location of the next lookup SET @StartIndex = @FindIndex+1 --Insert the value you are looking for in the table type that you want to return INSERT into @tempTable([VALUE])VALUES(@Content) END RETURN END SELECT * fromDbo. Split ('a,b,c,d,e,f,g',',')
SQL table and string Mutual transfer