比较实用的函数
================================================
--调用:SELECT ID FROM [Fn_StringSplitToTable]('1,12,36,65,58,56,DF',',')
-- 说明:把用逗号分隔的字符串转换成表格数据
ALTER FUNCTION [dbo].[fn_StringSplitToTable]
(
@StringX varchar(MAX),
@Split nvarchar(10)
)
RETURNS
@IdsTable TABLE
(
[ID] nvarchar(200)
)
AS
BEGIN
DECLARE @Index int
DECLARE @LenIndex int
SET @LenIndex=LEN(@Split)
SET @Index=CHARINDEX(@Split,@StringX,1)
WHILE (@Index>=1)
BEGIN
IF(LEFT(@StringX,@Index-1) <> '')
INSERT INTO @IdsTable SELECT LTRIM(RTRIM(LEFT(@StringX,@Index-1)))
SET @StringX=RIGHT(@StringX,LEN(@StringX)-@Index-@LenIndex+1)
SET @Index=CHARINDEX(@Split,@StringX,1)
END
IF(@StringX<>'') INSERT INTO @IdsTable SELECT LTRIM(RTRIM(@StringX))
RETURN
END
GO