Gunaseelan
Gunaseelan

Reputation: 2542

Get the substring from a string if the string contains the substring in mysql

We can do this using programming languages. But getting the result from mysql is hard to find.

I have column in a table that has string values.

ex: saturday monday tuesday wednesday thursday friday sunday

If I search monday from the above string, the query should give only monday as my result.

I need to get only the searched word from the string as a mysql result.

I tried the following query.

 SELECT SUBSTRING_INDEX(usertext,'STORY',0) FROM `usertext`

But it didn't give any result.

I am looking for regular expression to find the solution.

If my question is not clear, kindly comment.

Upvotes: 0

Views: 162

Answers (1)

MrApnea
MrApnea

Reputation: 1946

How about this?

select SUBSTR(usertext, INSTR(usertext, 'sunday'), length('sunday'))  
from myTable 
where CONCAT(' ', usertext, ' ') rlike ' sunday ';

If you are sure that you always search for complete words then a simpler version can be:

select SUBSTR(usertext, INSTR(usertext, 'sunday'), length('sunday'))
from myTable
where usertext like '%sunday%';

As far as i know MySql dont have support for regular expression in the select to get out values, only for the where part.

Upvotes: 1

Related Questions