Вот и пример функции, чтобы сделать это
delimiter //
CREATE DEFINER=`root`@`localhost` FUNCTION `cut`(`instring` varchar(255))
RETURNS varchar(255) CHARSET latin1
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
begin
declare tempstring varchar(100);
declare outstring varchar(100);
declare checkit int;
declare firsthashpos int;
declare tempid int;
set tempstring = ltrim(rtrim(instring));
set checkit = 0;
if instr(tempstring,'#') then set firsthashpos = instr(tempstring,'#'); end if;
looper: while tempstring is not null and instr(tempstring,'#') > 0 do
set outstring = reverse(substring(reverse(tempstring),1,instr(reverse(tempstring),'#')));
set tempstring = replace(tempstring,reverse(substring(reverse(tempstring),1,instr(reverse(tempstring),'#'))),'');
insert into b (address) values (outstring);
end while;
return concat(tempstring);
end//
delimiter ;
дал это
select * from a;
+------+---------------------+
| id | address |
+------+---------------------+
| 1 | 13 #ont 12#45 st |
| 2 | 13 #ont 12345 st |
| 3 | 13 #ont 12#45 45678 |
| 4 | 56789 #ont 12#45 st |
+------+---------------------+
результат от
select id,address,cut(address)
from a
where instr(address,'#') > 0;
Результаты в б
select * from b;
+----+---------------+
| id | address |
+----+---------------+
| 1 | #45 st |
| 2 | #ont 12 |
| 3 | #ont 12345 st |
| 4 | #45 45678 |
| 5 | #ont 12 |
| 6 | #45 st |
| 7 | #ont 12 |
+----+---------------+
Возвращаемое значение функции (outstring) содержит то, что осталось после бит после удаления #s. Надеемся, что манипуляция строк в функции не требует пояснений.
Вы можете использовать строковые функции, но логика будет несколько сложной. –