Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I combine text columns into one display value in SQLite?

Quick answer

Use SQLite’s || operator to join text values, adding a space as another operand: first_name || ' ' || last_name AS display_name. If either name is NULL, that expression returns NULL rather than skipping the missing value. To retain whichever name exists, handle NULL explicitly with COALESCE or a CASE expression and choose what the display should show when both parts are missing. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a contacts table with first_name and last_name. What query combines them into one value I can display?

Geez

Join the columns with SQLite’s || operator, and put a space between them:

SELECT
  first_name || ' ' || last_name AS display_name
FROM contacts;

For example, suppose the table contains these invented rows:

first_name | last_name
Mina       | Bekele
Jon        | Park

The query returns:

display_name
Mina Bekele
Jon Park

The AS display_name part labels the combined result; it does not change either stored column. SQLite’s || joins its operands as text. [S1]

Follow-up question

What if a contact has a first name but no last name? Will SQLite just leave out the missing part?

Geez

No. In SQLite, an operand that is NULL makes the concatenation result NULL, so first_name || ' ' || last_name returns NULL if either name is NULL. It does not automatically retain the available name. [S1]

For instance, with first_name = 'Ari' and last_name = NULL, the basic expression produces NULL—not Ari. Decide how you want incomplete names displayed, then handle missing values explicitly.

Follow-up question

Can you show a version that keeps whichever name is present, without adding a stray space?

Geez

Use a CASE expression to choose the appropriate result for each situation:

SELECT
  CASE
    WHEN first_name IS NULL THEN COALESCE(last_name, '')
    WHEN last_name IS NULL THEN first_name
    ELSE first_name || ' ' || last_name
  END AS display_name
FROM contacts;

With these invented rows:

first_name | last_name | display_name
Ari        | NULL      | Ari
NULL       | Chen      | Chen
Mina       | Bekele    | Mina Bekele
NULL       | NULL      |

Here, both missing names display as an empty value. That is a choice made by this example, not a universal rule: if your application needs a label such as Name unavailable, use that value in the both-missing branch instead. SQLite’s documentation recommends explicitly handling NULL, for example with COALESCE or CASE; it does not prescribe your display policy. [S1]

Follow-up question

How should I check this before using the expression in my application?

Geez

Try the query against a few rows that cover complete, first-name-only, last-name-only, and both-missing cases. Confirm the output matches your display choice, including what an empty name should look like. If the result is for a screen or report, you can also give the expression a clear alias such as display_name and select only the columns you need.

The example assumes the columns are named first_name and last_name and that a single space is the desired separator. Replace those names or the separator to suit your schema. The cited SQLite reference establishes how || and NULL interact and suggests explicit handling; it does not establish your application’s preferred label or formatting.

A practical checklist

  • Use || with the separator you want, such as a space.

  • Check whether either source column can be NULL.

  • Choose the display for first-name-only, last-name-only, and both-missing rows.

  • Use COALESCE or CASE to implement that choice explicitly.

  • Test the expression with representative rows and give the result a clear alias.

A few more useful answers

Does `||` change the original name columns?

No. It creates a combined result for the query; the example only selects that value and does not update the stored columns.

What should I display when both name columns are NULL?

That depends on your application. The example returns an empty value, but you could choose a label such as Name unavailable by changing the relevant CASE branch.

Can I use a different separator?

Yes. Replace the space literal in the concatenation with the separator you want, such as a comma and space, and check how it should behave for incomplete names.

Sources

References used for this guide.

  1. [S1] SQLite SQL Language Expressions: concatenation and NULL
    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