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?
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
So try this:
select concat(column1, column2)
Two ways to add a space:
select concat(column1, ' ', column2) select concat_ws(' ', column1, column2)
It’s work for me
SELECT CONCAT(column1, ' ' ,column2) AS newColumn;
Try this, it works for me
select (column1 || ' '|| column2) from table;
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)
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.
If you are Working On
SELECT column1 || column2 AS column3 FROM table;
If You Are Working On MySql Then:
SELECT Concat(column1 ,column2) AS column3 FROM table;
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
convert(varchar, column_name1) + (varchar, column_name)
SELECT Collumn1 + ‘ – ‘ + Collumn2 AS FROM FullName TableName