|
此文章由 gooderic1977 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 gooderic1977 所有!转贴必须注明作者、出处和本声明,并保持内容完整
你需要一个function来split text by ",",然后把结果存到临时表里处理:
CREATE FUNCTION [dbo].[fn_SplitString] (
@Text nText,
@delimiter char(1)
)
RETURNS @split_string table
(id int IDENTITY(1,1) NOT NULL, part_string varchar(8000) NULL)
AS
BEGIN
DECLARE @n_of_delimiters AS int
DECLARE @i AS int
DECLARE @pstr As Varchar(8000)
DECLARE @StartCharacter As BigInt
DECLARE @EndCharacter As BigInt
DECLARE @NumberIterations As BigInt
DECLARE @CurrentIteration As BigInt
DECLARE @DataLength As Decimal(19,2)
DECLARE @CharacterLength As BigInt
SET @CharacterLength = 3996
SET @DataLength = DATALENGTH (@Text) / 2 -- Divide the length by 2 as UNICODE uses 2 bytes to store 1 character of data
SET @NumberIterations = CEILING(@DataLength / @CharacterLength)
SET @CurrentIteration = 1
SET @StartCharacter = 1
SET @EndCharacter = @CharacterLength
WHILE @CurrentIteration <= @NumberIterations
BEGIN
SET @pstr = SUBSTRING(@Text,@StartCharacter,@EndCharacter)
SET @pstr = SUBSTRING(@pstr,1,@CharacterLength)
-- Remove the ended comma
If Right(@pstr,1) = @delimiter Set @pstr = substring(@pstr,1,Len(@pstr)-1)
SET @n_of_delimiters=DATALENGTH(REPLACE(@pstr,@delimiter,@delimiter+'_')) - DATALENGTH(@pstr)
SET @i = 0
WHILE @i < @n_of_delimiters
BEGIN
SET @i = @i + 1
INSERT INTO @split_string
SELECT LEFT(@pstr,CHARINDEX(@delimiter,@pstr)-1)
SET @pstr=SUBSTRING(@pstr,CHARINDEX(@delimiter,@pstr)+1,8000)
END
INSERT INTO @split_string VALUES(@pstr)
SET @CurrentIteration = @CurrentIteration + 1
SET @StartCharacter = (@CharacterLength * (@CurrentIteration-1)) + 1
SET @EndCharacter = @CharacterLength * (@CurrentIteration)
END
RETURN
END
--SELECT * FROM [dbo].[fn_SplitString]('a,b,c,d',',')
|
评分
-
查看全部评分
|