Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dash (-) character in LIKE query

In my database I have to filter records where name ends with -N, but when I make the WHERE clause like in the following query it returns me no records, because - is a wild card character.

I am using this query in Oracle database:

 select * from product where productname like '%-N' 

but the database has records that end with this product name

like image 726
adesh singh Avatar asked Oct 18 '25 21:10

adesh singh


1 Answers

At first I thought that Oracle allows to specify a range [a-z] in the LIKE operator, and that needs to treat - in a special way. So, my suggestion was to escape the dash:

select * from product where productname like '%\-N' ESCAPE '\'

https://docs.oracle.com/cd/B13789_01/server.101/b10759/conditions016.htm


On the other hand, as @Amadan correctly said in the comment, Oracle's LIKE operator only recognises two wildcard characters: _ and %.

It means that escaping the - should not change anything.

Which means that most likely the dash symbol in the query is not the same dash symbol that you have in your table. There are many-many-many different dashes and hyphens in unicode. Here are the most common. Hyphen-Minus (0x002D), En-Dash (0x2013, Alt+0150), Em-Dash (0x2014, Alt+0151).

- – —
like image 171
Vladimir Baranov Avatar answered Oct 21 '25 11:10

Vladimir Baranov