Well, this question includes one part of a complex stored procedure that inserts new entities into multiple tables.
The part that I'm having difficulties with right now needs to work like this:
- add object with original name
- check if the name of the new object contains any special characters listed in Table A “Symbols”
- if so, replace this character with the “replacement character” from table A
EDIT: I got it partially, but still not finished. I still have a problem showing each character replacement combination. Also, in the case of replacing a character that occurs more than once, for example, ".", Substitutions must be performed independently of each other.
ex: # www.test & aol.com → # wwwtest & aol.com, # www.test & aolcom
This is a rough start, I know that parts of this will not work, but I thought it was a worthy starting point:
declare @test varchar(50)
set @test = '#www.test&aol.com'
declare @len int, @ctr int
set @len = LEN(@test)
set @ctr = 1
declare @newName varchar(50)
declare @matchedChar table(match varchar(10),replaceChar varchar(10),processed int default(0))
declare @alternateEntities table(name varchar(50))
declare @repChar varchar(10)
declare @selectedChar varchar(1)
while @ctr<=@len
begin
insert into @matchedChar (match,replaceChar) select Character,ReplacementCharacter from tblTransliterations where Character = SUBSTRING(@test,@ctr,1)
while (select COUNT(*) from @matchedChar where processed = 0)>0
begin
set @selectedChar = (select top 1 match from @matchedChar where processed = 0)
set @repChar = (select top 1 replaceChar from @matchedChar where processed = 0)
set @newName = (select STUFF(@test,CHARINDEX(@selectedChar,@test),1,@repChar))
update @matchedChar set processed = 1 where @repChar = replaceChar
insert into @alternateEntities (name) values (@newName)
end
set @ctr = @ctr+1
set @len = LEN(@test)
end
select * from @alternateEntities
source
share