Get current user in MySQL without the hostname

Viewed 120

I know that we can use the user() and current_user() function to get the currently logged in user. But I need just the user without the host-name suffixed. I cant seem to find any reference to built-in MySQL function that provides only the username.

1 Answers

You can use the SUBSTRING_INDEX that basically returns the string before a character (in this case '@')

SELECT SUBSTRING_INDEX(current_user(),'@',1)

So basically with this you will avoid anything that is not the user part from current_user()

Related