余世维讲座视频,维尼熊官网,ok88.us
--创建一个函数,函数中有两个参数
create function [dbo].[splittotable]
(
@splitstring nvarchar(max),--输入的字符串
@separator nvarchar(10)=' '--分割条件
)
--返回的数据为一个表
returns @splitstringstable table
(
[id] int identity(1,1),
[value] nvarchar(max)
)
as
begin
declare @currentindex int;--当前索引
declare @nextindex int;--下一个索引
declare @returntext nvarchar(max);--返回内容
select @currentindex=1;--设置当前索引初始值为1
--当当前索引小于字符串长度时,执行循环体
while(@currentindex<=len(@splitstring))
begin
--charindex函数返回字符或者字符串在另一个字符串中的起始位置。charindex函数调用方法如下:
--charindex ( expression1 , expression2 [ , start_location ] )
-- expression1是要到expression2中寻找的字符中,start_location是charindex函数开始在expression2中找expression1的位置。
select @nextindex=charindex(@separator,@splitstring,@currentindex);
if(@nextindex=0 or @nextindex is null)
select @nextindex=len(@splitstring)+1;
--函数用法:substring ( expression, start, length )
select @returntext=substring(@splitstring,@currentindex,@nextindex-@currentindex);
insert into @splitstringstable([value]) values(@returntext);
select @currentindex=@nextindex+1;
end
return;
end
go
--测试函数
select * from dbo.splittotable('a,b,c,d,e,f,g', ',')
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
-
sql某个日期是当年的第几周
/* *周一作为一周的开始 *当年的1月1号所在的周算作第一周 */ CREATE function GetWeekIndexFirstDate ( @...
[阅读全文]
-
-
数据库SQL---范式
1、数据冗余导致的问题:冗余存储、更新异常、插入异常、删除异常。 2、函数依赖:一种完整性约束。 在关系模式r(R)中,α属于R,β属于R。 1)α函数...
[阅读全文]
-
-
数据库SQL---查询
1、查询所有列 select *from emp;--*表示所有的,from emp表示从emp表中查询。 2、查询指定列 select empno,e...
[阅读全文]
-
-
-
-
网友评论