Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I remove extra spaces before comparing text values?

Quick answer

In SQLite, compare trim(label) with the value you want to match: trim(label) = 'North'. trim() removes spaces from both ends for that comparison, so ' North ' matches while a raw comparison of label = 'North' does not. The expression does not change the stored value. This behavior is documented for SQLite; do not assume it applies to other database products or to every kind of whitespace. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have text values that sometimes have spaces before or after the word. How can I compare them as though those spaces were not there?

Geez

In SQLite, put trim() around the column in the comparison:

trim(label) = 'North'

For example, if label contains ' North ', the trimmed comparison matches 'North'. A comparison without trim(), such as label = 'North', compares the stored value as-is and does not match the spaced value. SQLite documents trim(X) as removing spaces from both ends of X. [S1]

This changes how the comparison is made, not the value saved in the table. It is useful when you need to match values despite leading or trailing spaces but are not ready to update the stored data.

Follow-up question

Can you show both results side by side with a small example, so I can check what the query is doing?

Geez

Here is a self-contained SQLite example using invented labels:

WITH samples(label) AS (
  VALUES (' North '), ('North')
)
SELECT
  label,
  label = 'North' AS raw_match,
  trim(label) = 'North' AS trimmed_match
FROM samples;

The comparison results are:

label       raw_match  trimmed_match
' North '   0          1
'North'     1          1

The spaces around North in the first label are part of the example value. Here, 0 means the comparison is false and 1 means it is true. The first row shows the difference: the raw comparison does not match, while the trimmed comparison does. The second row matches either way. This illustrates SQLite’s documented function and comparison behavior. [S1]

Follow-up question

What if I want to see the normalized text itself, or use the same approach in a filter?

Geez

Select trim(label) to display the value with its end spaces removed, or put the expression in a WHERE condition to filter rows:

SELECT label, trim(label) AS normalized_label
FROM places;
SELECT label
FROM places
WHERE trim(label) = 'North';

In these examples, places and label are placeholders for your table and column names. The first query shows the original and comparison-ready text side by side; the second returns rows whose trimmed label equals 'North'.

One useful limit: the documentation describes trim(X) as removing spaces from both ends. It does not establish that this handles tabs or every other whitespace character, or that other database products behave the same way. If you need to handle a different character or database, check that product’s documentation and test with representative values. [S1]

Follow-up question

I noticed there is also an optional second argument to trim(). Should I use it for a particular word or pattern?

Geez

Use the one-argument form for ordinary end spaces. SQLite’s optional second argument is a set of characters to remove from both ends—not a whole substring to strip. For example, trim(label, 'xy') removes any x or y characters at the ends; it does not remove the exact sequence 'xy' as one unit. [S1]

That distinction matters because the result may differ from what a “remove this entire prefix or suffix” operation would do. For the original space-comparison task, trim(label) = 'North' is the straightforward expression. Try the comparison query on a few invented boundary cases before relying on a more specialized second argument.

A practical checklist

  • Confirm that the database is SQLite before relying on this behavior.

  • Use trim(column) = 'target' when comparing while ignoring end spaces.

  • Run raw and trimmed comparisons side by side on representative values.

  • Check separately if tabs or other whitespace characters may occur.

A few more useful answers

Does `trim(label)` remove spaces inside the text too?

No. The documented behavior is to remove spaces from both ends of the string, so spaces between words remain. [S1]

Does comparing with `trim(label)` permanently clean the stored value?

No. It applies trimming to the comparison expression; it does not update the stored column.

Does `trim(label, 'North')` remove the exact word `North`?

No. SQLite treats the second argument as characters to remove from either end, not as a whole substring. [S1]

Sources

References used for this guide.

  1. [S1] SQLite Built-In Scalar SQL Functions: trim
    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