35 lines
959 B
SQL
35 lines
959 B
SQL
CREATE DEFINER=`root`@`%` FUNCTION `changestr`(duty_territory VARCHAR(500), location int, str VARCHAR(500)) RETURNS varchar(500) CHARSET utf8mb4
|
|
COMMENT '替换指定字符串'
|
|
begin
|
|
declare result varchar(500);
|
|
declare str1 varchar(500);
|
|
declare str2 varchar(500);
|
|
declare num int;
|
|
declare num1 int;
|
|
declare num2 int;
|
|
|
|
set num = LENGTH(duty_territory) - LENGTH( REPLACE (duty_territory, ',', ''));
|
|
set num1 = location-1;
|
|
set num2 = 0-(num-location+1);
|
|
|
|
set str1 = substring_index(duty_territory,',',num1);
|
|
set str2 = substring_index(duty_territory,',',num2);
|
|
|
|
if LENGTH(trim(str1))=0 && LENGTH(trim(str2))=0 then
|
|
set result = str;
|
|
|
|
elseif LENGTH(trim(str1))=0 then
|
|
set result = CONCAT(str,',',str2);
|
|
|
|
elseif LENGTH(trim(str2))=0 then
|
|
set result = CONCAT(str1,',',str);
|
|
|
|
else
|
|
set result = CONCAT(str1,',',str,',',str2);
|
|
end if;
|
|
|
|
|
|
|
|
return result;
|
|
end
|