Split text into temporary table (Fn_split_text) by delimiter

Source: Internet
Author: User

/************************************************************** Name: Fn_split_text feature Description: [Split text into temporary table by delimiter] parameter introduction: [@ Text] to be split back: Table: Subcolstr: Split String Special Description: SELECT * from Fn_split_text (' xxxx,yyyy,dddd ') ************************ **************************************/if object_id('Fn_split_text')>0    Drop functionFn_split_textGOCreate functionDbo.fn_split_text (@text nvarchar(Max))returns @returntable Table(Subcolstrvarchar( $)) as  begin    --------------------------Define variables-----------------------------------------------------------------------------    Declare @commastr varchar(Max),@splitstr varchar(Max),@splitlen int,@length int    Declare @splitChar Char(1)    --------------------------Assigning initial value-------------------------------------------------------------------------------    Select  @splitChar = ','    -----------------------------------------------------------------------------------------------------    Select @splitstr=@text,        @splitlen=charindex(@splitChar,@splitstr),        @length=datalength(@splitstr)     while @length>0    begin    --------------------------------------------------------------------------------------        if     @splitlen=0                   Set @splitlen=@length+1           Set    @commastr =@splitstr        Insert @returntable(SUBCOLSTR)Values(substring(@commastr,1,@splitlen-1))        Select @splitstr = substring(@commastr,@splitlen+1,@length),                   @length=datalength(@splitstr),                   @splitlen=charindex(@splitChar,@splitstr)   --------------------------------------------------------------------------------------    End        returnEndGO

Split text into temporary table (Fn_split_text) by delimiter

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.