| 订阅 | 在线投稿
分享
 
 
 

教你在SQL Server数据库中拆分字符串函数

来源:互联网网民  宽屏版  评论
2008-06-01 06:45:54

SQL Server数据库中拆分字符串函数的具体方法:

CREATE FUNCTION uf_StrSplit '1.1.2.50','.'

(@origStr varchar(7000), --待拆分的字符串

@markStr varchar(100)) --拆分标记,如','

RETURNS @splittable table

(

str_id varchar(4000) NOT NULL, --编号ID

string varchar(2000) NOT NULL --拆分后的字符串

)

AS

BEGIN

declare @strlen int,@postion int,@start int,@sublen int,

@TEMPstr varchar(200),@TEMPid int

SELECT @strlen=LEN(@origStr),@start=1,@sublen=0,@postion=1,

@TEMPstr='',@TEMPid=0

if(RIGHT(@origStr,1)<>@markStr )

begin

set @origStr = @origStr + @markStr

end

WHILE((@postion<=@strlen) and (@postion !=0))

BEGIN

IF(CHARINDEX(@markStr,@origStr,@postion)!=0)

BEGIN

SET @sublen=CHARINDEX(@markStr,@origStr,@postion)-@postion;

END

ELSE

BEGIN

SET @sublen=@strlen-@postion+1;

END

IF(@postion<=@strlen)

BEGIN

SET @TEMPid=@TEMPid+1;

SET @TEMPstr=SUBSTRING(@origStr,@postion,@sublen);

INSERT INTO @splittable(str_id,string)

values(@TEMPid,@TEMPstr)

IF(CHARINDEX(@markStr,@origStr,@postion)!=0)

BEGIN

SET @postion=CHARINDEX(@markStr,@origStr,@postion)+1

END

ELSE

BEGIN

SET @postion=@postion+1

END

END

END

RETURN

END

例如:select * from uf_StrSplit('1,1,2,50',',')

输出结果:

str_id string

1 1

2 1

3 2

4 50

 
特别声明:以上内容(如有图片或视频亦包括在内)为网络用户发布,本站仅提供信息存储服务。
 
SQL Server数据库中拆分字符串函数的具体方法: CREATE FUNCTION uf_StrSplit '1.1.2.50','.' (@origStr varchar(7000), --待拆分的字符串 @markStr varchar(100)) --拆分标记,如',' RETURNS @splittable table ( str_id varchar(4000) NOT NULL, --编号ID string varchar(2000) NOT NULL --拆分后的字符串 ) AS BEGIN declare @strlen int,@postion int,@start int,@sublen int, @TEMPstr varchar(200),@TEMPid int SELECT @strlen=LEN(@origStr),@start=1,@sublen=0,@postion=1, @TEMPstr='',@TEMPid=0 if(RIGHT(@origStr,1)<>@markStr ) begin set @origStr = @origStr + @markStr end WHILE((@postion<=@strlen) and (@postion !=0)) BEGIN IF(CHARINDEX(@markStr,@origStr,@postion)!=0) BEGIN SET @sublen=CHARINDEX(@markStr,@origStr,@postion)-@postion; END ELSE BEGIN SET @sublen=@strlen-@postion+1; END IF(@postion<=@strlen) BEGIN SET @TEMPid=@TEMPid+1; SET @TEMPstr=SUBSTRING(@origStr,@postion,@sublen); INSERT INTO @splittable(str_id,string) values(@TEMPid,@TEMPstr) IF(CHARINDEX(@markStr,@origStr,@postion)!=0) BEGIN SET @postion=CHARINDEX(@markStr,@origStr,@postion)+1 END ELSE BEGIN SET @postion=@postion+1 END END END RETURN END 例如:select * from uf_StrSplit('1,1,2,50',',') 输出结果: str_id string 1 1 2 1 3 2 4 50
󰈣󰈤
 
 
 
>>返回首页<<
 
 热帖排行
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有