{"id":19081,"date":"2021-07-26T14:24:54","date_gmt":"2021-07-26T04:24:54","guid":{"rendered":"https:\/\/database.guide\/?p=19081"},"modified":"2021-07-27T08:47:32","modified_gmt":"2021-07-26T22:47:32","slug":"replace-function-in-oracle","status":"publish","type":"post","link":"https:\/\/database.guide\/replace-function-in-oracle\/","title":{"rendered":"REPLACE() Function in Oracle"},"content":{"rendered":"\n<p>In Oracle, the <code>REPLACE()<\/code> function allows you to replace a part of a string with another string.<\/p>\n\n\n\n<p>The function accepts three arguments: the string, the substring to replace, and the replacement string (to replace the substring with). <\/p>\n\n\n\n<p>You can omit the replacement string, in which case, the function will remove all instances of the substring.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>REPLACE(char, search_string\n        &#91;, replacement_string ]\n       )<\/code><\/pre>\n\n\n\n<p>Where <code>char<\/code> is the string, <code>search_string<\/code> is the substring to replace, and <code>replacement_string<\/code> is the string to replace that substring with.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s a basic example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><meta charset=\"utf-8\">SELECT \n    REPLACE('Barrier Island', 'Island', 'Reef')\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Barrier Reef<\/pre>\n\n\n\n<p>In this case we replaced the substring <code>Island<\/code> with <code>Reef<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Multiple Matches<\/h2>\n\n\n\n<p>If the string to be replaced occurs multiple times within the string, all occurrences are replaced:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    REPLACE('Black dogs and white dogs', 'dog', 'cat')\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Black cats and white cats<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">No Match<\/h2>\n\n\n\n<p>If the substring doesn&#8217;t occur in the string, <code>REPLACE()<\/code> returns the string unchanged:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    REPLACE('Barrier Island', 'to entry', 'Reef')\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Barrier Island<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Remove a Substring<\/h2>\n\n\n\n<p>Omitting the third argument removes the substring from the string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    REPLACE('Black dogs and white dogs', 'dogs')\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Black  and white <\/pre>\n\n\n\n<p>If you look closely, you&#8217;ll notice that the spaces still remain, as we didn&#8217;t specify any spaces in the substring.<\/p>\n\n\n\n<p>Here it is again with the space included:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    REPLACE('Black dogs and white dogs', ' dogs')\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Black and white<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Case Sensitivity<\/h2>\n\n\n\n<p>The <code>REPLACE()<\/code> function performs a case-sensitive match:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    REPLACE('Barrier Island', 'island', 'reef')\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Barrier Island<\/pre>\n\n\n\n<p>In this example, the case didn&#8217;t match, and so the original string was returned unchanged.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Empty Strings<\/h2>\n\n\n\n<p>Here&#8217;s what happens when an empty string is passed for each given argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    REPLACE('', 'dog', 'cat') AS \"1\",\n    REPLACE('Black dog', '', 'cat') AS \"2\",\n    REPLACE('Black dog', 'dog', '') AS \"3\"\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   1            2         3 \n____ ____________ _________ \n     Black dog    Black    <\/pre>\n\n\n\n<p>So in this case:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Passing an empty string for the initial string returns an empty string. <\/li><li>Passing an empty string for the second argument returns the original string.<\/li><li>Passing an empty string for the third argument removes the string to be replaced from the string.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Space Character<\/h2>\n\n\n\n<p>An empty string is not the same as the space character. <\/p>\n\n\n\n<p>Here&#8217;s what happens when we change the empty string to a space:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    REPLACE(' ', 'dog', 'cat') AS \"1\",\n    REPLACE('Black dog', ' ', 'cat') AS \"2\",\n    REPLACE('Black dog', 'dog', ' ') AS \"3\"\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   1              2          3 \n____ ______________ __________ \n     Blackcatdog    Black     <\/pre>\n\n\n\n<p>Therefore, if the string is nothing but a space, then we can replace that with another string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT REPLACE(' ', ' ', 'cat')\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cat<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Null Arguments<\/h2>\n\n\n\n<p>Passing <code>null<\/code> for each argument works like passing an empty string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET NULL 'null';\nSELECT \n    REPLACE(null, 'dog', 'cat') AS \"1\",\n    REPLACE('Black dog', null, 'cat') AS \"2\",\n    REPLACE('Black dog', 'dog', null) AS \"3\"\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      1            2         3 \n_______ ____________ _________ \nnull    Black dog    Black    <\/pre>\n\n\n\n<p>By default, SQLcl and SQL*Plus return a blank space whenever <code>null<\/code> occurs as a result of a SQL <code><a href=\"https:\/\/database.guide\/sql-select-for-beginners\/\" data-type=\"post\" data-id=\"11928\">SELECT<\/a><\/code> statement. <\/p>\n\n\n\n<p>However, you can use <code><a href=\"https:\/\/database.guide\/set-null-specify-a-string-to-return-whenever-a-null-value-occurs-in-sqlcl-sqlplus\/\">SET NULL<\/a><\/code> to specify a different string to be returned. Here I specified that the string <code>null<\/code> should be returned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Missing Arguments<\/h2>\n\n\n\n<p>Calling <code>REPLACE()<\/code> without passing any arguments results in an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT REPLACE()\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Error starting at line : 1 in command -\nSELECT REPLACE()\nFROM DUAL\nError at Command Line : 1 Column : 8\nError report -\nSQL Error: ORA-00938: not enough arguments for function\n00938. 00000 -  \"not enough arguments for function\"\n*Cause:    \n*Action:<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Too Many Arguments<\/h2>\n\n\n\n<p>And passing too many arguments returns an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT REPLACE('Cat', 'a', 'b', 'c')\nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Error starting at line : 1 in command -\nSELECT REPLACE('Cat', 'a', 'b', 'c')\nFROM DUAL\nError at Command Line : 1 Column : 8\nError report -\nSQL Error: ORA-00939: too many arguments for function\n00939. 00000 -  \"too many arguments for function\"\n*Cause:    \n*Action:<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Oracle, the REPLACE() function allows you to replace a part of a string with another string. The function accepts three arguments: the string, the substring to replace, and the replacement string (to replace the substring with). You can omit the replacement string, in which case, the function will remove all instances of the substring.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[93,67,20],"class_list":["post-19081","post","type-post","status-publish","format-standard","hentry","category-oracle","tag-functions","tag-string-functions","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/19081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=19081"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/19081\/revisions"}],"predecessor-version":[{"id":19101,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/19081\/revisions\/19101"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=19081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=19081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=19081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}