Martin Fric
Martin Fric

Reputation: 730

SQL Like is not taking _

I would like to query only string that contains this exact string string_.

I tried this:

select * from table where name like "%string_%"

and results also include string-, but I need only exact _.

Any help?

Thanks

Upvotes: 0

Views: 58

Answers (1)

user330315
user330315

Reputation:

_ is the wildcard in SQL for a single character. If you want to search for _ you need to escape it:

select * 
from some_table 
where name like '%string\_%' escape '\' 

Additionally: string literals are put into single quotes in SQL. If you ever want to use a more standard compliant DBMS (Oracle, Postgres) you should get used to using single quotes for strings. Double quotes are only for identifiers (and should essentially be avoided in the first place)

Upvotes: 2

Related Questions