Reputation: 94
I have a table with a column which contains strings like below.
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.
Upvotes: 0
Views: 83
Reputation: 2499
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. "[email protected]" 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").
Upvotes: 1
Reputation: 1269883
substring_index()
is the best approach:
select substring_index(substring_index(email, '@', -1), '.', 1)
Upvotes: 0