简体   繁体   中英

MYSQL How to get the last second word from String

I'm trying to split the string by Firstname , MiddleName and LastName but I'm having trouble getting the MiddleName. Here is my query:

select
  SUBSTRING_INDEX(admin, ' ', 1) as 'FirstName',
  SUBSTRING_INDEX(admin, ' ', +1) as 'MiddleName',
  SUBSTRING_INDEX(admin, ' ', -1) as 'LastName'
FROM fullname

My table | id | FullName | | -- | ---------------------| | 1 | Kim Paulo D. Ercillo | | 2 | Levi T. Marquez | | 3 | Brian W. Smiley |

Desired output
| FirstName| Middle Name| Last Name | |----------| ---------- |-------------| | Kim | D | Ercillo | | Levi | T | Marquez | | Brian | W | Smiley |

Result I'm getting
| FirstName| Middle Name| Last Name | |----------|------------|-------------| | Kim | Kim | Ercillo | | Levi |Levi | Marquez | | Brian | Brian | Smiley |

select
  SUBSTRING_INDEX(admin, ' ', 1) as 'FirstName',
  TRIM(TRAILING '.' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(admin, ' ', -2), ' ', 1)) as 'MiddleName',
  SUBSTRING_INDEX(admin, ' ', -1) as 'LastName'
FROM
  fullname

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM