zoukankan      html  css  js  c++  java
  • sql server 函数

    1.Fun_Split 分割字符串,形成返回表

    CREATE FUNCTION [dbo].[Fun_Split]
    (    
        @Items VARCHAR(MAX),
        @SplitStr VARCHAR(MAX)
    )
    RETURNS @SplitTable TABLE(Item VARCHAR(MAX)) 
    AS
    BEGIN
        DECLARE @Split_Index INT=0;
        DECLARE @Split_len INT=0;
        SET @Items = RTRIM(LTRIM(@Items));
        SET @Split_Index = CHARINDEX(@SplitStr,@Items);
        SET @Split_len=LEN(@SplitStr);
        WHILE(@Split_Index>=1)
        BEGIN    
            INSERT INTO @SplitTable VALUES(LEFT(@Items,@Split_Index-1));
            SET @Items = SUBSTRING(@Items,@Split_Index + @Split_len,LEN(@Items)-@Split_Index);
            SET @Split_Index = CHARINDEX(@SplitStr,@Items);
        END
        IF(@Items<>'')
            INSERT INTO @SplitTable VALUES(@Items);
        RETURN
    END

    2.Fun_arrlen 分割字符串,返回长度

    CREATE FUNCTION [dbo].[Fun_arrlen]
    (    
        @Str varchar(max),
        @SplitStr varchar(max)
    )
    RETURNS int 
    AS
    begin
         Declare @i Int  
         Set @i = (Len(@Str) - Len(Replace(@Str,@SplitStr,'')))/Len(@SplitStr) +1 
         Return(@i)
    end

    3.Fun_GetStrIndex 模拟数组获取值

    CREATE FUNCTION [dbo].[Fun_GetStrIndex]
    (    
        @Str varchar(max),
        @SplitStr varchar(max),
        @index int
    )
    RETURNS varchar(max) 
    AS
    begin 
        declare @location int
        declare @start int
        declare @next int
        declare @seed int
    
        set @str=ltrim(rtrim(@str))
        set @start=0
        set @next=0
        set @seed=len(@SplitStr)
     
        set @location=charindex(@SplitStr,@str)
        while @location<>0 and @index>@next
        begin
            set @start=@location+@seed
            set @location=charindex(@SplitStr,@str,@start)
            set @next=@next+1
        end
        if @location =0 select @location =len(@str)+1
        return substring(@str,@start,@location-@start);
    end

    运行结果:

    欢迎指正:haizi2014@qq.com
  • 相关阅读:
    day 29-2 socket 文件传输、udp协议
    day 29-1 socket
    day 28-1 元类、异常处理
    day 27-1 反射、内置方法
    day 26-1 property、绑定与非绑定方法
    day 25-1 接口类、抽象类、多态
    day 24-1 继承
    day 23-1 类的命名空间、组合
    day 22
    PyMySQL操作mysql数据库(py3必学)
  • 原文地址:https://www.cnblogs.com/hcfan/p/5160422.html
Copyright © 2011-2022 走看看