首页 > 其他 > 详细

获取指定索引的值的函数

时间:2017-01-03 14:58:48      阅读:167      评论:0      收藏:0      [点我收藏+]
Go
--创建函数(作者:csdn邹建)
create function getstrofindex (@str varchar(8000),@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 --如果习惯从开始则select @next =0
  select @location = charindex(,,@str,@start)
  while (@location <>0 and @index > @next )
  begin
    select @start = @location +1
    select @location = charindex(,,@str,@start)
    select @next =@next +1
  end
  if @location =0 select @location =len(@str)+1 --如果是因为没有逗号退出,则认为逗号在字符串后
  select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗号之后的位置或者就是初始值
  if (@index <> @next ) select @str_return = ‘‘ --如果二者不相等,则是因为逗号太少,或者@index小于@next的初始值。
  return @str_return
end
 
--测试示例
SELECT [dbo].[getstrofindex](1,2,3,4,a,b,c,d,4)
 
--运行结果
/*
4
*/

 

获取指定索引的值的函数

原文:http://www.cnblogs.com/accumulater/p/6244711.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!