Add "" within concat

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Add "" within concat

Post by papa »

Hi,

Code: Select all

CONCAT_WS(' ', b.firstName, b.nickName, b.lastName)
Can I add a function within the concat which adds " " on b.nickName?

To get firstname "nick" lastname, and just not a separator between the names.

thanks!

edit:

And also if nickName == 0 it should not show. I guess that might not be possible in the query though.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Add "" within concat

Post by VladSun »

[sql]CONCAT_WS(' ', b.firstName, IF(b.nickName=0, NULL, concat('"', b.nickName, '"')), b.lastName)[/sql]

It seems strange that b.nickName is string, but you check against integer value (0).
Maybe NULL is more appropriate.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Add "" within concat

Post by papa »

Thanks once again!
Post Reply