Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


replacing numbers with strings using PATINDEX


replacing numbers with strings using PATINDEX

Author
Message
badamaams
badamaams
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 94
Hi

I am migrating data from a legacy system to a new system. I am trying to replace legacy client id with the new client ids.


create table sampleclient
(clientid int,
clientcode varchar(10))

INSERT sampleclient VALUES
(1, 'ABCD'),
(2, 'EFGH'),
(3, '3RETS'),
(4, 'T6UY');



I am trying to create a function where i can pass in a string with the old client id and return the new clientcode.

select dbo.ReplaceClientIDwithClientcode('ActiveMaster.Fields("ClientID").Value = 1')



I am expecting the output to be
ActiveMaster.Fields("ClientID").Value = 'ABCD'


Some of the input strings are bit complex.

ActiveMaster.Fields("ClientID").Value <> 356 and ActiveMaster.Fields("ClientID").Value <> 481 and ActiveMaster.Fields("ClientID").Value <> 17 and ActiveMaster.Fields("ClientID").Value <> 1775


Here is the code i have so far.


DECLARE
@clientid varchar(100),
@clientcode varchar(100),
@string varchar(3000),
@start int,
@end int,
@len int



SET @string = 'ActiveMaster.Fields("ClientID").Value <> 356 and ActiveMaster.Fields("ClientID").Value <> 481 and ActiveMaster.Fields("ClientID").Value <> 17 and ActiveMaster.Fields("ClientID").Value <> 1775'


while PATINDEX('%[0-9]%',@string) > 0
begin

set @len = len(@string)

set @start = PATINDEX('%[0-9]%',@string)
--print 'start of first number ' + cast (@start as varchar(1000))

--print substring(@string, @start, @len)

set @end = PATINDEX('%[^0-9]%',substring(@string, @start, @len))-1

--print @end
if @end = -1
begin

set @clientid = substring(@string, @start, @len)
end
else
begin
set @clientid = substring(@string, @start, @end)
end

print @clientid
set @clientcode = (select clientcode from clients where ClientID = @clientid)
print @clientcode
set @string = REPLACE(@string, @clientid ,''''+ @clientcode + '''')



--set @loopbreaker = @loopbreaker +1
--if @loopbreaker = 10
-- break

end

print @string



The problem i am facing is that the client code which replaces the client id also contains numbers. My loop finds the number again and chops up the client code.

Any help is greatly appreciated.
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