function customization for string interception

Source: Internet
Author: User
Tags rtrim

 SETAnsi_nulls onGOSETQuoted_identifier onGO/**************** Split string ****************/CREATE function [dbo].[splitstring](@Input nvarchar(Max),@Separator nvarchar(Max)=',', @RemoveEmptyEntries bit=1 )returns @TABLE Table ([Id] int Identity(1,1),[Value] nvarchar(Max))  asbegin Declare @Index int,@Entry nvarchar(Max)Set @Index = charindex(@Separator,@Input) while(@Index>0)beginSet @Entry=LTrim(RTrim(substring(@Input,1,@Index-1)))if(@RemoveEmptyEntries=0)or(@RemoveEmptyEntries=1  and @Entry<>"')beginInsert  into @TABLE([Value])Values(@Entry)EndSet @Input = substring(@Input,@Index+datalength(@Separator)/2,Len(@Input))Set @Index = charindex(@Separator,@Input)EndSet @Entry=LTrim(RTrim(@Input))if(@RemoveEmptyEntries=0)or(@RemoveEmptyEntries=1  and @Entry<>"')beginInsert  into @TABLE([Value])Values(@Entry)EndreturnEND



Calling the created SQL function
Declare @s varchar( -),@sql varchar( +),@split VARCHAR(Ten),@index INTSET @split='|';SET @index=1;SET @s='|21|2106|';Select COUNT(*) from [dbo].[splitstring](@s,@split,1)Select *  from [dbo].[splitstring](@s,@split,1)

Results

function customization for string interception

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.