143

I'm trying to find a way to combine two columns into one, but keep getting the value '0' in the column instead to the combination of the words.

These are what I've tried as well as others:

SELECT column1 + column2 AS column3
FROM table;

SELECT column1 || column2 AS column3
FROM table;

SELECT column1 + ' ' + column2 AS column3
FROM table;

Could someone please let me know what I'm doing wrong?

4

12 Answers 12

229

My guess is that you are using MySQL where the + operator does addition, along with silent conversion of the values to numbers. If a value does not start with a digit, then the converted value is 0.

So try this:

select concat(column1, column2)

Two ways to add a space:

select concat(column1, ' ', column2)
select concat_ws(' ', column1, column2)
3
  • i am using sql server 2005 and it is giving me error as Incorrect syntax near ')'.
    – hud
    Dec 29, 2015 at 7:37
  • 6
    @coder . . . This question is tagged "mysql". If you have a question about SQL Server, then ask it as a question, not in a comment. Dec 29, 2015 at 23:13
  • Works for T-SQL as well, excellent and simple solution. Reduces two columns into one. Feb 15, 2019 at 17:55
34

Try this, it works for me

select (column1 || ' '|| column2) from table;
3
  • 4
    This looks like Oracle.
    – coburne
    Apr 17, 2015 at 16:47
  • This worked for combining multiple (more than 2) columns too. Thx
    – Kayathiri
    Jun 1, 2016 at 10:35
  • 1
    The SQL standard provides the CONCAT() function to concatenate two strings into a single string. SQLite, however, does not support the CONCAT() function. Instead, it uses the concatenate operator (||) to join two strings into one.
    – PaulH
    Oct 28, 2019 at 10:09
18

It's work for me

SELECT CONCAT(column1, ' ' ,column2) AS newColumn;
7

This is the only solution that would work for me, when I required a space in between the columns being merged.

select concat(concat(column1,' '), column2)
6

If you are Working On Oracle Then:

SELECT column1 || column2 AS column3
FROM table;

OR

If You Are Working On MySql Then:

SELECT Concat(column1 ,column2) AS column3
FROM table;
3

For the MySQL fans out there, I like the IFNULL() function. Other answers here suggest similar functionality with the ISNULL() function in some implementations. In my situation, I have a column of descriptions which is NOT NULL, and a column of serial numbers which may be NULL This is how I combined them into one column:

SELECT CONCAT(description,IFNULL(' SN: ', serial_number),'')) FROM my_table;

My results suggest that the results of concatenating a string with NULL results in a NULL. I have been getting the alternative value in those cases.

1

I have used this way and Its a best forever. In this code null also handled

SELECT Title,
FirstName,
lastName, 
ISNULL(Title,'') + ' ' + ISNULL(FirstName,'') + ' ' + ISNULL(LastName,'') as FullName 
FROM Customer

Try this...

1

table:

---------------------
| column1 | column2 |
---------------------
|   abc   |   xyz   |
---------------------

In Oracle:

SELECT column1 || column2 AS column3
FROM table_name;

Output:

table:

---------------------
| column3           |
---------------------
| abcxyz            |
---------------------

If you want to put ',' or '.' or any string within two column data then you may use:

SELECT column1 || '.' || column2 AS column3
FROM table_name;

Output:

table:

---------------------
| column3           |
---------------------
| abc.xyz           |
---------------------
0
convert(varchar, column_name1) + (varchar, column_name)
0
0
SELECT Column1 + ' - ' + Column2 AS 'FullName' FROM TableName                              
0
0
SELECT   CONVERT (nvarchar (10), Month(NextDate))+'-'+CONVERT (nvarchar (10), Year(NextDate)) as MonthOfYear, COUNT(CaseNo) as CountDisposal
FROM dbo.Main_Cause_List 
WHERE (DisposalState = 'Dispossed-off') 
GROUP BY Month(NextDate),Year(NextDate);
1
  • 1
    Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to Answer. This is particularly important when answering old questions (this one is over 7 years old) with existing answers. How does this answer improve over what's already here?
    – Chris
    May 2, 2021 at 12:21
0

try to use coalesce() and concat() to combine columns in the SQL query.

Assume that you have 4 columns (id, name, phone_number, country_code) in a user table and you want to print phone number in this format: +countrycodephonenumber

Eg: 1, vishnu, 9961907453, 91 will return phone number as +919961907453.

You can use the following query to get the above result.

select coalesce(concat('+', country_code, phone_number)) from user;

Not the answer you're looking for? Browse other questions tagged or ask your own question.