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]
The question, the details, the next step.
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
COALESCEorCASEto 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.
- [S1] SQLite SQL Language Expressions: concatenation and NULL sqlite.org · Checked
