hello there,
in a table b25 the column "name" includes strings
in a table b26 the column "char_omitted" includes characters that have to be omitted from the strings of table b25
the table b26 is dynamic and can be changed by user from time to time
see below table b25
NAME
LORENZO BROWN
LORENZOA BROWN
LORENZOC BROWN
LORENZOD BROWN
LORENZOF BROWN
see below table b26
CHAR_OMITTED
A
C
D
F
this is the expected result:
NAME | CORRECT_NAME
LORENZO BROWN | LORENZO BROWN
LORENZOA BROWN | LORENZO BROWN
LORENZOC BROWN | LORENZO BROWN
LORENZOD BROWN | LORENZO BROWN
LORENZOF BROWN | LORENZO BROWN
below is tne tables script
create table b25 (name nvarchar(50))
insert into b25 select 'LORENZO BROWN'
insert into b25 select 'LORENZOA BROWN'
insert into b25 select 'LORENZOC BROWN'
insert into b25 select 'LORENZOD BROWN'
insert into b25 select 'LORENZOF BROWN'
create table b26 (char_omitted nvarchar(50))
insert into b26 select 'A'
insert into b26 select 'C'
insert into b26 select 'D'
insert into b26 select 'F'
; with cte as
( select string_agg(char_omitted,'') as ok from b26 )
select
name
, REPLACE(TRANSLATE(name, ok, '@@@@'), '@', '') as correct_name
from
cte , b25
Thank you for your reply however the tables should be updated without having to change the code. When I added a record to table b26 I had to change the function. Instead of @@@@, we have to write @@@@@
Thank you for your reply however the tables should be updated with out having to change the code. When I added a record to table b26 I got 9 records instead of 5.
thank you for your reply, however it seems to me that it works if the values in table b26 includes one character. in practise these values can include more than one character.
DECLARE @name NVARCHAR(50), @char_omitted NVARCHAR(50) , @output NVARCHAR(50)
DECLARE vendor_cursor CURSOR FOR select * from b25
OPEN vendor_cursor
FETCH NEXT FROM vendor_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE product_cursor CURSOR FOR select * from b26
OPEN product_cursor
FETCH NEXT FROM product_cursor INTO @char_omitted
set @output = @name
WHILE @@FETCH_STATUS = 0
BEGIN
set @output = replace(@output,@char_omitted,'')
FETCH NEXT FROM product_cursor INTO @char_omitted
END
CLOSE product_cursor
DEALLOCATE product_cursor
select @output
FETCH NEXT FROM vendor_cursor INTO @name
END
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;
I apologize for not being clear enough .Below is theexpected result
NAME | CORRECT_NAME
LORENZO BROWN | RENZO BROWN
LORENZOA BROWN | RENZO BROWN
LORENZOC BROWN | RENZO BROWN
LORENZOD BROWN | RENZO BROWN
LORENZOF BROWN | RENZO BROWN
DECLARE vendor_cursor CURSOR FOR select * from b25
OPEN vendor_cursor
FETCH NEXT FROM vendor_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE product_cursor CURSOR FOR select * from b26
OPEN product_cursor
FETCH NEXT FROM product_cursor INTO @char_omitted
set @output = @name
WHILE @@FETCH_STATUS = 0
BEGIN
set @output = replace(@output,@char_omitted,'')
FETCH NEXT FROM product_cursor INTO @char_omitted
END
CLOSE product_cursor
DEALLOCATE product_cursor
insert into #Output select @name,@output
FETCH NEXT FROM vendor_cursor INTO @name
END
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;
If we wanted to do everything in memory, we could eliminate the cursors and use tables B25 and B26 if only they were equipped with an ID column of type IDENTITY(1,1).
Otherwise, you could use table-type variables as in the example below.
DECLARE @B25Array TABLE (
IDX INTEGER IDENTITY(1,1)
, NAME VARCHAR(50)
)
INSERT INTO @B25Array(NAME) SELECT NAME FROM b25
--
DECLARE @B26Array TABLE (
IDX INTEGER IDENTITY(1,1)
, CHAR_OMITTED VARCHAR(50)
)
INSERT INTO @B26Array(CHAR_OMITTED) SELECT char_omitted FROM b26
--
DECLARE @CORRECT_NAMES_TABLE AS TABLE
(
NAME VARCHAR(50)
, CORRECT_NAME VARCHAR(50)
)
--
DECLARE @NAME VARCHAR(50)
DECLARE @CORRECT_NAME VARCHAR(50) = ''
DECLARE @CHAR_OMITTED VARCHAR(50)
DECLARE @I INT
DECLARE @J INT
DECLARE @B25Max INT = (SELECT MAX(IDX) FROM @B25Array)
DECLARE @B26Max INT = (SELECT MAX(IDX) FROM @B26Array)
--
SET @I = 1
WHILE @I <= @B25Max
BEGIN
SET @NAME = (SELECT NAME FROM @B25Array WHERE IDX = @I)
SET @CORRECT_NAME = @NAME
SET @J = 1
WHILE @J <= @B26Max
BEGIN
SET @CHAR_OMITTED = (SELECT CHAR_OMITTED FROM @B26Array WHERE IDX = @J)
SET @CORRECT_NAME = REPLACE(@CORRECT_NAME, @CHAR_OMITTED, '')
SET @J = @J + 1
END
INSERT INTO @CORRECT_NAMES_TABLE(NAME, CORRECT_NAME) VALUES (@NAME, @CORRECT_NAME)
SET @I = @I + 1
END
SELECT * FROM @CORRECT_NAMES_TABLE