{"id":1925,"date":"2018-05-12T22:21:12","date_gmt":"2018-05-13T05:21:12","guid":{"rendered":"https:\/\/database.guide\/?p=1925"},"modified":"2019-11-19T22:58:48","modified_gmt":"2019-11-20T05:58:48","slug":"sql-server-replace-vs-translate-what-are-the-differences","status":"publish","type":"post","link":"https:\/\/database.guide\/sql-server-replace-vs-translate-what-are-the-differences\/","title":{"rendered":"SQL Server REPLACE() vs TRANSLATE(): What are the Differences?"},"content":{"rendered":"<p>Starting with SQL Server 2017, you can now use the <a href=\"https:\/\/database.guide\/what-is-t-sql\/\">T-SQL<\/a> <code>TRANSLATE()<\/code> function to translate one or more characters into another set of characters.<\/p>\n<p>At first glance, you might think that the\u00a0<code>TRANSLATE()<\/code>\u00a0function does exactly the same thing as the\u00a0<code>REPLACE()<\/code>\u00a0function, but there are significant differences between the two.<\/p>\n<p><!--more--><\/p>\n<h2>Definitions<\/h2>\n<p>First, let&#8217;s look at the definition of each function:<\/p>\n<dl>\n<dt><a href=\"https:\/\/database.guide\/how-to-replace-all-occurrences-of-a-string-with-another-string-in-sql-server-replace\/\"><code>REPLACE()<\/code><\/a><\/dt>\n<dd>Replaces all occurrences of a specified string value with another string value.<\/dd>\n<dt><a href=\"https:\/\/database.guide\/how-the-translate-function-works-in-sql-server-t-sql\/\"><code>TRANSLATE()<\/code><\/a><\/dt>\n<dd>Returns the string provided as a first argument after some characters specified in the second argument are translated into a destination set of characters.<\/dd>\n<\/dl>\n<p>The main difference is how each function deals with multiple characters. <code>REPLACE()<\/code>\u00a0replaces one string with another string. Therefore, if a string contains multiple characters, each character must be in the same order. <code>TRANSLATE()<\/code>\u00a0on the other hand, replaces each character one by one, regardless of the order of those characters.<\/p>\n<h2>Example &#8211; Same result<\/h2>\n<p>There are some cases where both functions will return the same result. Like this:<\/p>\n<pre>SELECT \r\n    REPLACE('123', '123', '456') AS Replace,\r\n    TRANSLATE('123', '123', '456') AS Translate;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>Replace  Translate\r\n-------  ---------\r\n456      456      \r\n<\/pre>\n<p>In this case, <code>REPLACE()<\/code> returns <code>456<\/code> because the exact string in the second argument matched a string in first argument (in this case, it was the whole string).<\/p>\n<p><code>TRANSLATE()<\/code> returns <code>456<\/code> because each character in the second argument is present in the first argument.<\/p>\n<h2>Example &#8211; Different Result<\/h2>\n<p>Now for an example that demonstrates one of the differences between\u00a0<code>TRANSLATE()<\/code> and\u00a0<code>REPLACE()<\/code>:<\/p>\n<pre>SELECT \r\n    REPLACE('123', '321', '456') AS Replace,\r\n    TRANSLATE('123', '321', '456') AS Translate;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>Replace  Translate\r\n-------  ---------\r\n123      654      \r\n<\/pre>\n<p>In this case, <code>REPLACE()<\/code> has no effect (it returns the original string) because the second argument is not an exact match for the first argument (or a substring within it). Even though the second argument contains the correct characters, they are not in the same order as the first argument, and therefore, the whole string doesn&#8217;t match.<\/p>\n<p><code>TRANSLATE()<\/code> <em>does<\/em> have an effect because each character in the second argument is present in the first argument. It doesn&#8217;t matter that they&#8217;re in a different order, because each character is translated one by one. SQL Server translates the first character, then the second, then the third, and so on.<\/p>\n<h2>Non-Contiguous Strings<\/h2>\n<p>Similar to the previous example, you can also get different results when the first argument contains the characters in the second argument, but they are non-contiguous:<\/p>\n<pre>SELECT \r\n    REPLACE('1car23', '123', '456') AS Replace,\r\n    TRANSLATE('1car23', '123', '456') AS Translate;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>Replace  Translate\r\n-------  ---------\r\n1car23   4car56   \r\n<\/pre>\n<h2>Arguments of a Different Length<\/h2>\n<p>You may also get different results between each function whenever there are discrepancies in the number of characters in the various arguments.<\/p>\n<p>Here&#8217;s an example where the first argument contains less characters than the second and third arguments:<\/p>\n<pre>SELECT \r\n    REPLACE('123', '1234', '4567') AS Replace,\r\n    TRANSLATE('123', '1234', '4567') AS Translate;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>Replace  Translate\r\n-------  ---------\r\n123      456      \r\n<\/pre>\n<p>In this case, <code>REPLACE()<\/code> has no effect because the second argument contains more characters than the first argument. Therefore, it&#8217;s impossible for the first argument to contain the second argument.<\/p>\n<p>The <code>TRANSLATE()<\/code> function however, does have an effect in this case. This is because the second argument contains characters that are in the first argument. It doesn&#8217;t matter that the second argument contains more characters than the first. The most important thing is that the third argument contains the same number of characters as the second.<\/p>\n<p>There are also cases when <code>REPLACE()<\/code> works perfectly but <code>TRANSLATE()<\/code> throws an error.<\/p>\n<p>Example:<\/p>\n<pre>SELECT REPLACE('1234', '123', '4567') AS Replace;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>Replace\r\n-------\r\n45674  \r\n<\/pre>\n<p>In this case, <code>REPLACE()<\/code> works as expected.<\/p>\n<p>However, if we provide the same arguments to <code>TRANSLATE()<\/code>, we get an error:<\/p>\n<pre>SELECT TRANSLATE('1234', '123', '4567') AS Translate;\r\n<\/pre>\n<p>Result:<\/p>\n<pre>Error: The second and third arguments of the TRANSLATE built-in function must contain an equal number of characters. \r\n<\/pre>\n<p>As the error message states, the second and third arguments must contain an equal number of characters.<\/p>\n<h2>When Should I Use REPLACE()?<\/h2>\n<p>You should use <code>REPLACE()<\/code> when you need to replace all occurrences of a specific string, exactly as it is written. For example, changing someone&#8217;s name to another name.<\/p>\n<p>Using <code>TRANSLATE()<\/code> in such cases can have disastrous results:<\/p>\n<pre>SELECT \r\n    REPLACE('Homer Simpson', 'Homer', 'Jason') AS Replace,\r\n    TRANSLATE('Homer Simpson', 'Homer', 'Jason') AS Translate;<\/pre>\n<p>Result:<\/p>\n<pre>Replace        Translate    \r\n-------------  -------------\r\nJason Simpson  Jason Sispsan\r\n<\/pre>\n<h2>When Should I Use TRANSLATE()?<\/h2>\n<p>As demonstrated\u00a0 in the previous example, the <code>TRANSLATE()<\/code> function can be useful if you need to replace all occurrences of each character specified, regardless of their order within the original string.<\/p>\n<p>It can also be used in place of <code>REPLACE()<\/code> to simply the code. Here&#8217;s an example (based on an <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/functions\/translate-transact-sql?view=sql-server-2017\" target=\"_blank\" rel=\"noopener noreferrer\">example<\/a> on the Microsoft website):<\/p>\n<pre>SELECT \r\n    REPLACE(REPLACE(REPLACE(REPLACE('2*[3+4]\/{7-2}','[','('), ']', ')'), '{', '('), '}', ')') AS Replace,\r\n    TRANSLATE('2*[3+4]\/{7-2}', '[]{}', '()()') AS Translate;<\/pre>\n<p>Result:<\/p>\n<pre>Replace        Translate    \r\n-------------  -------------\r\n2*(3+4)\/(7-2)  2*(3+4)\/(7-2)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Starting with SQL Server 2017, you can now use the T-SQL TRANSLATE() function to translate one or more characters into another set of characters. At first glance, you might think that the\u00a0TRANSLATE()\u00a0function does exactly the same thing as the\u00a0REPLACE()\u00a0function, but there are significant differences between the two.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,5],"tags":[93,77,67,61],"class_list":["post-1925","post","type-post","status-publish","format-standard","hentry","category-sql","category-sql-server","tag-functions","tag-mssql","tag-string-functions","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/1925","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=1925"}],"version-history":[{"count":16,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/1925\/revisions"}],"predecessor-version":[{"id":5390,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/1925\/revisions\/5390"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=1925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=1925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=1925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}