日期:2014-05-17 浏览次数:20475 次
ALTER FUNCTION split2
(
@SplitString NVARCHAR(999) ,--需要被分隔的字符串
@Separator NVARCHAR(10)--分隔符
)
RETURNS @SplitTable TABLE
(
[value] NVARCHAR(999)
)
AS
BEGIN
DECLARE @CurrentIndex INT=1,@NextIndex INT,@ReturnText NVARCHAR(999)
WHILE ( @CurrentIndex <= LEN(@SplitString) )
BEGIN
SELECT @NextIndex = CHARINDEX(@Separator, @SplitString,@CurrentIndex)
IF ( @NextIndex = 0 OR @NextIndex IS NULL )
SELECT @NextIndex = LEN(@SplitString) + 1
SELECT @ReturnText = SUBSTRING(@SplitString, @CurrentIndex,@NextIndex - @CurrentIndex)
INSERT INTO @SplitTable( [value] ) VALUES ( @ReturnText )
SELECT @CurrentIndex = @NextIndex + 1 ;
END
RETURN
END
SELECT * FROM dbo.SPLIT2(
(SELECT REPLACE(REPLACE(id,',',' '),'-',' ')FROM #a)
,' '
)
value
----------------------------------------------------------------------------------------------------------------
A00
A03
A06
B00
C00
C03
(6 行受影响)
DECLARE @string VARCHAR(100)