1

I want to display two columns of different types or two column data of same type that will be displayed in one column.

The types are date + time, or varchar + varchar etc

I know how to concat strings (add a string to one column) but can't seem to do it for two columns data.

Say I want to display two columns both varchar type, fname + lname = Ajay Punja

Or

Lname + DOB = Punja 01/01/2001

I tried using single and double pipes, plus signs etc but always returns 0.

Is it because I need to convert two different data types into one matching data type? But, both varchar types returns 0.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Ajay Punja
  • 101
  • 4
  • 11
  • 1
    [`CONCAT()`](http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat) is how concatenations are done in MySQL. – Michael Berkowski Dec 01 '12 at 14:09
  • possible duplicate of [Can MySQL concatenate strings with ||](http://stackoverflow.com/questions/8212192/can-mysql-concatenate-strings-with) – Michael Berkowski Dec 01 '12 at 14:10

2 Answers2

2

I think it will help you.

SELECT CONCAT(2, ' test') as result form table_name;

It is also possible to convert a number to a string explicitly using the CAST() function. Conversion occurs implicitly with the CONCAT() function because it expects string arguments. e.g.

SELECT 38.8, CAST(38.8 AS CHAR);
mychalvlcek
  • 3,956
  • 1
  • 19
  • 34
Ram Patidar
  • 666
  • 1
  • 6
  • 16
1

My answer thanks to everyone here :)

SELECT CONCAT(fname, ' ', DOB) as Results FROM Person;
SELECT CONCAT(fname, ' ', lname) as Results FROM Person;

Now I understand how to use CONCAT properly. Before I thought for each bracket it must contain only one attribute (or string) and then CONCAT it with enough bracket of data, but clearly that's wrong.

Wrong example (fail attempt):

SELECT CONCAT(fname, '') + (' ', lname)) as Results FROM Person;

Ajay Punja
  • 101
  • 4
  • 11