Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching a String having '%' as a character

Tags:

sql

I want to match a String using Like operator. The challenge is having '%' as a character in my string.

i.e. Row1 : Column = CT%CNV!XYZABCD...
     Row2 : Column = CTXXXCNV!XYZABCDE...

If I use "SELECT * FROM table WHERE Column like 'CT%CNV!%'. It doesn't consider '%' as a character and the statement returns both rows.

I need to return the first row only.

like image 404
mowienay Avatar asked Apr 16 '14 06:04

mowienay


People also ask

How do you get the matching characters in a string?

In order to find the count of matching characters in two Java strings the approach is to first create character arrays of both the strings which make comparison simple. After this put each unique character into a Hash map.

What does ?= Mean in regex?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

Which operator is used to match the occurrence of one character in the string?

The simplest and very common pattern matching character operators is the . This simply allows for any single character to match where a . is placed in a regular expression. For example /b.t/ can match to bat, bit, but or anything like bbt, bct ....


Video Answer


2 Answers

You shoud use escape keyword:

select *
  from MyTable
 where Column like 'CT\%CNV!XYZABCD%' escape '\'

here '\%' is treated as a plain symbol, while '%' is wild card one

like image 136
Dmitry Bychenko Avatar answered Oct 01 '22 22:10

Dmitry Bychenko


You can use brackets to escape the percent sign :

SELECT * FROM table WHERE Column like 'CT[%]CNV!%'
like image 37
aleroot Avatar answered Oct 01 '22 22:10

aleroot