SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Script to replace 5 consecutive digits with special character


Script to replace 5 consecutive digits with special character

Author
Message
tanmayi
tanmayi
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1661 Visits: 150
Comments posted to this topic are about the item Script to replace 5 consecutive digits with special character
Carlo Romagnano
Carlo Romagnano
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7481 Visits: 3395
Your script replace 5 consecutive digits with 2 stars.
Here an optimized version.

declare @substr varchar(50)      --substring of input string in while loop
declare @index int --index of number in the substring

set @substr = '1234567890abc89900123456abcde'
while 1=1
begin
select @index = PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%',@substr)
if(@index < 1)
break
SELECT @substr = STUFF ( @substr, @index, 5 ,'**' )
end
print @substr



I run on tuttopodismo
James Marsh
James Marsh
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 348
I love how you shortened this script Carlo. I took your script and added a few more variables to make it more flexible. Simply update the number being passed to @len to change your pattern and your replacement. I also felt if you are going to replace 5 chars you should substitute 5 chars. However I can see scenario's why you would want that to be the same across the board.


DECLARE @substr varchar(MAX) --substring of input string in while loop
DECLARE @index INT --index of number in the substring
DECLARE @len INT --number of numbers being searched in the substring
DECLARE @patindex VARCHAR(256) --patern being searched in the substring


SET @len = 5
SET @patindex = '%' + REPLICATE('[0-9]',@len) + '%'

set @substr = '1234567890abc89900123456abcde'
while 1=1
begin
select @index = PATINDEX(@patindex,@substr)
if(@index < 1)
break
SELECT @substr = STUFF ( @substr, @index, @len ,REPLICATE('*', @len))
end
print @substr


Iwas Bornready
Iwas Bornready
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22330 Visits: 885
Thanks for the script.
Iwas Bornready
Iwas Bornready
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22330 Visits: 885
Carlo Romagnano (1/10/2013)
Your script replace 5 consecutive digits with 2 stars.
Here an optimized version.

declare @substr varchar(50)      --substring of input string in while loop
declare @index int --index of number in the substring

set @substr = '1234567890abc89900123456abcde'
while 1=1
begin
select @index = PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%',@substr)
if(@index < 1)
break
SELECT @substr = STUFF ( @substr, @index, 5 ,'**' )
end
print @substr

Thanks for the update.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search