Hi friends, in SQL server 2016 another string function that introduced is String_Escape. This function can escape special characters within texts and will return text with escaped characters.
STRING_ESCAPE (expression, type)
Currently only the value that is supported for type argument is ‘json’ only. If you try to specify another type it will result into error stating ‘An invalid value was specified for argument 2’.
In expression any nvarchar expression can be specified that needs to be escaped.
Currently, only limited JASON escape characters can be escaped.
Here is the below script that is used to show the escaped characters like this –
It will results in this manner –
As we can see in result set the escaped characters like new line, carriage return, double quotes etc. for the expression that we specified.
Let’s check another example in which I will create a table and insert some escape characters in the column value.
Create table #temp
Insert into #temp values (‘as/ ”””’)
Now let’s run below query and check the results –
SELECT string_escape(name,’json’) FROM #temp
This function will helps in returning the escape characters from the expression.
Hope you like this post. 🙂