標籤:
SQL自訂函數split分隔字串
一、F_Split:分割字串拆分為資料表
Create FUNCTION [dbo].[F_Split] ( @SplitString nvarchar(max), --源字串 @Separator nvarchar(10)=‘ ‘ --分隔字元號,預設為空白格 ) RETURNS @SplitStringsTable TABLE --輸出的資料表
( [id] int identity(1,1), [value] nvarchar(max) ) AS BEGIN DECLARE @CurrentIndex int; DECLARE @NextIndex int; DECLARE @ReturnText nvarchar(max); SELECT @CurrentIndex=1; 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,@[email protected]); INSERT INTO @SplitStringsTable([value]) VALUES(@ReturnText); SELECT @[email protected]+1; END RETURN; END --使用樣本select * FROm dbo.F_Split(‘111,b2222,323232,32d,e,323232f,g3222‘, ‘,‘)
結果為
id value
-------- ---------------------------------------
1 111
2 b2222
3 323232
4 32d
5 e
6 323232f
7 g3222
=========================================================================
二、F_SplitLength:擷取分割後的字元數組的長度
Create function [dbo].[F_SplitLength] ( @String nvarchar(max), --要分割的字串 @Split nvarchar(10) --分隔字元號 ) returns int as begin declare @location int declare @start int declare @length int set @String=ltrim(rtrim(@String)) set @location=charindex(@split,@String) set @length=1 while @location<>0 begin set @[email protected]+1 set @location=charindex(@split,@String,@start) set @[email protected]+1 end return @length end--調用樣本select dbo.F_SplitLength(‘111,b2222,323232,32d,e,323232f,g3222‘,‘,‘)
結果為7。
=========================================================================
三、F_SplitOfIndex:擷取分割後特定索引的字串
Create function [dbo].[F_SplitOfIndex] ( @String nvarchar(max), --要分割的字串 @split nvarchar(10), --分隔字元號 @index int --取第幾個元素 ) returns nvarchar(1024) as begin declare @location int declare @start int declare @next int declare @seed int set @String=ltrim(rtrim(@String)) set @start=1 set @next=1 set @seed=len(@split) set @location=charindex(@split,@String) while @location<>0 and @index>@next begin set @[email protected][email protected] set @location=charindex(@split,@String,@start) set @[email protected]+1 end if @location =0 select @location =len(@String)+1 return substring(@String,@start,@[email protected]) end--使用樣本select dbo.F_SplitOfIndex(‘111,b2222,323232,32d,e,323232f,g3222‘,‘,‘, 3)
結果為323232。
轉自:http://www.cnblogs.com/xiaofengfeng/archive/2012/06/01/2530930.html
-------------------------------------------------------------------------------------
SQL字串逗號分隔函數
繼SQl -Function建立函數資料庫輸出的結果用逗號隔開,在開發中也有許多以參數的形式傳入帶逗號字條串參數(資料大時不建議這樣做)
例:尋找姓名為“張三,李二” 的資料此時在資料庫裡就要對此參數做處理
函數代碼如下
CREATE FUNCTION [dbo].[fnSplitStr] (
@sText NVARCHAR(Max),
@sDelim CHAR(1)
)
RETURNS @retArray TABLE (
value VARCHAR(100)
)
AS
BEGIN
DECLARE
@posStart BIGINT,
@posNext BIGINT,
@valLen BIGINT,
@sValue NVARCHAR(100);
IF @sDelim IS NULL
BEGIN
IF LEN(@sText)>100 SET @sText = SUBSTRING(@sText, 1, 100)
INSERT @retArray (value)
VALUES (@sText);
END
ELSE
BEGIN
SET @posStart = 1;
WHILE @posStart <= LEN(@sText)
BEGIN
SET @posNext = CHARINDEX(@sDelim, @sText, @posStart);
IF @posNext <= 0
SET @valLen = LEN(@sText) - @posStart + 1;
ELSE
SET @valLen = @posNext - @posStart;
SET @sValue = SUBSTRING(@sText, @posStart, @valLen);
SET @posStart = @posStart + @valLen + 1;
IF LEN(@sValue) > 0
BEGIN
IF LEN(@sValue)>100 SET @sValue = SUBSTRING(@sValue, 1, 100)
INSERT @retArray (value)
VALUES (@sValue);
END
END
END
RETURN
END
SQL自訂函數split分隔字串