I've got example two strings with the following layout:
'COMPANY A - 01-Jan-19 - 30-Jun-19 - 100.00%'
'COMPANY B - 01-Jan-19 - 30-Jun-19 - 50.00% / COMPANY C - 01-Jan-19 - 30-Jun-19 - 50.00%'.
It describes the percentage of a consultant who works at a certain client or multiple clients. I'm receiving their info in the above layout. What I would like to do is to split those strings and store it in a table. The structure of the table is as follows (with the intentional data between brackets):
- ID - Number --Automatic generated row ID
- ConsultantID - Number --Foreign key to Person table
- Company - Varchar2(128) --Name of the company where the consultant works (Company A)
- StartDate - Date --Start date of the project (01-Jan-19)
- EndDate - Date --End date of the project (30-Jun-19)
- Percentage - Number --Percentage of time consultant works at the client (100.00%)
As far as I've seen so far the max number of companies someone works for is two, but might be more.
I'm working in Oracle SQL and that's non negotiable with the project I'm working with.
I've started with PL/SQL not that long ago (I assume I need PL/SQL for this, because I can't figure out a way to do this only with Oracle SQL) and I haven't figured out a way to make this work. The splitting isn't the problem when there are one (obvious) or two different companies, but if there are more than two I don't know how to automate that. With languages like Java it wouldn't be a problem with array's and a for loop, but in PL/SQL for me it is still problem...
Below some code that I'm using for the splitting.
DECLARE
total varchar2(256) := 'Company B - 01-Jan-19 - 30-Jun-19 - 50.00% / Company C - 01-Jan-19 - 30-Jun-19 - 50.00%';
firstPart varchar2(128);
secondPart varchar2(128);
BEGIN
firstPart := SUBSTR(total, 0, (INSTR(total, '/')) -2);
secondPart := SUBSTR(total, (INSTR(total, '/')) +2);
END;
Is there a way to make this work with (potentially) infinite companies? If it's very difficult I can make an assumption that people work at no more than five companies, but I very much would like to have no maximum.
The second part is to split company name, start date, end date and percentage. I don't think it's that hard, but it might be relevant for the solution to the main problem. So if you have a solution for the main problem I can pick it up from there.
Feel free to ask any questions or to make me feel stupid because it's easy and I just haven't found the solution yet.
Thanks a lot in advance!