码迷,mamicode.com
首页 > 数据库 > 详细

SqlServer Function

时间:2019-01-02 12:38:18      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:sel   and   cti   分割   div   --   ide   ati   start   

 

set quoted_identifier on;
set ansi_nulls on;
go

create function [dbo].[Get_StrArrayStrOfIndex]
       ( @str NVarchar(max) ,  --要分割的字符串
         @split NVarchar(10) ,  --分隔符号
         @index Int --取第几个元素
         )
returns NVarchar(max)
as
begin
    declare @location Int;
    declare @start Int;
    declare @next Int;
    declare @seed Int;

    set @str = LTrim(RTrim(@str));
    set @start = 1;
    set @next = 1;
    set @seed = Len(@split);
  
    set @location = CharIndex(@split, @str);
    while @location <> 0 and @index > @next
          begin
                set @start = @location + @seed;
                set @location = CharIndex(@split, @str, @start);
                set @next = @next + 1;
          end;
    if @location = 0
       select   @location = Len(@str) + 1; 
  
    return Substring(@str,@start,@location-@start);

end;
go

 

set quoted_identifier on;
set ansi_nulls on;
go

create function [dbo].[Get_StrArrayLength]
       ( @str NVarchar(max) ,  --要分割的字符串
         @split NVarchar(10)  --分隔符号
         )
returns Int
as
begin
    declare @location Int;
    declare @start Int;
    declare @length Int;

    set @str = LTrim(RTrim(@str));
    set @location = CharIndex(@split, @str);
    set @length = 1;
    while @location <> 0
          begin
                set @start = @location + 1;
                set @location = CharIndex(@split, @str, @start);
                set @length = @length + 1;
          end;
    return @length;

end;
go

 

SqlServer Function

标签:sel   and   cti   分割   div   --   ide   ati   start   

原文地址:https://www.cnblogs.com/kikyoqiang/p/10207250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!