SQLite concat() Function

Savigo
You organize your data. Now organize your savings.
Set savings goals, track your progress, and stay on course.
Get Savigo for iPhone 

Summary: in this tutorial, you will learn how to use the concat() function and concatenation operator (||) and to concatenate two strings into a string.

Introduction to SQLite concat() function #

In SQLite, the concat() function allows you to concatenate multiple strings into a single string. Here’s the syntax of the concat() function:

concat(s1, s2, ...);

In this syntax:

  • s1, s2, .. are the strings that you want to concatenate.

The concat() function returns a string that is the result of concatenating the input strings.

The concat() function treats NULL as an empty string. If all the input strings are NULLs, the concat() function returns an empty string.

SQLite concat() function examples #

Let’s take some examples of using the concat() function.

1) Basic concat() function example #

The following example uses the concat() function to concatenate two strings into a string:

SELECT concat('SQLite', ' Concat') result;

Output:

result
-------------
SQLite Concat

The following statement uses the concat() function to concatenate three strings into a single string:

SELECT concat('SQLite',' ','Concat') result;

Output:

result
-------------
SQLite Concat

2) Using concat() function with table data #

We’ll use the employees table from the sample database.

Image

The following example uses the concat() function to concatenate the first name, space, and last name of employees to form full names:

SELECT
    concat(FirstName,' ', LastName) FullName
FROM
    Employees
ORDER BY
    FullName;

Output:

FullName
----------------
Andrew Adams
Jane Peacock
Laura Callahan
Margaret Park
Michael Mitchell
Nancy Edwards
Robert King
Steve Johnson

SQLite concatenation operator (||) #

Besides the concat() function, SQLite provides the concatenation operator (||) to join two strings into one string.

Here’s the syntax of the concatenation operator:

s1 || s2

It is possible to use multiple concatenation operators in the same expression:

s1 || s2 || s3

The following example shows how to concatenate two literal strings into one:

SELECT 'SQLite ' || 'concat' result;

Here is the output:

result
-------------
SQLite concat

The following example illustrates how to use two concatenation operators in the same expression:

SELECT 'SQLite' || ' ' || 'concat' result;

Output:

result
-------------
SQLite concat

This example shows how to construct the full name of employees from the first name, space, and last name:

SELECT
    FirstName || ' ' || LastName AS FullName
FROM
    Employees
ORDER BY
    FullName;

Output:

FullName
----------------
Andrew Adams
Jane Peacock
Laura Callahan
Margaret Park
Michael Mitchell
Nancy Edwards
Robert King
Steve Johnson

Summary #

  • Use the concat() function to concatenate multiple strings into a single string.
  • Use the concatenation operator (||) to join multiple strings into one string.

Was this tutorial helpful?