{"id":519,"date":"2011-06-08T09:19:51","date_gmt":"2011-06-08T09:19:51","guid":{"rendered":"http:\/\/bar-solutions.com\/weblog\/?p=519"},"modified":"2011-06-08T09:19:51","modified_gmt":"2011-06-08T08:19:51","slug":"","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=519","title":{"rendered":"Replace by position"},"content":{"rendered":"<p align=\"left\">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.<\/p>\n<p><!--more--><\/p>\n<p align=\"left\">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.<\/p>\n<p align=\"left\"><font size=\"2\" face=\"Courier New\"><font color=\"#008000\">CREATE OR REPLACE FUNCTION<\/font> <font color=\"#004080\">replacepos<br \/>\n      <br \/>&#160; <\/font><\/font><font size=\"2\" face=\"Courier New\"><font color=\"#004080\">( source_in&#160;&#160;&#160;&#160;&#160; <\/font><font color=\"#008000\">IN VARCHAR2<br \/>\n      <br \/><\/font>&#160; <font color=\"#004080\">, replacechar_in<\/font> <font color=\"#008000\">IN VARCHAR2<br \/>\n      <br \/><\/font>&#160; <font color=\"#004080\">, position_in<\/font>&#160;&#160;&#160; <font color=\"#008000\">IN NUMBER) RETURN VARCHAR2 IS<br \/>\n      <br \/><\/font>&#160; l_<font color=\"#004080\">returnvalue<\/font> <font color=\"#008000\">VARCHAR2<\/font><font color=\"#004080\">(32767);<br \/>\n      <br \/><\/font><font color=\"#008000\">BEGIN<br \/>\n      <br \/><\/font><font color=\"#ff0000\">&#160; &#8212; copy from the source string up to, but not including,<br \/>\n      <br \/>&#160; &#8212; the character position<br \/>\n      <br \/>&#160; &#8212; to be replaced<br \/>\n      <br \/><\/font>&#160; <font color=\"#004080\">l_returnvalue := substr( str1 =&gt; source_in<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , pos =&gt; 1<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , len =&gt; position_in &#8211; 1);<br \/>\n      <br \/><\/font><font color=\"#ff0000\">&#160; &#8212; add the replacement character<br \/>\n      <br \/>&#160; &#8212; just a single character, but more can be sent in,<br \/>\n      <br \/>&#160; &#8212; so substring the parameter<br \/>\n      <br \/><\/font>&#160; <font color=\"#004080\">l_returnvalue := l_returnvalue ||<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; substr( str1 =&gt; replacechar_in<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , pos =&gt; <\/font><\/font><font size=\"2\" face=\"Courier New\"><font color=\"#004080\">1<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , len =&gt; 1);<br \/>\n      <br \/><\/font><font color=\"#ff0000\">&#160; &#8212; copy the rest of the source string<br \/>\n      <br \/><\/font>&#160; <font color=\"#004080\">l_returnvalue := l_returnvalue ||<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; substr( str1 =&gt; source_in<br \/>\n      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , pos =&gt; position_in + 1);<br \/>\n      <br \/><\/font>&#160; <font color=\"#008000\">RETURN<\/font> <font color=\"#004080\">l_returnvalue;<br \/>\n      <br \/><\/font><font color=\"#008000\">END<\/font> <font color=\"#004080\">replacepos;<\/font><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5],"tags":[],"class_list":["post-519","post","type-post","status-publish","format-standard","hentry","category-oracle","category-plsql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=519"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/519\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}