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

时间:2024年07月08日

/

来源:sz206832

/

编辑:本站小编

收藏本文

下载本文

以下是小编收集整理的SQL中自己创建函数,分割字符串,本文共4篇,希望对大家有所帮助。本文原稿由网友“sz206832”提供。

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

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

--- 这个函数直接调用了另外的两个函数,可以先阅读下面提到的两个函数

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

-- 用来取得一个epnum,

-- 规则:首先从chatid中取,如果有在线得,则取得最前面得在线得返回

-- 如果全部不在线,则返回 ‘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 --找到epnum后置为1

BREAK

end

ELSE

begin

select @temp_i = @temp_i + 1

select @findepnumok = 0 --找不到epnum后置为1

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

--getstrcount 输入一个没有分割的字符串,以及分割符

--返回数组的个数

CREATE function getstrcount (@str varchar(8000),@splitstr varchar(100))

--returns varchar(8000)

returns int

as

begin

declare @int_return int

declare @start int

declare @next int

declare @location int

select @next = 0

select @location = 1

if len(@str)

select @int_return =0

if charindex(@splitstr,@str) = 0

select @int_return =0

while (@location0)

begin

select @start = @location + 1

select @location = charindex(@splitstr,@str,@start)

select @next = @next + 1

select @int_return = @next

end

return @int_return

end

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_NULLS ON

GO

-- getstrofindex 输入一个未分割的字符串,舒服分割符号,舒服要取得的字符位置

-- 返回 制定位置的字符串

CREATE function getstrofindex (@str varchar(8000),@splitstr varchar(4),@index int=0)

returns varchar(8000)

as

begin

declare @str_return varchar(8000)

declare @start int

declare @next int

declare @location int

select @start =1

select @next = 1 --如果习惯从0开始则select @next =0

select @location = charindex(@splitstr,@str,@start)

while (@location 0 and @index >@next )

begin

select @start = @location +1

select @location = charindex(@splitstr,@str,@start)

select @next =@next +1

end

if @location =0 select @location =len(@str)+1 --如果是因为没有逗号退出,则认为逗号在字符串后

select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗号之后的位置或者就是初始值1

if (@index @next ) select @str_return = '' --如果二者不相等,则是因为逗号太少,或者@index小于@next的初始值1,

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

return @str_return

end

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

篇2:SQL 中自己创建函数,分割字符串数据库教程

创建|函数|字符串

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

/**

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

*

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

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

* QQ : 9690501

*

* 所有请注明本信息!

*/

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

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

--- 这个函数直接调用了另外的两个函数,可以先阅读下面提到的两个函数

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

-- 用来取得一个epnum,

-- 规则:首先从chatid中取,如果有在线得,则取得最前面得在线得返回

--      如果全部不在线,则返回 ‘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 --找到epnum后置为1

BREAK

end

ELSE

begin

select @temp_i = @temp_i + 1

select @findepnumok = 0 --找不到epnum后置为1

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

-- getstrcount 输入一个没有分割的字符串,以及分割符

--返回数组的个数

CREATE    function getstrcount (@str varchar(8000),@splitstr varchar(100))

--returns varchar(8000)

returns int

as

begin

declare @int_return int

declare @start int

declare @next int

declare @location int

select @next = 0

select @location = 1

if len(@str)

select @int_return =0

if charindex(@splitstr,@str) = 0

select @int_return =0

while (@location0)

begin

select @start = @location + 1

select @location = charindex(@splitstr,@str,@start)

select @next = @next + 1

select @int_return = @next

end

return @int_return

end

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_NULLS ON

GO

-- getstrofindex 输入一个未分割的字符串,舒服分割符号,舒服要取得的字符位置

-- 返回 制定位置的字符串

CREATE  function getstrofindex (@str varchar(8000),@splitstr varchar(4),@index int=0)

returns varchar(8000)

as

begin

declare @str_return varchar(8000)

declare @start int

declare @next int

declare @location int

select @start =1

select @next = 1 --如果习惯从0开始则select @next =0

select @location = charindex(@splitstr,@str,@start)

while (@location 0 and @index > @next )

begin

select @start = @location +1

select @location = charindex(@splitstr,@str,@start)

select @next =@next +1

end

if @location =0 select @location =len(@str)+1 --如果是因为没有逗号退出,则认为逗号在字符串后

select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗号之后的位置或者就是初始值1

if (@index  @next ) select @str_return = '' --如果二者不相等,则是因为逗号太少,或者@index小于@next的初始值1,

SQL 中自己创建函数,分割字符串数据库教程

return @str_return

end

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

篇3:Dephi 中优秀的字符串分割函数.net

DELPHI没有自己的字符串分割函数,所以只能 程序员 自己写了,网上搜了好多但是真正好用的没有几个, 下面这个是我在网上找到修改后了的,个人感觉算法不错,所以就贴了上来, functionSplitString(Source,Deli:string):TStringList;s td call; var EndOfCurr

DELPHI没有自己的字符串分割函数,所以只能程序员自己写了,网上搜了好多但是真正好用的没有几个,

下面这个是我在网上找到修改后了的,个人感觉算法不错,所以就贴了上来。

function SplitString(Source, Deli: string ): TStringList;stdcall;

var

EndOfCurrentString: byte;

StringList:TStringList;

begin

StringList:=TStringList.Create;

while Pos(Deli, Source)>0 do

begin

EndOfCurrentString := Pos(Deli, Source);

StringList.add(Copy(Source, 1, EndOfCurrentString - 1));

Source := Copy(Source, EndOfCurrentString + length(Deli), length(Source) - EndOfCurrentString);

end;

Result := StringList;

StringList.Add(source);

end;

本文引用通告地址: blog.csdn.net/mahuzi/services/trackbacks/502563.aspx

原文转自:www.ltesting.net

篇4:[]mysql中的inteval函数自己看书没整明白的看过来

书上是这样写的:

INTERVAL(N,N1,N2,N3,...)

假如N < N1,则返回值为0;假如N < N2 等等,则返回值为1;假如N 为NULL,则返回值为 -1 ,所有的参

数均按照整数处理。为了这个函数的正确运行,必须满足N1 < N2 < N3 < ……< Nn 。其原因是使用了二分

查找(极快速)。

mysql>SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200);

->3

mysql>SELECT INTERVAL(10, 1, 10, 100, 1000);

->2

mysql>SELECT INTERVAL(22, 23, 30, 44, 200);

->0

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

看半天没明白什么意思,以为是返回的大于N的个数,但实际自己做实验的时候发现不是,查网上资料也不好查,最终又试了几次之后方才明白,原来是返回的第一个大于N的数字的位置,当然编号是从0开始,看下例子:

mysql>select interval(2,1,3,4,5,8);

+-----------------------+

| interval(2,1,3,4,5,8) |

+-----------------------+

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 1 |

+-----------------------+

1 row in set (0.00 sec)

--上面第一个大于N(例中为2)的是3,位置编号为1,因此返回1,再看下面几个例子自然会明白:

mysql>select interval(2,1,2,4,5,8);

+-----------------------+

| interval(2,1,2,4,5,8) |

+-----------------------+

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 2 |

+-----------------------+

1 row in set (0.00 sec)

mysql>select interval(20,1,2,4,5,8,90);

+---------------------------+

| interval(20,1,2,4,5,8,90) |

+---------------------------+

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 5 |

+---------------------------+

1 row in set (0.00 sec)

PHP中的字符串函数说明PHP

Sybase中SQL语言概述

怎样用Excel中的随机函数

大气中的水汽滞留函数

动态创建SQL Server数据库、表、存储过程数据库教程

下载SQL中自己创建函数,分割字符串(合集4篇)
SQL中自己创建函数,分割字符串.doc
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档
点击下载本文文档