SQL中自己创建函数,分割字符串2011-10-14if exists (select * from dbo.sysobjects where id = object_id(N"[dbo].[getEPnum]") and xtype in (N"FN", N"IF", N"TF"))drop function [dbo].[getEPnum]GOif exists (select * from dbo.sysobjects where id = object_id(N"[dbo].[getstrcount]") and xtype in (N"FN", N"IF", N"TF"))drop function [dbo].[getstrcount]GOif exists (select * from dbo.sysobjects where id = object_id(N"[dbo].[getstrofindex]") and xtype in (N"FN", N"IF", N"TF"))drop function [dbo].[getstrofindex]GOSET QUOTED_IDENTIFIER ONGOSET ANSI_NULLS ONGO--- 这个函数直接调用了另外的两个函数,可以先阅读下面提到的两个函数CREATE function getEPnum (@str varchar(8000))returns varchar(8000)asbegindeclare @str_return varchar(8000)declare @i intdeclare @temp_i intdeclare @onlineornot intdeclare @findepnumok int-- 用来取得一个epnum,-- 规则:首先从chatid中取,如果有在线得,则取得最前面得在线得返回-- 如果全部不在线,则返回 ‘00000000’select @findepnumok = 0select @temp_i = 0IF len(@str)<=0beginSELECT @str_return = "00000000"endelsebeginselect @i = dbo.getstrcount(@str,",")WHILE @temp_i<@iBEGINselect @onlineornot = online from wwchat_user where epnum=dbo.getstrofindex(@str,",",@temp_i)IF (@onlineornot=1)beginselect @str_return =dbo.getstrofindex(@str,",",@temp_i)select @findepnumok = 1 --找到epnum后置为1BREAKendELSEbeginselect @temp_i = @temp_i + 1select @findepnumok = 0 --找不到epnum后置为1endENDif @findepnumok = 0beginSELECT @str_return = "00000000"endendreturn @str_returnendGOSET QUOTED_IDENTIFIER OFFGOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_NULLS ONGO