Separating MySQL Username and Host

// May 6th, 2009 // Database, Development, MySQL

As the MySQL USER() command returns users as ‘user’@'host’, it can be difficult to do straight comparasons. One way to extract both username and host is by using SUBSTRING_INDEX. For example, in a stored procedure you could use:


-- USER
SELECT SUBSTRING_INDEX(USER(), '@', 1) INTO txtUsername;
-- HOST
SELECT SUBSTRING_INDEX(USER(), '@', -1) INTO txtHost;

Voila!

Leave a Reply