0

I'm trying to figure out how to remove characters in a string in SQL. The nomenclature that is currently in the database looks like this:

Engine - Brand - Category

How can I remove everything and only leave the 'category' portion?

Funnily enough, the code I've been currently using has been doing the opposite of what I want. It's keeping everything and removing the 'category' portion. I've tried leading, both, and trailing and still get the same results

select trim(Both'-' from SUBSTRING_INDEX(ad_group,'- ',2)) 
from farm.GoogleAdGroup GAG
join farm.GoogleCampaign GC on GAG.googlecampaign_id_fk = GC.id
where campaign_type like 'product'
limit 50;

I just need the code to keep the 'category' section and remove everything else.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
mpstring
  • 39
  • 1
  • 9

1 Answers1

5

try like below

select SUBSTRING_INDEX('Engine - Brand - Category','-',-1)

it will return Category

so in your case

select SUBSTRING_INDEX(ad_group,'-',-1)
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63