一个分隔字符串的函数,并显示出分隔符的个数

此函数用来分隔字符串,如“#ddfd##dfsdf###”,分隔符为“#”,用函数分隔后结果为:1ddfd2dfsdf3

函数详细内容:

 

代码
   
     
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


-- =============================================
--
Author: <高龙>
--
Create date: <2010-09-22>
--
Description: <拆分字符串,并得到区分字符串的字符的个数>
--
字符串格式:#到了极点###得分偶尔偶尔##发了的肌肤###的决定###### (其中#的个数不确定也无规律,并且字符结尾可以是#也可以是文字)
--
=============================================
ALTER FUNCTION [ dbo ] . [ funSplitString ]
(
@strString VARCHAR ( MAX ), @strChar VARCHAR ( 1 )
)
RETURNS VARCHAR ( MAX )
AS
BEGIN
DECLARE @i INT ;
DECLARE @count INT ;
DECLARE @strReturn VARCHAR ( MAX );
DECLARE @subString VARCHAR ( 1000 );

SET @subString = '' ;
SET @strReturn = '' ;
SET @count = 0 ;
SET @i = CHARINDEX ( @strChar , @strString );
WHILE @i >= 0
BEGIN
IF @i = 1
BEGIN
SET @count = @count + 1 ;
SET @strString = SUBSTRING ( @strString , 2 , LEN ( @strString ) - 1 );
SET @i = CHARINDEX ( @strChar , @strString );
END
ELSE
BEGIN
IF @i = 0
BEGIN
SET @strReturn = @strReturn + CONVERT ( VARCHAR ( 50 ), @count );
BREAK ;
END
SET @subString = SUBSTRING ( @strString , 1 , @i - 1 );
SET @strReturn = @strReturn + CONVERT ( VARCHAR ( 50 ), @count ) + @subString ;
SET @strString = SUBSTRING ( @strString , @i , LEN ( @strString ) - LEN ( @subString ));
SET @i = CHARINDEX ( @strChar , @strString );
SET @count = 0 ;
END
END

RETURN @strReturn ;
END


 

你可能感兴趣的:(字符串)