You are on page 1of 8

MySQL combine two columns into one

column
Ask Question
Asked 6 years, 3 months ago
Active 2 years, 4 months ago
Viewed 586k times
104
16
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?

mysql sql

share improve this question follow


edited Apr 27 '15 at 3:29

shA.t
14.4k55 gold badges4343 silver badges8787 bronze badges
asked Mar 30 '14 at 3:17

aab
1,29522 gold badges1010 silver badges1414 bronze badges
 1
What database are you using? What are the types of the two columns? – Gordon Linoff Mar 30 '14 at 3:18
 possible duplicate of concatenate two database columns into one resultset column – Choudhury Saadmaan
Mahmid Mar 30 '14 at 8:39
 Check this out: stackoverflow.com/questions/6427764/… – Choudhury Saadmaan Mahmid Mar 30 '14 at 8:39
 Use top answer's solution but add a pipe. select concat(column1, '|', column2). It helps in excel
later – Cosmic Hawk Sep 30 '16 at 19:21
add a comment
9 Answers

ActiveOldestVotes

163
1
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)
share improve this answer follow
answered Mar 30 '14 at 3:20

Gordon Linoff
977k4141 gold badges416416 silver badges533533 bronze badges
 i am using sql server 2005 and it is giving me error as Incorrect syntax near ')'. – hud Dec 29 '15
at 7:37
 5
@coder . . . This question is tagged "mysql". If you have a question about SQL Server, then ask it as a
question, not in a comment. – Gordon Linoff Dec 29 '15 at 23:13
 Works for T-SQL as well, excellent and simple solution. Reduces two columns into one. – Ryan
Battistone Feb 15 '19 at 17:55
add a comment
22
Try this, it works for me

select (column1 || ' '|| column2) from table;


share improve this answer follow
edited Nov 5 '14 at 8:30

Vimalnath
6,11222 gold badges2222 silver badges4444 bronze badges
answered Nov 5 '14 at 8:21

Oreniwa Babatunde
22122 silver badges22 bronze badges
 1
This looks like Oracle. – coburne Apr 17 '15 at 16:47
 This worked for combining multiple (more than 2) columns too. Thx – Kayathiri Jun 1 '16 at 10:35
 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 '19 at 10:09
add a comment
14
It's work for me

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


share improve this answer follow
answered Jul 9 '15 at 8:54

sk juli kaka
42011 gold badge55 silver badges1919 bronze badges
add a comment
5
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)


share improve this answer follow
answered Sep 24 '14 at 2:00

mattk
19322 silver badges1111 bronze badges
add a comment
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.
share improve this answer follow
answered Jan 7 '15 at 19:17

2NinerRomeo
2,25344 gold badges2020 silver badges3535 bronze badges
add a comment
3
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;
share improve this answer follow
edited Aug 23 '16 at 11:34

Moumit
3,72966 gold badges3333 silver badges4141 bronze badges
answered Aug 23 '16 at 11:01

Balaji Dongare
2755 bronze badges
add a comment
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...

share improve this answer follow


answered Dec 20 '14 at 13:24

Sunil Acharya
92333 gold badges1414 silver badges3232 bronze badges
add a comment
0
convert(varchar, column_name1) + (varchar, column_name)
share improve this answer follow
edited Sep 18 '15 at 7:29
Matt
67.1k2020 gold badges135135 silver badges170170 bronze badges
answered Sep 17 '15 at 11:13

Ritesh Yadav
13511 gold badge11 silver badge66 bronze badges
add a comment
0
SELECT Collumn1 + ' - ' + Collumn2 AS 'FullName' FROM TableName
share improve this answer follow
edited Feb 11 '18 at 10:57

Onitech
7811 silver badge1111 bronze badges
answered Aug 9 '16 at 6:19

Ravin
111 bronze badge
 Simple way to combine – Ravin Aug 9 '16 at 6:20
add a comment
Highly active question. Earn 10 reputation in order to answer this question. The reputation requirement helps
protect this question from spam and non-answer activity.
Not the answer you're looking for? Browse other questions tagged mysql sql or ask your own
question.
The Overflow Blog
 The Overflow #27: A simulation
 Does scrum ruin great engineers or are you doing it wrong?
Featured on Meta

We're switching to CommonMark


New post lock available on meta sites: Policy Lock


Feature test: Thank you reaction



2020 Moderator Election Q&A - Question Collection

Linked

45
concatenate two database columns into one resultset column

24
Case when a value is different to other value, SQL Server

1
Total number of yes and no values from mysql database table row

2
vertical merge of two columns into a single one

0
SQL many to many subquery

0
How do I update a VARCHAR column so that it contains the contents of two other VARCHAR columns, separated
by a space?

-2
How to convert 40302402 into 40/302/4/02 with the help of Query

-1
How to search for string in a co-joined database column

Related

2779
Add a column with a default value to an existing table in SQL Server

1217
Can I concatenate multiple MySQL rows into one field?

2696
Should I use the datetime or timestamp data type in MySQL?

740
SQL exclude a column using SELECT * [except columnA] FROM tableA?

518
How to do a regular expression replace in MySQL?

1252
How to reset AUTO_INCREMENT in MySQL?

822
MySQL error code: 1175 during UPDATE in MySQL Workbench

2055
How do I import an SQL file using the command line in MySQL?

Hot Network Questions


 What wavelengths are used practically in optical trapping?
 Should I include proof of my extenuating circumstances when applying for a PhD?
 My botched ordering task
 How do I get better at tasting?
 How did dusty deck Fortran handle overflow?
 Mini 8 queens puzzle
 What is the cutting edge for open-source Force-Field generation?
 Should non-priority technical debt tickets be pruned from backlog?
 Gamma PnL from Itô's Lemma derivation
 Does the motto “Don’t tread on me” have a specific political color?
 Is Python a viable language to do statistical analysis in?
 Is there value to learning openings as a new player?
 What is 吗 doing in 你有什么事吗?
 Why pkexec does not launch Qt-based application as root user?
 Why do Hogwarts owls almost always arrive at breakfast?
 Split paper into two, one was accepted, is it fine to negotiate the rejected paper to be accepted?
 4k reputation special: "I hate square numbers!"
 Is it always helpful to add "external" datasets to the training dataset?
 Measuring disk usage of specific file types per each directory (recursively, as a demo for 'du --include')
 Are the non-solvable groups of abundant order?
 How did Cochrane obtain dilithium for the first warp flight?
 Did Northern troops attempt to re-enslave African Americans in Southern plantations during the Civil War?
 What are some examples of famous scholars with low h-index?
 Old washing machine takes long and longer to do a cycle

Question feed

STACK OVERFLOW
 Questions
 Jobs
 Developer Jobs Directory
 Salary Calculator
 Help
 Mobile
 Disable Responsiveness
PRODUCTS
 Teams
 Talent
 Advertising
 Enterprise
COMPANY
 About
 Press
 Work Here
 Legal
 Privacy Policy
 Contact Us
STACK EXCHANGE
NETWORK
 Technology
 Life / Arts
 Culture / Recreation
 Science
 Other
 Blog
 Facebook
 Twitter
 LinkedIn
 Instagram

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2020.6.29.37151

You might also like