Replace by position
Have you ever wanted to replace a character in a string just by position? I needed this the other day and IMHO there is no way in Oracle PL/SQL to do this, so I decided to write this my self.
It is really simple code and it probably needs improvement but it works for now and I think it is rather simple. Any improvement suggestions are of course very welcome in the comments.
CREATE OR REPLACE FUNCTION replacepos
( source_in IN VARCHAR2
, replacechar_in IN VARCHAR2
, position_in IN NUMBER) RETURN VARCHAR2 IS
l_returnvalue VARCHAR2(32767);
BEGIN
— copy from the source string up to, but not including,
— the character position
— 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;
Related posts:
- Oracle RDBMS 11gR2 – alter or replace user defined types even when there are dependencies
- Solving a Sudoku with 1 SQL-statement: the Model-clause
- Construct a "Save" URL syntax for the Oracle Reports Server
- Rounding numbers given in string representation while controlling their number of significant digits
- Copy Triggers when you Copy a Table
This entry was posted by Patrick Barel on June 8, 2011 at 11:21 am, and is filed under Database, Devel. + PL/SQL tools. Follow any responses to this post through RSS 2.0.You can skip to the end and leave a response. Pinging is currently not allowed.
-
Leave a Reply Cancel reply
- Comment Feed for this Post
Didn't find any related posts :(
Like I already replied to on Laurent Schneiders comment on the crossposting on http://blog.bar-solutions.com:
Which is ofcourse a valid way to do it. I definitely need to learn more about these regular expressions.