SQL Server Split function

Source: Internet
Author: User
Tags rtrim

------------------------------------------------------------------------Copyright: 2011--Time: 2011-06-17--use: Slicing strings, Null characters between consecutive separators and delimiters are ignored--usage: SELECT * FROM [dbo]. [Wf_split] (' A,b,c,d ', ', ');--     returns the table structure:--    id     the self-increment primary key--     rs     Each paragraph of the delimiter slice----------------------------------------------------------------------use qptreasuredbgoif EXISTS ( SELECT * from sys.objects WHERE object_id = object_id (N ' [dbo].[ Wf_split] ') and type in (n ' FN ', n ' IF ', n ' TF ', n ' FS ', n ' FT ')) DROP FUNCTION dbo. [Wf_split] GO----------------------------------------------------------------CREATE FUNCTION [dbo]. [Wf_split] (   @strSource NVARCHAR (4000),   --string to manipulate    @strSeparator CHAR (1)       --delimiter, single byte) RETURNS @tbResult TABLE (id INT IDENTITY (), RS NVARCHAR (+)) with encryption Asbegindeclare @ Dwindex INT, @strResult NVARCHAR (1000); SET @strSource = RTRIM (LTRIM (@strSource)); --Blanking set @dwINdex = CHARINDEX (@strSeparator, @strSource);   --gets the position of the first delimiter
While @dwIndex >0 BEGIN SET @strResult = LTRIM (RTRIM (@strSource, @dwIndex-1)); IF @strResult is NULL or @strResult = "or @strResult = @strSeparator BEGIN SET @strSeparator = SUBSTRING (@strSource, @dwI Ndex+1,len (@strSource) [email protected]); --the string to be manipulated removes the segmented part SET @dwIndex = CHARINDEX (@strSeparator, @strSource); --cyclic volume increases CONTINUE; END
INSERT @tbResult VALUES (@strResult); SET @strSource = SUBSTRING (@strSource, @dwIndex +1,len (@strSource)-[email protected]); --the string to be manipulated removes the segmented part SET @dwIndex =charindex (@strSeparator, @strSource); --loop increment end-handles the last section IF @strSource is not NULL and LTRIM (RTRIM (@strSource)) <> "and @strSource <> @strSepara Tor BEGIN INSERT @tbResult VALUES (@strSource) END
RETURN; END



SQL Server Split function

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.