It's messy, but based on the answer to this question How to get the last index of a substring in SQLite?
SQLFiddle with live example here This may not be as efficient as it can be. It's ugly. That's for sure!
As for a variable number of dashes, well, if it's insignificant how many dashes there are, you could execute a query first to replace all double dashes '--'
with single dashes '-'
. Run that multiple times if necessary if there are occurrences of more than two dashes.
SQLite Solution
CREATE TABLE `parts` (
`partname` TEXT,
`postfix1` TEXT,
`postfix2` TEXT,
`postfix3` TEXT
);
INSERT INTO `parts`
(`partname`)
VALUES
('aaaa-bbbb-ddddd-ccc-ggggggg'),
('aaaa-bbbbb-dddd-cccc-gggggg'),
('aaaa-bbbbbb-ddd-ccccc-ggggg'),
('aaaa-bbbbbbb-dd-cccccc-gggg')
;
UPDATE `parts`
SET `postfix1` = CASE
WHEN instr(`partname`, '-') > 0 THEN
replace(`partname`, rtrim(`partname`, replace(`partname`, '-', '')), '')
ELSE NULL
END;
UPDATE `parts`
SET `postfix3` = CASE
WHEN instr(`partname`, '-') > 0 THEN
replace(`partname`, rtrim(
rtrim( rtrim(`partname`, replace(`partname`, '-', '')), '-'),
replace(`partname`, '-', '')), '')
ELSE NULL
END;
UPDATE `parts`
SET `postfix2` = CASE
WHEN instr(`partname`, '-') > 0 THEN
replace(
replace(`partname`,
'-' || replace(`partname`, rtrim( rtrim( rtrim(`partname`, replace(`partname`, '-', '')), '-'), replace(`partname`, '-', '')), ''),
''),
rtrim(
replace(`partname`,
'-' || replace(`partname`, rtrim( rtrim( rtrim(`partname`, replace(`partname`, '-', '')), '-'), replace(`partname`, '-', '')), ''),
''),
replace(
replace(`partname`,
'-' || replace(`partname`, rtrim( rtrim( rtrim(`partname`, replace(`partname`, '-', '')), '-'), replace(`partname`, '-', '')), ''),
''),
'-', '')
),
'')
ELSE NULL
END;
SELECT * FROM `parts`;
Gives the results
partname postfix1 postfix2 postfix3
--------------------------------------------------------------------
"aaaa-bbbb-ddddd-ccc-ggggggg" "ggggggg" "ddddd" "ccc-ggggggg"
"aaaa-bbbbb-dddd-cccc-gggggg" "gggggg" "dddd" "cccc-gggggg"
"aaaa-bbbbbb-ddd-ccccc-ggggg" "ggggg" "ddd" "ccccc-ggggg"
"aaaa-bbbbbbb-dd-cccccc-gggg" "gggg" "dd" "cccccc-gggg"