How to parse Full Name separated by comma into First Name and Last Name in Oracle SQL? For example, the following string 'Smith, James'
should be split into 2 strings 'Smith'
and 'James'
Here you go, it works and does not throw exceptions even at the edge cases
select full_name,
TRIM(SUBSTR(full_name, INSTR(full_name,',')+1, LENGTH(full_name))) first_name,
TRIM(SUBSTR(full_name,1, INSTR(full_name,',')-1)) last_name
from
(
select 'Smith, Michael' as full_name from dual union all
select 'Smith ,Michael' as full_name from dual union all
select 'Smith , Michael' as full_name from dual union all
select 'Lopes, Jennifer' as full_name from dual union all
select 'Lopes, Jennifer' as full_name from dual union all
select 'Lopes Jennifer' as full_name from dual union all
select 'Lopes' as full_name from dual union all
select 'Jlo,' as full_name from dual union all
select ',Jlo' as full_name from dual union all
select '' as full_name from dual
)
Results:
FavScripts.com is a free tool to save your favorite scripts and commands, then quickly find and copy-paste your commands with just few clicks.
Boost your productivity with FavScripts.com!