将字符串进行分隔
createFUNCTION [dbo].[StringSplit]
(
@STR NVARCHAR(MAX)='',
@SPLITER NVARCHAR(20)='')
RETURNS @TB TABLE(TB_V NVARCHAR(200))
AS
BEGIN
DECLARE @NUM INT, @POS INT,@NEXT_POS INT
SET @NUM=0
SET @POS=1
WHILE (@POS <=LEN(@STR))
BEGIN
SELECT @NEXT_POS=CHARINDEX(@SPLITER,@STR,@POS)
IF (@NEXT_POS=0 OR @NEXT_POS IS NULL)
BEGIN
SELECT @NEXT_POS=LEN(@STR)+1
INSERT INTO @TB VALUES(RTRIM(LTRIM(SUBSTRING(@STR,@POS,@NEXT_POS-@POS))))
SELECT @POS=@NEXT_POS +1
END
END
RETURN
END
调用:select * from dbo.StringSplit('123,123,22,3,3,2,1',',')