Escapes
special characters in texts and returns text with escaped characters. STRING_ESCAPE is
a deterministic function, introduced in SQL Server 2016.
Syntax
STRING_ESCAPE
(text, type)
Text: it is an nvarchar expression
representing the object that should be escaped.
Type: Escaping rules that will be
applied. Currently the value supported is 'Json'.
See the example:
Return Types
Nvarchar (max) text
with escaped special and control characters. Currently STRING_ESCAPE can
only escape JSON special characters shown in the following tables.
Special
character
|
Encoded
sequence
|
Quotation
mark (")
|
\"
|
Reverse
solidus (\)
|
\\
|
Solidus
(/)
|
\/
|
Backspace
|
\b
|
Form feed
|
\f
|
New line
|
\n
|
Carriage
return
|
\r
|
Horizontal
tab
|
\t
|
Control character
|
Encoded
sequence
|
CHAR(0)
|
\u0000
|
CHAR(1)
|
\u0001
|
...
|
...
|
CHAR(31)
|
\u001f
|
See the example
SELECT
STRING_ESCAPE('['' This is a special / "message"
/'']', 'json') AS
escapedJson;
SELECT STRING_ESCAPE('\ /
\\ "
', 'json') AS escapedText;
|