Вы можете достичь этого, используя две функции Replacepos
для замены строки в позиции и replacepos1
, чтобы указать no_of_chars для замены. После этого вы можете просто использовать следующие SQL
WITH data
AS (SELECT ROWNUM rw,
Lpad('404040', 54, '404040') A
FROM dual),
d1
AS (SELECT rw,
Decode(Instr(a, '404040', 1), 1, Replacepos1(a, '0', 1, 6)) A
FROM data),
d2
AS (SELECT rw,
Decode(Instr(a, '404040', 1), 7, Replacepos1(a, '0', 7, 6)) A
FROM d1),
d3
AS (SELECT rw,
Decode(Instr(a, '404040', 1), 13, Replacepos1(a, '0', 13, 6))A
FROM d2),
d4
AS (SELECT rw,
Decode(Instr(a, '404040', 1), 19, Replacepos1(a, '0', 19, 6))A
FROM d3),
d5
AS (SELECT rw,
Decode(Instr(a, '404040', 1), 25, Replacepos1(a, '0', 25, 6))A
FROM d4),
d6
AS (SELECT rw,
Decode(Instr(a, '404040', 1), 31, Replacepos1(a, '0', 31, 6))A
FROM d5),
d7
AS (SELECT rw,
Decode(Instr(a, '404040', 1), 37, Replacepos1(a, '0', 37, 6))A
FROM d6),
d8
AS (SELECT rw,
Decode(Instr(a, '404040', 1), 43, Replacepos1(a, '0', 43, 6))A
FROM d7),
d9
AS (SELECT rw,
Decode(Instr(a, '404040', 1), 49, Replacepos1(a, '0', 49, 6))A
FROM d8)
SELECT A
FROM d9;
Технические характеристики функций ниже
CREATE OR replace FUNCTION Replacepos(source_in IN VARCHAR2,
replacechar_in IN VARCHAR2,
position_start IN NUMBER)
RETURN VARCHAR2
IS
l_returnvalue VARCHAR2(32767);
position_in NUMBER;
BEGIN
-- copy from the source string up to, but not including,
-- the character position
position_in := position_start;
-- to be replaced
l_returnvalue := Substr(str1 => source_in, pos => 1, len => position_in - 1)
;
-- add the replacement character
-- just a single character, but more can be sent in,
-- so substring the parameter
l_returnvalue := l_returnvalue
|| Substr(str1 => replacechar_in, pos => 1, len => 1);
-- copy the rest of the source string
l_returnvalue := l_returnvalue
|| Substr(str1 => source_in, pos => position_in + 1);
RETURN l_returnvalue;
END replacepos;
И нижеуказанная функция
CREATE OR replace FUNCTION Replacepos1 (source_in IN VARCHAR2,
replacechar_in IN VARCHAR2,
position_start IN NUMBER,
no_of_chars IN NUMBER)
RETURN VARCHAR2
IS
l_returnvalue VARCHAR2(32767);
position_in NUMBER;
BEGIN
l_returnvalue := source_in;
FOR i IN 1..no_of_chars LOOP
l_returnvalue := Replacepos(l_returnvalue, replacechar_in,
position_start + i - 1);
END LOOP;