Tuesday, December 6, 2011

Get initials from a string using Oracle PL/SQL

To transform 'First Name' into 'FN', etc

create or replace function get_initials(input_string in varchar2) return varchar2 as
tmp varchar2(64); rv varchar2(64); c char; pos int;
rv := ''; tmp := input_string;
-- grab first char, if it's in [A-Z]
c := substr(tmp,1,1);
if ascii(c) >= 65 and ascii(c) <= 90 then rv := rv || c; end if;
-- next word, or done
pos := instr(tmp,' ');
if pos > 0 then tmp := substr(tmp,instr(tmp,' ')+1);
else exit;
end if;
end loop;
return rv;

No comments: