user3409461
user3409461

Reputation: 177

mysql - how to select a string in column?

I have column contain a string like that :

"https://www.youtube.com/watch?v=R3-kfnKmDVc"

I want to select this column but get string like that "R3-kfnKmDVc". How do?

Upvotes: 0

Views: 48

Answers (3)

radu.cigmaian
radu.cigmaian

Reputation: 46

You can use:

SELECT SUBSTRING('https://www.youtube.com/watch?v=R3-kfnKmDVc', (SELECT LOCATE('?v=', 'https://www.youtube.com/watch?v=R3-kfnKmDVc') + 3));

-> usage in select:

select SUBSTRING(url_name, LOCATE('?v=', url_name) + 3) from table;

Thank You

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269763

If you want the part of the string after the last =, you can do:

select substring_index(col, '=', -1)

In the event that the string may not contain an '=':

select (case when col like '%=%' then substring_index(col, '=', -1) end)

Upvotes: 4

CMPS
CMPS

Reputation: 7769

By getting the id only you are not improving the performance, so you can simply fetch it from DB then use regex functions to get the id of the youtube link.

Upvotes: 0

Related Questions