SQL Server Functions

Source: Internet
Author: User
Tags rtrim

1.fun_split split string to form a return table

CREATEFUNCTION [dbo].[Fun_split](        @Items VARCHAR(MAX),    @SplitStr VARCHAR(MAX))RETURNS @SplitTable TABLE(ItemVARCHAR(MAX))  asBEGIN    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); RETURNEND

2.fun_arrlen split string, return length

CREATE FUNCTION [dbo].[Fun_arrlen](        @Str varchar(Max),    @SplitStr varchar(Max))RETURNS int  asbegin     Declare @i Int       Set @i =(Len(@Str)- Len(Replace(@Str,@SplitStr,"')))/Len(@SplitStr)+1      Return(@i)End

3.fun_getstrindex analog Array Get value

CREATE FUNCTION [dbo].[Fun_getstrindex](        @Str varchar(Max),    @SplitStr varchar(Max),    @index int)RETURNS varchar(Max)  asbegin     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

Operation Result:

SQL Server Functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.