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

Select statement with a NULL

P: n/a
I have forgotten how to do this (At least I think I knew how)

I have SELECT CONCAT(A, ' ', B) as C ........

If A or B is NULL this select fails. What do I need to do so it won't
fail...

Thanks...
Apr 29 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Bruce A. Julseth" <br*************@bellsouth.netwrote in message
news:f1**********@aioe.org...
>I have forgotten how to do this (At least I think I knew how)

I have SELECT CONCAT(A, ' ', B) as C ........

If A or B is NULL this select fails. What do I need to do so it won't
fail...

Thanks...
Never Mind. I found it on the internet...

SELECT CONCAT_WS('', A, ' ', B) as C .....

Thanks....
Apr 29 '07 #2

P: n/a
Bruce A. Julseth wrote:
SELECT CONCAT_WS('', A, ' ', B) as C .....
That looks like some MySQL-specific voodoo. The standards-compliant way of
doing this is:

SELECT COALESCE(a,'')||' '||COALESCE(b,'') AS c

However, neither MySQL nor Microsoft SQL server support the concatenation
operator (||) correctly. Thus if you want to write code which is broadly
compatible on a variety of databases, one is forced to simply:

SELECT a, b

and then concatenate them in PHP itself:

if (strlen($row['a']) * strlen($row['b']))
$row['c'] = "{$row['a']} {$row['b']}";
else
$row['c'] = $row['a'].$row['b'];

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.