Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match text ending with a text on DB2?

Tags:

sql

sql-like

db2

I've executed the query on DB2:

SELECT * FROM MYTABLE WHERE MYFIELD LIKE '%B' 

Although I know there are records that end with 'B' in the database, the query returned no results. After some research, it seems that DB2 is not recognizing LIKE expressions that don't end with '%'. So the following query would work:

SELECT * FROM MYTABLE WHERE MYFIELD LIKE '%B%' 

but naturally not as expected, because it will return only the rows, where MYFIELD contains 'B', but doesn't end with it.

How to go around that strage, hmmm, feature? How to match the text on the end of the word in LIKE-like expressions?

like image 530
Danubian Sailor Avatar asked Feb 13 '23 15:02

Danubian Sailor


1 Answers

DB2 can match patterns at the end of the string. The problem is probably that there are other characters.

You can try:

WHERE rtrim(MYFIELD) LIKE '%B'

You can also look at the lengths of the field and delimit the string value to see if there are other characters:

select length(MyField), '|' || MyField || '|'
from mytable
where MyField like '%B%';
like image 135
Gordon Linoff Avatar answered Feb 15 '23 10:02

Gordon Linoff