Home > OS >  Case Statement, Like operator in SQL
Case Statement, Like operator in SQL

Time:08-26

I have multiple rows in the table (as in the image). I need to make a new column from specific keywords extracted from the strings.

Title Output
Ad - LA - [Ke] [NC] [W] Test for IPHIndi W
[MWeb] My Ads Center (Batch 3) Part 2 Mweb
Ad - LA [NC] [W MWeb] My Ads Center W MWeb
Ad - LA [NC] [WZ K] My Ads Center Others
Ad - LA [NC] [W MWeb] My Ads Center Mweb
[M] WMy Ads Center (Batch 3) Part 2 M

I tried using case statement

CASE 
    WHEN title LIKE '%W MWeb%' THEN 'w m'
    WHEN title LIKE '%MWeb%' THEN 'MWeb'
    WHEN title LIKE '%W%' THEN 'w'
    WHEN title LIKE '%M%' THEN 'M'
    ELSE 'OTHER'
END AS MARKER

However, I am getting incorrect data as sometimes if we have any letter as M, it is marking it as M which is incorrect and is there any way where I can search using the square brackets?

Please help me out and thank you

CodePudding user response:

I think you need to escape the square brackets as they act as a wildcard operator to specify a range.

Usually it's done with \ before the character you want to escape but might depend on the types of database.

This might be helpful, good luck

escaping square brackets in SQL

  • Related