Wimpie Norman
Wimpie Norman

Reputation: 27

How to use SQL "LIKE" operator by using wildcard?

I am currently having trouble finding all rows that contain the following data in a table "%2" or "%20".

For Example: 'Susan rides%2bike across town'.

SELECT * FROM [my_table] WHERE [description] LIKE % %2 %

I know this example will not work, however is there a way to search for these values "%2" or "%20" using the SQL "LIKE" operator? As I am required to find and replace them with a white space.

Upvotes: 0

Views: 732

Answers (3)

Empower Coding
Empower Coding

Reputation: 66

enter image description hereThe solution for the above stated probelem is

select * from my_table where description like'%[%]2%'

Upvotes: 0

Simulant
Simulant

Reputation: 20142

SELECT * FROM my_table WHERE description LIKE '%\%2%' ESCAPE '\';

For the LIKE keyword you can specify the ESCAPE sequence afterwards. In this example the leading backwards slash escapes the % so it will be interpreted as literal value.

Read more about it in the documentation: https://learn.microsoft.com/en-gb/sql/t-sql/language-elements/like-transact-sql?view=sql-server-ver15

you can test it in this SQL Fiddle.

Upvotes: 1

Wimpie Norman
Wimpie Norman

Reputation: 27

Thank you for this.

SELECT * FROM [my_table] WHERE [description] LIKE '%[%]2%'

Upvotes: 0

Related Questions