5

In MySQL, I have a text column with "bla bla bla YYY=76767 bla bla bla".

I need to cut the number 76767.

How can I do it in SQL?

Phillip
  • 2,127
  • 3
  • 24
  • 39
Miko Meltzer
  • 53
  • 1
  • 1
  • 4

3 Answers3

7

You can use

select substring_index(substring(mycol, instr(mycol, "=")+1), " ", 1)

to get the first token after the =.

This returns 76767.


This works in two steps :

substring(mycol, instr(mycol, "=")+1)

returns the string starting after the =

and

substring_index( xxx , " ", 1)

get the first element of the virtual array you'd got from a split by " ", and so returns the first token of xxx.

Denys Séguret
  • 355,860
  • 83
  • 755
  • 726
  • More abstract/general: set `@frst` and `@scnd` as delimiters. The term you are looking for is between `@frst` and `@scnd`. In this case it is `SET @frst:='YYY='; SET @scnd:='';` You either want to update, so then `UPDATE tbl set my_col = SUBSTRING_INDEX(SUBSTRING(my_col , INSTR(my_col , @frst)+1), @scnd, 1) WHERE my_col LIKE '%@scnd%';` or you want to select, so `SELECT SUBSTRING_INDEX(SUBSTRING(my_col , INSTR(my_col , @frst)+1), @scnd, 1) from WHERE my_col LIKE '%@scnd%';` – kklepper Jan 07 '22 at 16:50
2

The easiest approach to eliminate the number in your field would be just to REPLACE it.

SELECT REPLACE(yourcolumn, '76767', '')
FROM yourtable;

You can use a combination of SUBSTRING and INSTR, too.

Bjoern
  • 15,244
  • 4
  • 40
  • 47
0

If you wan't to UPDATE, use

UPDATE table_name SET column_name = REPLACE(column_name, '76767', '');

If you wan't to replace 76767 with bla bla bla in your db, use

UPDATE table_name SET column_name = REPLACE(column_name, '76767', 'bla bla bla');
Pradeep Kumar
  • 3,845
  • 2
  • 33
  • 39