sreehari
sreehari

Reputation: 189

How to use like condition in query to fetch strings that contains % symbol

I have 3 strings entries in my table sample and column test 1.abc 2.abc%d 3.abc%E Now I want to write a query to fetch all the records in column test that contains abc% using like condition. The output should be abc%d and abc%E.

Upvotes: 0

Views: 976

Answers (2)

BHUVANESH MOHANKUMAR
BHUVANESH MOHANKUMAR

Reputation: 2787

Have you used the "_" underscore character in SQL query? This may help to resolve your issue.

    select * from myTableName where details like 'abc%_%'
or 
    select * from myTableName where details like 'abc%_'
or 
    select * from myTableName where details LIKE '%abc\%%' ESCAPE '\'
or
    select * from myTableName where details LIKE 'abc\%%' ESCAPE '\'

All the above queries will solve your issue, use the appropriate query based on your application need and requirement.

Reference: Use Underscore character in wild card charecter of Like query gives me all table result

Upvotes: 1

Quietust
Quietust

Reputation: 257

As stated in the documentation, you must escape instances of wildcard characters if you do not want them to behave as such.

To test for literal instances of a wildcard character, precede it by the escape character. If you do not specify the ESCAPE character, \ is assumed.

  • \% matches one % character.
  • \_ matches one _ character.

Upvotes: 0

Related Questions