Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL CASE statement and REGEXP

Tags:

regex

mysql

case

I want to use a CASE statement that uses REGEXP. Currently I am doing something like this:

SELECT NAME,
 CASE INFO
   WHEN 'not cool' THEN 'Not Cool'
   WHEN 'very cool' THEN 'Cool'
 ELSE INFO
 END AS INFO
FROM INFO_TABLE

Is there any way to use REGEXP in the initial statement to make the condition act as a REGEXP? In theory this is what I want, which doesn't work:

SELECT NAME,    
 CASE INFO REGEXP
   WHEN 'not cool' THEN 'Not Cool'
   WHEN 'very cool' THEN 'Cool'
 ELSE INFO
 END AS INFO
FROM INFO_TABLE

I want 'not cool' and 'very cool' to be regular expressions. Hope that is clear enough.

like image 282
bjo Avatar asked Jan 21 '11 04:01

bjo


1 Answers

try this

select name,
case
  when info regexp 'not cool' then 'Not Cool'
  when info regexp 'very cool' then 'Cool'  
else 
  info
end 
  as info
from INFO_TABLE;
like image 192
ajreal Avatar answered Oct 26 '22 01:10

ajreal