Home
PHP
Tech Tube
MySQL
Linux
CSS&HTML
JavaScript

Get string before or after a specific character via MySQL

Here is my solution to a situation where I needed to select just parts of MySQL records using specific char as a delimeter (in this case "@"). In this example the task was to get the domain names of email addresses of the users but there's also an example of how to get the part of the email before the "@" symbol just in case.
# Get everything after specific symbol:
SELECT RIGHT(user_email, CHAR_LENGTH(user_email) - INSTR(user_email,"@")) FROM users;

# Get everything before specific symbol:
SELECT LEFT(user_email, INSTR(user_email,"@")-1) FROM users;

# The following will do a good job if you expect more than one occurrence of specific symbol:
SELECT SUBSTRING_INDEX(user_email, "@", -1) AS domain;

# Select the string after last occurrence of slash
SELECT SUBSTRING_INDEX(column, '/', -1) FROM table;