unkn0wn unkn0wn - 1 month ago 9
MySQL Question

How to get a substring from a MYSQL table TEXTFIELD

Assume my DB structure is

TABLE1 - id, field1


and sample entry is

1
<div class="image" data-content="contact"><img style="margin: 0px auto;display: table;" src="LINK" alt="contact" title=""></div>


Now i am trying to get this value LINK that is under src tag. I tried useing regex, search but none of them are working correctly.

SELECT *
FROM TABLE1
WHERE field1
BETWEEN 'src' AND 'alt';


And,

SELECT substring_index(substring_index(field1, 'src=', -1),
'alt', 1)
FROM TABLE1

Answer

HI you can use below command

SELECT SUBSTRING(@str, CHARINDEX('src', @str)+5 , (CHARINDEX('alt',@str)+5) - (CHARINDEX('src', @str) + Len('alt'))-9) from TABLE1

Comments