sebnears
sebnears

Reputation: 63

How to use "NOT LIKE" for a column values in SQL

So I have a table like this:

X.        Y.
34560     eudhkri 34560 abc
26558     hsyrb 26558 ax
3666      dhurb 3666 yzhdj
3666      dhfhjf 366688 avh
233       abc 233 hdhsijej

Since characters in the Y column are changeable (meaning they have different number of letters) I thought I might use the NOT LIKE operator to exclude the rows where the X value is in the Y column.

However, I don't know how. It is not possible to just write it every values of X down. Also, I wrote the 4th row specifically to show you I can't exclude this data since the numbers are not same, so using % might cause some trouble.

Upvotes: 3

Views: 2515

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133380

if you want to use the like operatoe you could try this way

select *
from my_table 
where y NOT like concat('%',x,'%')

or

select *
from my_table 
where y NOT like concat('% ', x,' %')

Upvotes: 3

Laurenz Albe
Laurenz Albe

Reputation: 247063

You could use a regular expression:

WHERE y ~ ('\m' || x || '\M')

\m marks the beginning of a word, \M the end.

Upvotes: 4

Related Questions