1315 Votes

SQLite: String and Column Concatenation using SQLite

Info by SmartUser | Last update on 2021-03-08 | Created on 2013-05-26

Problem: In MySQL, it is possible to join the contents of several columns using CONCAT. It is even possible to include arbitrary strings to the concatenation. When trying CONCAT in SQLite, it only returns an error message: SQLite does not know the CONCAT command. Nevertheless, also with SQLite it is possible to join several strings and columns to display them together as a result.

Solution: The operator for concatenation in SQLite is ||. So, if we want to retrieve the columns "col1" and "col2" from the table "tab" and output their contents in only one column in the result, we can use the following query for this:

SELECT col1 || col2 FROM tab

With this, you can join as many columns as you like. Additionally, you can also add arbitrary other strings and characters:

SELECT 'Column 1: ' || col1 || ', Column 2: ' || col2 FROM tab

This creates an output of the form "Column 1: <contents of col1>, Column 2: <contents of col2>".

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.