Tuesday, March 20, 2012

Problem with select statement

Hello,

If I give
select * from test where description like '%test%'
it displays me all the results that have the word test in is such as testing, tests and so on.

If I give
select * from test where description = 'test'
it displays me the result with the rows which have description as test.

I have got a paragraph for description that have got many words and I want to get the results which have test in it, i.e. the description should not only be 'test' but paragraph which have got 'test' word in it and not testing and tests.

How can I do so.

Please reply as soon as possible.

Thanks in advance,
Uday.select * from test where description like '% test %'

This will not catch test if the text is like so:

'This is a test.'
'This is a test, that is not.'

and so to do what you are looking to do, you would really need to do a more complete parsing of the entry. A naive attempt at this would be:

select * from test where description like '% test[. ,;]%'

Even this will miss:

'Test, one two three'|||Hi,

It seems the above one is working but what exactly are we doing by giving [. ,;] and why "Test, one two three" will not work.

Thanks in advance,
Uday.|||[. ,;] is a regular expression meaning that the character after the last t in test must be a space, comma, period or semicolon.

As written, the Like clause expects a space in front of the word Test. Without that space, it would not match. You could do an additional test for LIKE '% Test[. ,;]%'. Again, even that would miss something like '(this is a test)'|||Hi,

Thanks for you help.

Best Regards,
Uday.

No comments:

Post a Comment