SQL – How to split a single column values to multiple column values

How to split a single column values to multiple column values..

For Example:

#Current table Field is like::

Name
------------
abcd efgh
ijk lmn opq
asd j. asdjja
asb (asdfas) asd
asd

#Have to Change Like This ::

first_name last_name
----------------------------------
abcd efgh
ijk opq
asd asdjja
asb asd
asd null



#Query::

SELECT uid,IF(
 LOCATE(' ', `name`) > 0,
 SUBSTRING(`name`, 1, LOCATE(' ', `name`) - 1),
 `name`
 ) AS memberfirst,
 IF(
 LOCATE(' ', `name`) > 0,
 SUBSTRING(`name`, LOCATE(' ', `name`) + 1),
 NULL
 ) AS memberlast
FROM `users` 
ORDER BY `users`.`uid` DESC

Leave a Reply

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