{"id":4071,"date":"2019-05-19T20:19:39","date_gmt":"2019-05-20T03:19:39","guid":{"rendered":"https:\/\/database.guide\/?p=4071"},"modified":"2019-11-19T22:57:05","modified_gmt":"2019-11-20T05:57:05","slug":"how-the-string_escape-function-works-in-sql-server-t-sql","status":"publish","type":"post","link":"https:\/\/database.guide\/how-the-string_escape-function-works-in-sql-server-t-sql\/","title":{"rendered":"How the STRING_ESCAPE() Function Works in SQL Server (T-SQL)"},"content":{"rendered":"<p>In <a href=\"https:\/\/database.guide\/what-is-sql-server\/\">SQL Server<\/a>, the <a href=\"https:\/\/database.guide\/what-is-t-sql\/\">T-SQL<\/a>\u00a0<code>STRING_ESCAPE()<\/code> function escapes special characters in texts and returns the text with escaped characters.<\/p>\n<p>You provide the text and the escape type as arguments when calling the function.<\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>The syntax goes like this:<\/p>\n<pre>STRING_ESCAPE( text , type )<\/pre>\n<p>Where <var>text<\/var> is the text that contains any characters to be escaped, and <var>type<\/var> determines the escaping rules to apply. <\/p>\n<p>Currently, the only value supported for the <var>type<\/var> argument is <code>'json'<\/code>.<\/p>\n<h2>Example 1 &#8211; Basic Usage<\/h2>\n<p>Here&#8217;s an example of how it works:<\/p>\n<pre>SELECT STRING_ESCAPE('\\', 'json') AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>\r\n+----------+\r\n| Result   |\r\n|----------|\r\n| \\\\       |\r\n+----------+\r\n<\/pre>\n<p>Here&#8217;s an example where a forward slash is escaped:<\/p>\n<pre>SELECT STRING_ESCAPE('\/', 'json') AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>\r\n+----------+\r\n| Result   |\r\n|----------|\r\n| \\\/       |\r\n+----------+\r\n<\/pre>\n<p>And here&#8217;s an example with a bit more text:<\/p>\n<pre>SELECT STRING_ESCAPE('Free beer\/wine with every lambo!', 'json') AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>\r\n+-----------------------------------+\r\n| Result                            |\r\n|-----------------------------------|\r\n| Free beer\\\/wine with every lambo! |\r\n+-----------------------------------+\r\n<\/pre>\n<h2>Example 2 &#8211; Invalid Type<\/h2>\n<p>At the time of writing, the only value supported for the <var>type<\/var> parameter is <code>'json'<\/code>. Here&#8217;s what happens if we supply an unsupported value:<\/p>\n<pre>SELECT STRING_ESCAPE('\\', 'oops') AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>\r\nAn invalid value was specified for argument 2.\r\n<\/pre>\n<h2>Escapable Characters<\/h2>\n<p>At the time of writing, the <code>STRING_ESCAPE()<\/code> function can only escape the JSON special characters listed in the following table:<\/p>\n<table>\n<thead>\n<tr>\n<th>Special character<\/th>\n<th>Encoded sequence<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Quotation mark (&quot;)<\/td>\n<td>\\&quot;<\/td>\n<\/tr>\n<tr>\n<td>Reverse solidus (\\)<\/td>\n<td>\\|<\/td>\n<\/tr>\n<tr>\n<td>Solidus (\/)<\/td>\n<td>\\\/<\/td>\n<\/tr>\n<tr>\n<td>Backspace<\/td>\n<td>\\b<\/td>\n<\/tr>\n<tr>\n<td>Form feed<\/td>\n<td>\\f<\/td>\n<\/tr>\n<tr>\n<td>New line<\/td>\n<td>\\n<\/td>\n<\/tr>\n<tr>\n<td>Carriage return<\/td>\n<td>\\r<\/td>\n<\/tr>\n<tr>\n<td>Horizontal tab<\/td>\n<td>\\t<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<thead>\n<tr>\n<th>Control character<\/th>\n<th>Encoded sequence<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>CHAR(0)<\/td>\n<td>\\u0000<\/td>\n<\/tr>\n<tr>\n<td>CHAR(1)<\/td>\n<td>\\u0001<\/td>\n<\/tr>\n<tr>\n<td>&#8230;<\/td>\n<td>&#8230;<\/td>\n<\/tr>\n<tr>\n<td>CHAR(31)<\/td>\n<td>\\u001f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>In SQL Server, the T-SQL\u00a0STRING_ESCAPE() function escapes special characters in texts and returns the text with escaped characters. You provide the text and the escape type as arguments when calling the function.<\/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-4071","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\/4071","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=4071"}],"version-history":[{"count":2,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/4071\/revisions"}],"predecessor-version":[{"id":4073,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/4071\/revisions\/4073"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=4071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=4071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=4071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}