📜  sql 拆分字符串函数 - SQL 代码示例

📅  最后修改于: 2022-03-11 15:04:53.494000             🧑  作者: Mango

代码示例1
CREATE FUNCTION [dbo].[Func_SplitString] 
( @Values VARCHAR(MAX)
, @splitStr VARCHAR(50)
)

RETURNS @Result_Table TABLE
       (
         [value] nvarchar(MAX) NULL
       )
BEGIN
    DECLARE @TempStr nvarchar(MAX)
    WHILE (CHARINDEX(@splitStr,@Values)>0)
    BEGIN
        SET @TempStr=SUBSTRING(@Values,1,CHARINDEX(@splitStr,@Values)-1)
        INSERT INTO @Result_Table (value) VALUES (ltrim(rtrim(isnull(@TempStr, ''))))
        SET @Values = REPLACE(@Values,@TempStr+@splitStr,'')
    END/*End While*/
    
    IF(LEN(RTRIM(LTRIM(@Values)))>0 AND CHARINDEX(@splitStr,RTRIM(LTRIM(@Values)))=0) 
    BEGIN
        SET @TempStr=@Values 
        INSERT INTO @Result_Table (value) VALUES (ltrim(rtrim(isnull(@TempStr, ''))))
    End /*End IF*/
   RETURN 
END