Create functions in SQL, split strings

Source: Internet
Author: User

if 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]

Go

if 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]

Go

if 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]

Go

SET QUOTED_IDENTIFIER ON

Go

SET ANSI_NULLS on

Go

---This function calls the other two functions directly, and you can read the two functions mentioned below first.

CREATE function Getepnum (@str varchar (8000))

Returns varchar (8000)

As

Begin

DECLARE @str_return varchar (8000)

DECLARE @i int

DECLARE @temp_i int

DECLARE @onlineornot int

DECLARE @findepnumok int

--To get a epnum,

--Rules: first from the Chatid, if there is online, then get the front must be returned online

--If all is not on the line, return ' 00000000 '

Select @findepnumok = 0

Select @temp_i = 0

IF Len (@str) <=0

Begin

SELECT @str_return = ' 00000000 '

End

Else

Begin

Select @i = Dbo.getstrcount (@str, ', ')

While @temp_i <@i

BEGIN

Select @onlineornot = online from Wwchat_user where Epnum=dbo.getstrofindex (@str, ', ', @temp_i)

IF (@onlineornot =1)

Begin

Select @str_return =dbo.getstrofindex (@str, ', ', @temp_i)

Select @findepnumok = 1--Find epnum after 1

Break

End

ELSE

Begin

Select @temp_i = @temp_i + 1

Select @findepnumok = 0--Epnum 1 not found

End

End

If @findepnumok = 0

Begin

SELECT @str_return = ' 00000000 '

End

End

Return @str_return

End

Go

SET QUOTED_IDENTIFIER OFF

Go

SET ANSI_NULLS on

Go

SET QUOTED_IDENTIFIER ON

Go

SET ANSI_NULLS on

Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.