5

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

user455318
  • 3,280
  • 12
  • 41
  • 66

3 Answers3

21

Take a look at substring_index.

select substring_index(field, " ", 1) ....
Suroot
  • 4,315
  • 1
  • 22
  • 28
1

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:

Community
  • 1
  • 1
Naveed
  • 41,517
  • 32
  • 98
  • 131
0

For something like this you would normally put the first and last name in their own columns. If you cannot, then you can get the index of the first space, then substring(0,index of the first space). I would really recommend though that you split that into two columns, if you can.

hivie7510
  • 1,246
  • 10
  • 23