By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

Problem joining select strings

P: n/a
Joe
I've got 3 columns in a mysql database that I am trying to join. I'm using
CONCAT(), but it's giving me NULL results. This has worked for me before ...
What am I doing wrong?

Here's a straight select:

mysql> select peopleID, firstName, middleInit, lastname
-> from people;
+----------+-----------+------------+------------+
| peopleID | firstName | middleInit | lastname |
+----------+-----------+------------+------------+
| 1 | Joe | NULL | Shockey |
| 3 | John | NULL | Doe |
Here my select with the concat. It's not concatenating the string for some
reason:

mysql> select peopleID, CONCAT(firstName, ' ', middleInit, ' ', lastname) AS
person
-> from people;
+----------+--------+
| peopleID | person |
+----------+--------+
| 1 | NULL |
Can anyone tell me what I'm doing wrong? Should I be using a different
method for joining these srings?

BTW - here's my table structure:

mysql> show columns from people;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| peopleID | int(10) | | PRI | NULL | auto_increment |
| firstName | varchar(50) | YES | | NULL | |
| middleInit | varchar(50) | YES | | NULL | |
| lastName | varchar(50) | YES | | NULL | |
| phone | varchar(50) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Joe wrote:
I've got 3 columns in a mysql database that I am trying to join. I'm using
CONCAT(), but it's giving me NULL results. This has worked for me before ...
What am I doing wrong?


CONCAT(str1,str2,...)
Returns the string that results from concatenating the arguments.
Returns NULL if any argument is NULL. May have more than 2 arguments. A
numeric argument is converted to the equivalent string form:

mysql> SELECT CONCAT('My', 'S', 'QL');
-> 'MySQL'
mysql> SELECT CONCAT('My', NULL, 'QL');
-> NULL
mysql> SELECT CONCAT(14.3);
-> '14.3'

CONCAT_WS(separator, str1, str2,...)
CONCAT_WS() stands for CONCAT With Separator and is a special form
of CONCAT(). The first argument is the separator for the rest of the
arguments. The separator can be a string as well as the rest of the
arguments. If the separator is NULL, the result will be NULL. The
function will skip any NULL values after the separator argument. The
separator will be added between the strings to be concatenated:

mysql> SELECT CONCAT_WS(",","First name","Second name","Last Name");
-> 'First name,Second name,Last Name'
mysql> SELECT CONCAT_WS(",","First name",NULL,"Last Name");
-> 'First name,Last Name'

--
Sugapablo
------------------------------------
http://www.sugapablo.com <--music
http://www.sugapablo.net <--personal

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.