createFUNCTION [dbo].[Fun_SplitStr]
(
@originalStr VARCHAR(8000), –要分割的字符串
@split varchar(100) –分隔符号
)
RETURNS @temp TABLE(Result VARCHAR(100))
AS
BEGIN
DECLARE @result AS VARCHAR(100); –定义变量用于接收单个结果
(
@originalStr VARCHAR(8000), –要分割的字符串
@split varchar(100) –分隔符号
)
RETURNS @temp TABLE(Result VARCHAR(100))
AS
BEGIN
DECLARE @result AS VARCHAR(100); –定义变量用于接收单个结果
SET @originalStr = @originalStr + @split ;
WHILE (@originalStr <> ”)
BEGIN
SET @result = LEFT(@originalStr, CHARINDEX(@split, @originalStr, 1) -1) ;
INSERT @temp VALUES(@result) ;
–STUFF()函数用于删除指定长度的字符,并可以在指定的起点处插入另一组字符。
SET @originalStr = STUFF(@originalStr, 1, CHARINDEX(@split, @originalStr, 1), ”);
END
RETURN
END
案例:select * from fun_splitStr(@selectitem,’/’)