2

I am searching for a string, '%(' in a column that contains various text. This string can come anywhere within the text, so I tried to use LIKE clause in searching for string.

where COLUMN LIKE '%(%'

However, this treats the % as a wildcard and searches every ( in the text.

To escape, I tried to use ESCAPE keyword

where COLUMN LIKE '%(%' ESCAPE '%'

But this resulted in an error:

Selection error involving field *N.

Please let me know if the string can be found. Some of the strings involved are : "%FOUND(....", or "...%SUBST(....", or even "...(%ABS(..."

I am using DB2 on AS400.

3 Answers 3

4

WHERE column LIKE '%\%(%' ESCAPE '\'

3
  • strangely, this does not works on my end, in my case, like '%\%(%' escape '\' returns no records Jan 29, 2013 at 13:16
  • That is indeed strange, as this is definitely the correct syntax (you can use any character for '\', ie. LIKE '%_%(%' ESCAPE '_')
    – paul
    Jan 29, 2013 at 13:32
  • Oh, finally it clicked: The string that I was searching had characters between % and ( so I had to insert another %. Hence my query became LIKE '%/%%(%' escape '/'. Thanks for help! Jan 29, 2013 at 13:39
0

Well I tried following and it sorta works..

CREATE TABLE test(column1 varchar(500))
go
INSERT INTO test VALUES('42%344%lsdj%'),('hello%asgj%sdf%l8-')

SELECT * FROM test WHERE column1 LIKE '%344[%]lsd%'

Returns:

column1
--------------
42%344%lsdj%
0

Another method, but not as efficient as above,

WHERE INSTR(column,'%(') > 0 
1
  • @WarrenT I might have missed out, OP has stated as %(, thanks for pointing out
    – bonCodigo
    Jan 29, 2013 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.