Search code examples
mysqlsqlselectdomo

CASE WHEN "__" LIKE "__" ELSE "__" MySQL Error


I am trying to standardize campaign naming conventions in a dataset via MySQL. Please see the code below;

SELECT 
`Campaign`
FROM 
`cost_per_platform`
case when `Campaign` like '%brand%' then 'Brand'
else 'Other' end

This is run via DOMO.com MySQL function. I am provided with the following error;

The database reported a syntax error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'case when Campaign like '%brand%' then 'Brand' else 'Other' end' at line 5

Does anyone have a suggestion on a fix? Thank you for your help.


Solution

  • You want the case expression in the select clause:

    select
        campaign,
        case when campaign like '%brand%' then 'Brand' else 'Other' end as new_campaign
    from cost_per_platform
    

    This adds the second column to the resultset, called new_campaign, that represents the "standardized" value.

    If you want an update query instead (that is, replace the existing value with the "standardized" value)

    update cost_per_platform
    set 
    campaign = case when campaign like '%brand%' then 'Brand' else 'Other' end