资源描述:
《sql中自己创建函数,分割字符串》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、SQL中自己创建函数,分割字符串>>教育资源库 ifexists(select*fromdbo.sysobjects]')andxtypein(N'FN',N'IF',N'TF')) dropfunction[dbo].[getEPnum] GO ifexists(select*fromdbo.sysobjectsdbo.sysobjects (str varchar(8000)) returns varchar(8000) as begin
2、 declare str_return varchar(8000) declare i int declaretemp_iint declareonlineornotint declarefindepnumokint --用来取得一个epnum, --规则:首先从chatid中取,如果有在线得,则取得最前面得在线得返回 -- 如果全部不在线,则返回00000000 selectfindepnumok=0 selecttemp_i=0 IFlen(str)<=0 begin SE
3、LECTstr_return='00000000' end else begin selecti=dbo.getstrcount(str,',') p_i<i BEGIN selectonlineornot=onlinefrom=dbo.getstrofindex(str,',',temp_i) IF(onlineornot=1) begin selectstr_return=dbo.getstrofindex(str,'
4、;,',temp_i) selectfindepnumok=1--找到epnum后置为1 BREAK end ELSE begin selecttemp_i=temp_i+1 selectfindepnumok=0--找不到epnum后置为1 end END iffindepnumok=0 begin SELECTstr_return='00000000' end end return str_return end GO SETQUOTED_IDENT
5、IFIEROFF GO SETANSI_NULLSON GO SETQUOTED_IDENTIFIERON GO SETANSI_NULLSON GO --getstrcount 输入一个没有分割的字符串,以及分割符 --返回数组的个数 CREATE function getstrcount(strvarchar(8000),splitstrvarchar(100)) --returnsvarchar(8000) returnsint as begin declare in
6、t_returnint declare start int declare next int declare location int selectnext=0 selectlocation=1 iflen(str)<len(splitstr) selectint_return=0 ifcharindex(splitstr,str)=0 selectint_return=0 while (location<>0) begin select start = loc
7、ation +1 select location = charindex(splitstr,str,start) 12下一页>>>>这篇文章来自..,。select next =next+1 select int_return=next end return int_return end GO SETQUOTED_IDENTIFIEROFF GO SETANSI_NULLSON GO SETQUOTED_IDENTIFIERON GO SETANSI_NULLSON GO -- g
8、etstrofindex 输入一个未分割的字符串,舒服分割符号,舒服要取得的字符位置 --返回制定位置的字符串 CREATE function getstrofindex(strvarchar(8000),splitstrvarchar(4),indexint=0) returns varchar(8000) as begin declare str_