The solution to the field content containing underscores in oracle fuzzy query

    In a recent project, I encountered a problem about fuzzy queries. If the value of the field name in the table tabA is underlined, it is found that the queried records are incorrect during the fuzzy query.

table structure

Table name: tabA

id     name         sex

1      test_601     1

2      test_602      2

3 test16 1

4 test26 2

Sql statement: select * from name like '%test_6%' ; I originally wanted to query the records with id 1 and 2, but all the records were queried.

It turns out that the underscore [_] has a special meaning in oracle, which means matching any single character, so the meaning of the above sql is to query all records with any character between test and 6.

Solution: Use the escape keyword to escape these special characters into the original character meaning

Correct sql: select * from name like '%test/_6%' escape '/'; It means that the characters behind / after escape no longer have special meanings.

% in oracle is also a special character, representing one or more characters

Guess you like

Origin blog.csdn.net/dhklsl/article/details/129725344