Prepare the report input
Original export derives a suffix by removing one leading AB only. trim(code,'AB') instead strips any A or B from both ends. Use a prefix test and substr; retain nonmatching codes unchanged.
Every record and report rule below is original and fictional. Reproduce the setup in a fresh SQLite test database; input previews include every table in this small fixture.
| id | code |
|---|---|
| 1 | ABBA |
| 2 | ABXAB |
| 3 | CABA |
CREATE TABLE labels(id INTEGER PRIMARY KEY,code TEXT NOT NULL); INSERT INTO labels VALUES(1,'ABBA'),(2,'ABXAB'),(3,'CABA');Compare misleading and corrected results
SELECT id,trim(code,'AB') AS suffix FROM labels ORDER BY id;| id | suffix |
|---|---|
| 1 | Empty string (length 0) |
| 2 | X |
| 3 | C |
SELECT id,CASE WHEN substr(code,1,2)='AB' THEN substr(code,3) ELSE code END AS suffix FROM labels ORDER BY id;| id | suffix |
|---|---|
| 1 | BA |
| 2 | XAB |
| 3 | CABA |
ABBA should become BA, ABXAB should become XAB, and CABA stays CABA. The misleading values empty/X/C demonstrate that TRIM is not a prefix-removal operation.
Check a fresh boundary case
The exact code AB produces an empty suffix after its sole prefix is removed.
Recreate the original setup in another fresh database, then apply the change and corrected query below. For an empty result, the table displays a labelled empty-result marker rather than a data record.
INSERT INTO labels VALUES(4,'AB');
SELECT id,CASE WHEN substr(code,1,2)='AB' THEN substr(code,3) ELSE code END AS suffix FROM labels ORDER BY id;| id | suffix |
|---|---|
| 1 | BA |
| 2 | XAB |
| 3 | CABA |
| 4 | Empty string (length 0) |
What suffix should ABXAB retain?
Show the worked answer
XAB. Remove its leading AB once; the trailing AB is part of the suffix.