MySQL explode firstname and lastname from fullname

1
2
3
4
5
6
7
8
9
10
11
12
SELECT
IF(
    LOCATE(' ', `fullname`) > 0,
    SUBSTRING(`fullname`, 1, LOCATE(' ', `fullname`) - 1),
    `fullname`
) AS 'first_name',
IF(
    LOCATE(' ', `fullname`) > 0,
    SUBSTRING(`fullname`, LOCATE(' ', `fullname`) + 1),
    NULL
) AS 'last_name'
FROM my_table;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>