Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select first word - mysql query

Tags:

database

mysql

i need to select only the first name (that is the first word) of the user.

For example, Andy Jones, only select Andy

Any idea?

thanks

like image 394
user455318 Avatar asked Jun 22 '11 03:06

user455318


2 Answers

Take a look at substring_index.

select substring_index(field, " ", 1) ....
like image 72
Suroot Avatar answered Oct 06 '22 00:10

Suroot


If I understand, your FirstName and LastName are in one column. You can achieve this using user defined functions.

Example:

SELECT SPLIT_STR(name, ' ', 1) as firstname FROM users;

Read following post for more options:

  • Split value from one field to two
like image 37
Naveed Avatar answered Oct 05 '22 23:10

Naveed