SQL table-valued function-splits an incoming string into a temporary table with a delimiter of 2

Source: Internet
Author: User

Use [TMS]
GO
/****** object:userdefinedfunction [dbo]. [Fn_strtotable_double] Script DATE:2017/4/26 9:07:38 ******/
SET ANSI_NULLS on
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
--AUTHOR:HXQ
--Create date:2016 August 9 18:04:11
--Description: Table-valued Function-splits an incoming string into a temporary table with a delimiter of 2, for example: SELECT * from dbo.fn_strtotable_double (' 111|222,333|444,555|666 ', ', ', ' | ')
-- =============================================
ALTER FUNCTION [dbo]. [Fn_strtotable_double]
(
@String NVARCHAR (max),--the string to convert.
@Separator1 VARCHAR (10) = ', ',--delimiter.
@Separator2 varchar (10) = ' | ' --A second delimiter
)
RETURNS @TableDouble TABLE ([ID] NVARCHAR (+), [Text] NVARCHAR (+), Sort int)
As
BEGIN

SET @String = @String [email protected]
DECLARE @int int=1
While CHARINDEX (@Separator1, @String) > 0
BEGIN
declare @str nvarchar (+) = ", @str1 nvarchar (500) ="
Set @str =substring (@String, 1, CHARINDEX (@Separator1, @String)-1)
SET @str1 =substring (@str, 1, CHARINDEX (@Separator2, @str)-1)
Set @str =substring (@str, CHARINDEX (@Separator2, @str) +1, CHARINDEX (@Separator2, @str)-1)
INSERT into @TableDouble ([Id],[text],sort) VALUES (@str1, @str, @int)
Set @[email protected]+1
SELECT @String = SUBSTRING (@String, CHARINDEX (@Separator1, @String) + 1, LEN (@String)-CHARINDEX (@Separator1, @String))
END
Return
END

SQL table-valued function-splits an incoming string into a temporary table with a delimiter of 2

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.