Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have product codes such as SKU-482. How do I extract just the three digits after the hyphen in SQLite?

Geez

Use substr() with the starting position and the number of characters to return:

SELECT substr('SKU-482', 5, 3) AS segment;

The result is 482. SQLite counts text positions from 1, not 0: S is position 1, the hyphen is position 4, and the first digit is position 5. The third argument, 3, asks for three characters. [S1]

For several invented product codes, you can use the same expression in a query:

WITH items(code) AS (
  VALUES ('SKU-482'), ('BOX-731')
)
SELECT code, substr(code, 5, 3) AS segment
FROM items;

The output is:

code     | segment
SKU-482  | 482
BOX-731  | 731

The position works for these examples because both codes have the same prefix length. If your actual values have different layouts, the same fixed position might not select the digits you intend.

Follow-up question

What if I want everything after the hyphen, and how can I avoid getting the position wrong?

Geez

You can omit the length to return the remainder of the value from the chosen position:

SELECT substr('SKU-482', 5) AS remainder;

That returns 482. The start is still position 5; leaving out the third argument does not change how positions are counted. [S1]

To check an extraction, write the value out and number its characters before choosing the start:

Value:    S K U - 4 8 2
Position: 1 2 3 4 5 6 7

Then verify both the start and length against a sample whose format you know. If you need to extract a fixed-width section, include the length. If you need the rest of the value, omit it. This avoids silently relying on a different starting position than the code actually uses.

Follow-up question

Can I count the starting position from the end instead?

Geez

Yes. A negative start counts from the right. For example:

SELECT substr('SKU-482', -3) AS last_three;

This returns 482: the start is three characters from the right, and omitting the length returns the remainder from there. [S1]

Use this when the part you need is consistently at the end, even if the text before it varies. It is not a way to identify a separator or validate a product-code format: it simply counts characters from the right. Test it against representative values, especially if some values may have a different ending or length.

Follow-up question

Are there any indexing details I should keep in mind when I use this with real text values?

Geez

For SQLite’s built-in substr(), text positions count UTF codepoints; when the input is a BLOB, positions count bytes. [S1] So a position you chose by counting visible symbols may not match what the function counts in every kind of text. Check the actual input type and test the expression on representative values. The documented behavior also says a negative length selects characters preceding the starting position; that differs from using a negative start, which counts the start from the right. [S1]

A reusable pattern is:

SELECT substr(column_name, start_position, character_count)
FROM table_name;

Replace the placeholders with your column, a one-based start position, and the desired length. Omit the third argument when you want the remainder. These examples explain extraction mechanics; they do not establish whether a particular product-code format is consistent or whether an extracted segment is valid.

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.

  1. [S1] SQLite Built-In Scalar SQL Functions: substr
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work