Home > database >  MySQL Case Statement with LIKE for string
MySQL Case Statement with LIKE for string

Time:10-08

I have a table with an existing user_agent column that I want to make a new column based on if user agent contains the substrings 'windows' or 'os x' in MySQL and I can't figure out what is wrong or find examples of case statements with LIKE for strings on this site. My version of MySQL is 8.0.26.

SELECT user_id, 
    CASE user_agent 
    WHEN LIKE '%windows%' THEN 'Windows'
    WHEN LIKE '%OS X%' THEN 'Mac'
    Else 'Other'
    END AS Operating System

    FROM pageviews;

CodePudding user response:

SELECT user_id, 
    CASE 
    WHEN user_agent  LIKE '%windows%' THEN 'Windows'
    WHEN user_agent  LIKE '%OS X%' THEN 'Mac'
    Else 'Other'
    END AS  `Operating System` 

    FROM pageviews;
  • Related