How do I extract part of a text value with SQLite substring functions?
Quick answer
Use substr(value, start, length) to extract text from a SQLite value. Positions start at 1, so substr('SKU-482', 5, 3) returns 482. Leave out the length to take the rest of the value. A negative start counts from the right. These rules describe SQLite’s built-in function; text positions count UTF codepoints, while BLOB positions count bytes. [S1]
The question, the details, the next step.
A practical checklist
Write down a representative value and number its characters starting at 1.
Choose a positive start from the left or a negative start from the right.
Set the length for a fixed-size segment, or omit it for the remainder.
Run the expression on several representative values and inspect the selected text.
Check whether the input is text or a BLOB if position counting matters.
A few more useful answers
Does SQLite substring indexing start at zero?
No. SQLite’s documented substr() positions are one-based, so the first character is at position 1. [S1]
What does `substr(value, start)` do when I leave out the length?
It returns the remainder of the value beginning at the specified start position. [S1]
Does a negative start mean the same thing as a negative length?
No. A negative start counts the starting position from the right; a negative length selects characters preceding the starting position. [S1]
Sources
References used for this guide.
- [S1] SQLite Built-In Scalar SQL Functions: substr sqlite.org · Checked
