Is there any function for finding the result with special character in a sql like?

Viewed 82

I'm try to get data from SQL using LIKE successful to get data but the result more than I need

Here is my some data

apple1
apple2
apple3
applejuice1
applej1
applej2

My query:

select * from apple where name like '%applej%'

Currently I got:

applejuice1
applej1
applej2

My Expected output:

applej1
applej2
4 Answers

You can try this,

SELECT * FROM apple where name LIKE '%applej_'

'_' is used to represent a single character in SQL server. For MS Access you can try this

SELECT * FROM apple where name LIKE '*applej?'

Using REGEXP this is possbile:

select * 
from apple 
where `name` REGEXP '^applej[0-9]'

Demo on db<>fiddle.


Update:

If the name has data as applej1, applej2, applej15, applej109, the following query will work:

select * 
from apple 
where `name` REGEXP '^applej[0-9]+'

Demo on db<>fiddle

You can use a regular expression, this will include all rows containing 'applej' + 0 or 1 more character

SELECT * FROM test WHERE  col1 REGEXP '^applej.?$'

This one will find all rows containing 'applej' + exactly 1 more character

SELECT * FROM test WHERE  col1 REGEXP '^applej.{1}$'

And if the number after 'applej' might contain several digits

SELECT * FROM test WHERE  col1 REGEXP '^applej[0-9]+$'

Of course maybe the LIKE suggested by @forpas in the comments above is all you need

i found the solution with

SELECT * FROM apple WHERE MATCH(name) AGAINST('applej')
Related