This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing few examples for the ESCAPE Square Brackets in LIKE Predicate of SQL Server. We can use ESCAPE options for escaping such character from the string.
Check the below examples, and try it yourself.
Create sample table with data:
1 2 3 4 5 |
CREATE TABLE tbl_testStr (word varchar(100)) GO INSERT INTO tbl_testStr VALUES ('anvesh[dbrnd]patel') GO |
Test 1:
1 2 3 4 5 |
SELECT *FROM tbl_testStr WHERE word LIKE 'anvesh[dbrnd]patel' GO -- No Result |
Test 2:
1 2 3 4 5 |
SELECT *FROM tbl_testStr WHERE word LIKE '%[dbrnd]' GO -- No Result |
Test 3:
1 2 3 4 |
SELECT *FROM tbl_testStr WHERE word LIKE '%[d%' -- No Result |
Test 4:
1 2 3 4 5 6 |
SELECT *FROM tbl_testStr WHERE word LIKE '%\[d%' ESCAPE '\' word --------------------- anvesh[dbrnd]patel |
Test 5:
1 2 3 4 5 6 7 |
SELECT *FROM tbl_testStr WHERE word LIKE 'anvesh\[dbrnd]patel' ESCAPE '\' GO word --------------------- anvesh[dbrnd]patel |
SQL Server 2016: Use STRING_ESCAPE to escape single quotes, double quotes, forward slashes