I did something similar to your function but just using Instr and substr as suggested by Dagon.
It was more complicated as users enter absolutely any thing they want into this field. Its not strictly their fault as lazy design has allowed them to do it. Form should have title, first name and last name fields.
I have data like
Mr John Smith
Mr Smith
John Smith
John
Further complicated by fact that Title for say Mr has been entered as
'Mr', 'Mr.', 'mr', 'MR' hence my title filter in the query.
This is my attempt at code it generally works except where/when I have not anticipated a new title. Welcome any solutions which can make it more robust.
select Client_key, cli_post_salu,
(case
when instr(trim(cli_post_salu), ' ', 1) =0 then Null
when substr(cli_post_salu, 1, (instr(cli_post_salu, ' ', 1)-1)) in ('DR', 'Dr', 'Dr.', 'MR', 'MRs', 'Miss', 'Mis', 'Mr.', 'Mr', 'Mr/Mrs', 'Mr/Miss', 'Mrd', 'Mrs', 'Mrs.', 'Ms', 'Nr', 'miss', 'mr', 'mrs', 'ms', '|Mr')
then substr(cli_post_salu, 1, (instr(cli_post_salu, ' ', 1)-1)) else Null end ) TITLE,
(case
when instr(trim(cli_post_salu), ' ', 1) = 0 then cli_post_salu
when not(substr(cli_post_salu, 1, (instr(cli_post_salu, ' ', 1)-1)) in ('DR', 'Dr', 'Dr.', 'MR', 'MRs', 'Miss', 'Mis', 'Mr.', 'Mr', 'Mr/Mrs', 'Mr/Miss', 'Mrd', 'Mrs', 'Mrs.', 'Ms', 'Nr', 'miss', 'mr', 'mrs', 'ms', '|Mr'))
then substr(cli_post_salu, 1, (instr(cli_post_salu, ' ', 1)-1))
when substr(cli_post_salu, 1, (instr(cli_post_salu, ' ', 1)-1)) in ('DR', 'Dr', 'Dr.', 'MR', 'MRs', 'Miss', 'Mis', 'Mr.', 'Mr', 'Mr/Mrs', 'Mr/Miss', 'Mrd', 'Mrs', 'Mrs.', 'Ms', 'Nr', 'miss', 'mr', 'mrs', 'ms', '|Mr')
and instr(trim(cli_post_salu), ' ', 1, 2) <> 0
then substr(cli_post_salu, (instr(cli_post_salu, ' ', 1) +1), (instr(cli_post_salu, ' ', 1, 2)-(instr(cli_post_salu, ' ', 1) +1)))
Else Null End) FIRST_NAME,
(case
when instr(trim(cli_post_salu), ' ', 1) = 0 then null
when instr(Trim(cli_post_salu), ' ', 1, 2) = 0 then substr(cli_post_salu, (instr(cli_post_salu, ' ', 1) +1), length(cli_post_salu))
when instr(cli_post_salu, ' ', 1, 2) <> 0 then substr(cli_post_salu, (instr(cli_post_salu, ' ', 1, 2) +1), length(cli_post_salu))
else Null end) LAST_NAME
from client
where cli_post_salu is not null
Thanks
Ian