MySQL: How to remove value matching another column from beginning or end of column? -


i'm parsing through tv program data stored in sql table, , data has 2 columns, 1 program title whole (e.g. series name) , 1 program's subtitle (e.g. individual episode title). program subtitle quite contains series name within well, either @ beginning of string or @ end, or both, in following formats:

e.g. title of "horizon" , correct subtitle of "the £10 million challenge", following possible combinations value in subtitle field:

  • horizon: £10 million challenge
  • the £10 million challenge: horizon
  • horizon: £10 million challenge: horizon

instead of colon followed space, separator space hyphen space so: "horizon - £10 million challenge - horizon"

so want each row in table, if subtitle contains value in title column @ beginning of subtitle (followed ': ' or ' - ') or if value @ end of column (preceded ': ' or ' - ') or both, update subtitle remove prefix or suffix. can or point me in right direction? i'm unsure start.

why want title name in subtitle field?

i think should extract actual name , want it. think requires massive case statement:

update table t     set subtitle = (case when subtitle concat(title, ':%', title)                          trim(substring_index(substring_index(subtitle, ':', 2), ':', -1)))                          when subtitle concat(title, '-%', title)                          trim(substring_index(substring_index(subtitle, '-', 2), '-', -1)))                          when subtitle concat('%:', title)                          trim(substring_index(subtitle, ':', 1))                          when subtitle concat('%-', title)                          trim(substring_index(subtitle, '-', 2))                          else subtitle                    end) 

test out in select first. also, won't work in cases if subtitle has hyphens or colons in name.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -