Skip to content
Astera Technology

Why does TRIM remove letters from the end of a report label?

Remove one exact prefix rather than a character set from both ends.

Download complete case notes and answer keys ↓

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.

Synthetic input: labels
idcode
1ABBA
2ABXAB
3CABA
Download editable CSV ↓
Complete CREATE and INSERT fixture
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

Misleading SELECT: reproduce the defect
SELECT id,trim(code,'AB') AS suffix FROM labels ORDER BY id;
Literal misleading result
idsuffix
1Empty string (length 0)
2X
3C
Download editable CSV ↓
Corrected SELECT
SELECT id,CASE WHEN substr(code,1,2)='AB' THEN substr(code,3) ELSE code END AS suffix FROM labels ORDER BY id;
Literal corrected result
idsuffix
1BA
2XAB
3CABA
Download editable CSV ↓

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.

Boundary change and corrected query
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;
Literal boundary result
idsuffix
1BA
2XAB
3CABA
4Empty string (length 0)
Download editable CSV ↓

What suffix should ABXAB retain?

Show the worked answer

XAB. Remove its leading AB once; the trailing AB is part of the suffix.

Related resources