|
|
|
|
|
Re: stuff() equialent function in oracle [message #231241 is a reply to message #231237] |
Mon, 16 April 2007 01:47 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
Like I expected, SUBSTR will do. I had a quick go at it:
My test script:
CREATE OR REPLACE FUNCTION stuff (
char_expr1 IN VARCHAR2
, startpos IN NUMBER
, strlen IN NUMBER
, char_expr2 IN VARCHAR2
)
RETURN VARCHAR2 DETERMINISTIC
AS
v_return VARCHAR2 (4000);
BEGIN
IF LEAST (NVL (startpos, 0), NVL (strlen, 0)) >= 0
THEN
v_return :=
SUBSTR (char_expr1, 1, startpos - 1)
|| char_expr2
|| SUBSTR (char_expr1, startpos + strlen, LENGTH (char_expr1));
END IF;
RETURN v_return;
END stuff;
/
sho err
col mystuff format A25
SELECT stuff('HelloWorld',6,0,' ') mystuff
, 'Hello World' expected
FROM dual
/
SELECT stuff('HelloWorld',6,4,NULL) mystuff
, 'Hellod' expected
FROM dual
/
SELECT stuff('abcdef', 2, 3, 'ijklmn') mystuff
, 'aijklmnef' expected
FROM dual
/
DROP FUNCTION stuff
/
My run:
SQL> @orafaq
Function created.
No errors.
MYSTUFF EXPECTED
------------------------- -----------
Hello World Hello World
MYSTUFF EXPECT
------------------------- ------
Hellod Hellod
MYSTUFF EXPECTED
------------------------- ---------
aijklmnef aijklmnef
Function dropped.
SQL>
MHE
|
|
|
|