SQL / PLSQL to split string into chunks
Ramky99Aug 18 2009 — edited Aug 18 2009Hi all,
I have an issue converting some name data from one table structure to another.
e.g.
SQL> desc names
Name Null? Type
----------------------------------------- -------- ----------------------------
TITLE VARCHAR2(5)
FNAME VARCHAR2(20)
LNAME VARCHAR2(20)
SQL> desc data
Name Null? Type
----------------------------------------- -------- ----------------------------
FULLNAME VARCHAR2(50)
insert into data values ('MR I HAVE AN ESPECIALLY LONG NAME INDEED');
insert into data values ('MINE IS EVEN LONGER, QUITE RIDICULOUSLY SO IN FACT');
Essentially I need to split these long names, stored in 1 field, into the 3 fields above. The tricky part however is that I want to do it in such a way so that if the 1st part of the name fits the 1st 5 char field I'd like it to do that, if not I'd want it to split it between the next 2 fields - again without splitting a string. Reason behind this is that the application automatically puts a space between each field when they display and I'd like to avoid gaps in names if at all possible.
This has me a little stumped so any help would be seriously great ... it might not even be a go-er as it might be too wasteful with available space but I'd like to give it a shot.
Thanks!
Adam