MySQL merges two columns into one column

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

This is what I 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; 

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

+48
sql mysql
Mar 30 '14 at 3:17
source share
10 answers

I assume that you are using MySQL, where the + operator does the addition as well as silently converting values ​​to numbers. If the value does not start with a digit, then the converted value is 0 .

So try the following:

 select concat(column1, column2) 

Two ways to add a space:

 select concat(column1, ' ', column2) select concat_ws(' ', column1, column2) 
+73
Mar 30 '14 at 3:20
source share

It works for me

 SELECT CONCAT(column1, ' ' ,column2) AS newColumn; 
+9
Jul 09 '15 at 8:54
source share

Try it works for me

 select (column1 || ' '|| column2) from table; 
+8
Nov 05 '14 at 8:21
source share

This is the only solution that will work for me when I need the space between the columns to be merged.

 select concat(concat(column1,' '), column2) 
+3
Sep 24 '14 at 2:00
source share

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

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

My results show that concatenating strings with NULL results in NULL . I get alternative value in these cases.

+3
Jan 07 '15 at 7:17
source share

If you are running 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
Aug 23 '16 at 11:01
source share

I used this method and its best forever. In this code, null is also processed

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

Try it...

+1
Dec 20 '14 at 13:24
source share
 convert(varchar, column_name1) + (varchar, column_name) 
0
Sep 17 '15 at 11:13
source share

SELECT Collumn1 + '-' + Collumn2 AS FROM FullName TableName

0
Aug 09 '16 at 6:19 06:19
source share

- for example: columns with sets of numbers .... - When combining columns with numbers, you will need to drop these columns from the #data type into a row - another sql will add these numbers together to the same columns / result.

select a throw (shipperid as varchar) + '| '+ cast (empid as varchar) from sales.Orders

-2
Oct 31 '17 at 15:49
source share



All Articles