Skip to main content

How to Concatenate Strings in SQLite with ||

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

To show a full name from separate first and last name columns, you join the two pieces of text into one. In SQLite, the operator for that is ||, two vertical bars. This guide builds full names from two columns, with real output from SQLite 3.54.1.

Syntax​

Syntax
SELECT <text 1> || <text 2> FROM <table>;
PartWhat it means
<text 1>, <text 2>The pieces to join: column names, text in single quotes, or numbers
The two vertical barsThe concatenation operator. It joins the piece on its left to the piece on its right, and you can chain as many pieces as you need

SQLite's documentation on operators: "The || operator is "concatenate" - it joins together the two strings of its operands."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as join_text.sql:

join_text.sql
CREATE TABLE people (
first_name TEXT NOT NULL,
last_name TEXT NOT NULL
);

INSERT INTO people (first_name, last_name) VALUES ('Ana', 'Silva');
INSERT INTO people (first_name, last_name) VALUES ('Ben', 'Okafor');

SELECT first_name || ' ' || last_name AS full_name FROM people;

The join_text.sql script in a macOS-style code window: a people table with first_name and last_name, two people, Ana Silva and Ben Okafor, and a SELECT that joins first_name, a space and last_name into full_name.

  • first_name || ' ' || last_name joins three pieces: the first name, a space in single quotes, and the last name.
  • AS full_name names the result column. Without it, the heading would be the whole expression.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table people.db ".read join_text.sql"

sqlite3 opens the database file people.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.

Output
+------------+
| full_name |
+------------+
| Ana Silva |
| Ben Okafor |
+------------+

The output in a terminal: a full_name column with Ana Silva and Ben Okafor.

Each row came back as one piece of text: the first name and the last name, with a space between them.

Common Mistakes​

MistakeWhat happensDo this instead
+ between two pieces of text, as in 'Ana' + 'Silva'It returned 0: + adds numbers, it doesn't join textJoin text with the two vertical bars
No space between the piecesThe first name joined straight to the last name gave AnaSilvaPut ' ' between them, as the script does

Troubleshooting​

SymptomLikely causeFix
A joined value is empty for some rowsOne of its pieces is NULL, and joining anything to NULL gives NULLUse coalesce() or concat_ws(), as the FAQ shows
Parse error near line 1 of join_text.sql: table people already existsThe script already ran on people.dbDelete people.db and run the script again

FAQ​

What happens when one piece is NULL?​

The whole result is NULL. In a people table with a middle_name column that was NULL for Ana, first_name || ' ' || middle_name || ' ' || last_name IS NULL returned 1. The documentation: "All operators generally evaluate to NULL when any operand is NULL, with specific exceptions as stated below." Wrapping the optional piece in coalesce(), which returns its first argument that isn't NULL, fixes it: first_name || ' ' || coalesce(middle_name || ' ', '') || last_name returned Ana Silva.

Does SQLite have a concat() function?​

Yes, concat() and concat_ws(), and both skip NULL pieces. With Ana's NULL middle name, concat(first_name, ' ', middle_name, ' ', last_name) skipped the NULL but kept both spaces around it, so her name came back with two spaces in the middle. concat_ws(' ', first_name, middle_name, last_name) returned Ana Silva. The documentation: "The concat_ws(SEP,...) function returns a string that is the concatenation of all non-null arguments beyond the first argument, using the text value of the first argument as a separator."

Can I join a number to text?​

Yes: 'Page ' || 412 returned Page 412, and typeof() reported the result as text.

Conclusion​

first_name || ' ' || last_name AS full_name joined each person's two names, with a space between them, into one column.

Additional Resources​