Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut part of a string in MySQL?

Tags:

sql

select

mysql

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?

like image 228
Miko Meltzer Avatar asked Feb 21 '23 02:02

Miko Meltzer


1 Answers

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.

like image 102
Denys Séguret Avatar answered Mar 01 '23 03:03

Denys Séguret