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

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

代码示例1
CREATE FUNCTION FUNC_Split](@text VARCHAR(8000), @delimiter VARCHAR(20) = ' ')
RETURNS @Strings TABLE
(   
  position INT IDENTITY PRIMARY KEY,
  value VARCHAR(8000)  
)
AS
BEGIN
DECLARE @index INT
SET @index = -1

WHILE (LEN(@text) > 0)
  BEGIN 
    SET @index = CHARINDEX(@delimiter , @text) 
    IF (@index = 0) AND (LEN(@text) > 0) 
      BEGIN  
        INSERT INTO @Strings VALUES (@text)
          BREAK 
      END 
    IF (@index > 1) 
      BEGIN  
        INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))  
        SET @text = RIGHT(@text, (LEN(@text) - @index)) 
      END 
    ELSE
      SET @text = RIGHT(@text, (LEN(@text) - @index))
    END
  RETURN
END