How to use circular substitution on SQL SERVER

Source: Internet
Author: User

--select Right (@strReplace,-2)

/*
Loop replaces a string
Set @strReplace = ' City; district; company ' characters to be replaced, separated by semicolons
Set @strTest = ' xiamen Simin District XXX Information Technology Co., Ltd ' the original string to be replaced
*/
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].getarraryreplacestr ') and xtype= ' FN ')
Drop function Getarraryreplacestr
Go
Create function Getarraryreplacestr (@strReplace varchar (1000), @strTest varchar (1000))
Returns varchar (100)
As
Begin

declare @start int, @end int, @len int, @strTmp varchar (1000)
Set @start =0
Set @len =len (@strReplace)
while (@len >0)
Begin
--select @start
Select @end =charindex ('; ', @strReplace)
--select @end
If @start = @end
Begin
Set @len =0
Select @strTest =replace (@strTest, @strReplace, ")
End
Else
Begin
Set @len = @end-@start
--select @len
Select @strTmp =substring (@strReplace, @start, @len)
Select @strReplace =replace (@strReplace, @strTmp + '; ', ')
--select @strReplace
Select @strTest =replace (@strTest, @strTmp, ")
--set @start = @end
End
End
Return @strTest
End
Go
DECLARE @strReplace varchar (1000), @strTest varchar (1000), @strTmp varchar (1000)
Set @strReplace = ' City; district; company '
Set @strTest = ' xiamen Simin District XXX Information Technology Co., Ltd. '

Select Dbo.getarraryreplacestr (@strReplace, @strTest)

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.