This article is half-done without your Comment! *** Please share your thoughts via Comment ***
SQL Server 2016 introduced STRING_ESCAPE() to escape the characters like single quotes, double quotes, forward slashes.
SQL Server 2016: Introduced JSON support, to store and retrieve JSON document
Please check the below examples:
Example without STRING_ESCAPE:
Check the below example where I added single quotes, double quotes, forward slashes in the JSON string which is incorrect and IsJSON is also false.
1 2 3 4 |
DECLARE @str VARCHAR(800) = 'Anvesh''s blog is "dbrnd.com" which is running from 2015/05/01' DECLARE @json VARCHAR(800) = '[{"Name":"Anvesh","Gender":"Male","About":"'+@str+'"}]' SELECT @json as Json |
Result:
1 2 3 |
Json ------------------------------------------------------------------------------------------------------------- [{"Name":"Anvesh","Gender":"Male","About":"Anvesh's blog is "dbrnd.com" which is running from 2015/05/01"}] |
Check IsJSON:
1 |
SELECT ISJSON(@json) IsJson |
Result:
1 2 3 |
IsJson ----------- 0 |
Example with STRING_ESCAPE:
In the below example, check the result of STRING_ESCAPE() and IsJSON is also true.
1 2 |
DECLARE @str VARCHAR(800) = 'Anvesh''s blog is "dbrnd.com" which is running from 2015/05/01' DECLARE @json VARCHAR(800) = '[{"Name":"Anvesh","Gender":"Male","About":"'+STRING_ESCAPE(@str, 'JSON')+'"}]' |
Result:
1 2 3 4 |
SELECT @json as Json Json ----------------------------------------------------------------------------------------------------------------- [{"Name":"Anvesh","Gender":"Male","About":"Anvesh's blog is \"dbrnd.com\" which is running from 2015\/05\/01"}] |
Check IsJSON:
1 |
SELECT ISJSON(@json) IsJson |
Result:
1 2 3 |
IsJson ----------- 1 |
Leave a Reply