您的位置:首页 > 数据库

SQL中自己创建函数,分割字符串

2008-04-26 23:32 549 查看
----------------------------------------------------------------

/**

* 版权: 石太祥[E.Alpha] 所有;

*

* email: ealpha(AT)msn(DOT)com ;

* msn:ealpha(AT)msn(DOT)com ;

* :9690501

*

* 所有转载请注明本信息!

*/

----------------------------------------------------------------

ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[getEPnum]')andxtypein(N'FN',N'IF',N'TF'))
dropfunction[dbo].[getEPnum]
GO

ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[getstrcount]')andxtypein(N'FN',N'IF',N'TF'))
dropfunction[dbo].[getstrcount]
GO

ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[getstrofindex]')andxtypein(N'FN',N'IF',N'TF'))
dropfunction[dbo].[getstrofindex]
GO

SETQUOTED_IDENTIFIERON
GO
SETANSI_NULLSON
GO

--- 这个函数直接调用了另外的两个函数,可以先阅读下面提到的两个函数
CREATE function getEPnum (@str varchar(8000))
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @i int
declare@temp_iint
declare@onlineornotint
declare@findepnumokint

--用来取得一个epnum,
--规则:首先从chatid中取,如果有在线得,则取得最前面得在线得返回
-- 如果全部不在线,则返回‘00000000’

select@findepnumok=0
select@temp_i=0

IFlen(@str)<=0
begin
SELECT@str_return='00000000'
end
else
begin
select@i=dbo.getstrcount(@str,',')

WHILE@temp_i<@i
BEGIN
select@onlineornot=onlinefromwwchat_userwhereepnum=dbo.getstrofindex(@str,',',@temp_i)
IF(@onlineornot=1)
begin
select@str_return=dbo.getstrofindex(@str,',',@temp_i)
select@findepnumok=1--找到epnum后置为1
BREAK
end
ELSE
begin
select@temp_i=@temp_i+1
select@findepnumok=0--找不到epnum后置为1共3页 第1页
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: