Fetching the sub-string from the database

2019-09-19 13:39发布

I have a table with a column which contains strings like below.

sourabh@winworldsoft.com

monica@winworldsoft.com

sachin@winworldsoft.com

I need to get the substring from the @ to .(dot) i have writing some sql but it have fixed length i need the dynamic query to get the sub-string.

select *from registration where email like '%@%';

Sub string Query select email, substr(email,4,10) from registration;

please and one write query for me.

3条回答
我只想做你的唯一
2楼-- · 2019-09-19 14:20

This should do it:

SELECT REVERSE(SUBSTRING(REVERSE(SUBSTRING_INDEX(email, '@', -1)), LOCATE('.', REVERSE(email))+1));

This will include all dots between @ and the final one (e.g. "someone@a.long.domain.name.com" will result in "a.long.domain.name") as opposed to nested SUBSTRING_INDEX which will only return whats between @ and the first dot (e.g "a").

查看更多
beautiful°
3楼-- · 2019-09-19 14:25

substring_index() is the best approach:

select substring_index(substring_index(email, '@', -1), '.', 1)
查看更多
登录 后发表回答