资源描述:
《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 declare str_return varcha
2、r(8000) declare i int declaretemp_iint declareonlineornotint declarefindepnumokint --用来取得一个epnum, --规则:首先从chatid中取,如果有在线得,则取得最前面得在线得返回 -- 如果全部不在线,则返回00000000 selectfindepnumok=0 selecttemp_i=0 IFlen(str)<=0 begin SELECTstr_return='00000000' end else b
3、egin selecti=dbo.getstrcount(str,',') p_i<i BEGIN selectonlineornot=onlinefrom=dbo.getstrofindex(str,',',temp_i) IF(onlineornot=1) begin selectstr_return=dbo.getstrofindex(str,',',temp_i) selectfindepnumok=1--找到epnum后置为1 BREAK end ELSE
4、 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_IDENTIFIEROFF GO SETANSI_NULLSON GO SETQUOTED_IDENTIFIERON GO SETANSI_NULLSON GO --get
5、strcount 输入一个没有分割的字符串,以及分割符 --返回数组的个数 CREATE function getstrcount(strvarchar(8000),splitstrvarchar(100)) --returnsvarchar(8000) returnsint as begin declare int_returnint declare start int declare next int declare location int selectnext=0 selectlocation=1 ifl
6、en(str)<len(splitstr) selectint_return=0 ifcharindex(splitstr,str)=0 selectint_return=0 while (location<>0) begin select start = location +1 select location = charindex(splitstr,str,start) 12下一页>>>>这篇文章来自..,。select next =next+1 select int_return=next end ret
7、urn int_return end GO SETQUOTED_IDENTIFIEROFF GO SETANSI_NULLSON GO SETQUOTED_IDENTIFIERON GO SETANSI_NULLSON GO -- getstrofindex 输入一个未分割的字符串,舒服分割符号,舒服要取得的字符位置 --返回制定位置的字符串 CREATE function getstrofindex(strvarchar(8000),splitstrvarchar(4),indexint=0) returns varcha
8、r(8000) as begin declare str_retur