11

I need to CONCAT two fields, if they both exist and add parentheses around the second field. Otherwise I just need to list the first field.

Here's the simple version:

SELECT id, CONCAT(name,' (',nickname,')') as name FROM user;

As long as name and nickname both exist you should get results like:

1 | Adam (Alpha Dog)
2 | Bob (Bobby)
3 | Charles (Charlie)

But if there is no nickname it just skips the whole thing as null. For example: id 4, name Doug, nickname null gives the result:

4 | null

What I'd like to see is it to list just the name... like this:

4 | Doug

So I started looking at CONCAT IFNULL. But I'm having a hard time getting it right. Can you help?

Here are a few examples of things I've tried:

SELECT id, CONCAT(IFNULL(name, ' (', nickname, ')', name) as name FROM user;
SELECT id, CONCAT(name, IFNULL(' (', nickname, ')')) as name FROM user;
2
  • 1
    IFNULL( CONCAT( '(', nickname, ')' ), '' )
    – Sir Rufo
    Dec 13, 2012 at 19:16
  • 1
    Something to point out that threw me off at first. When nickname is null then CONCAT('(',nickname,')') will also return null. Feb 7, 2013 at 11:47

4 Answers 4

22

You could use CONCAT_WS which skips null strings:

SELECT id, CONCAT_WS(' ', name, concat('(',nickname,')')) as name FROM user;
3
  • if nickname is NULL you will get NULL as result
    – Sir Rufo
    Dec 13, 2012 at 19:18
  • 2
    If you'd need to use CONCAT instead of CONCAT_WS for any reason, you can do a COALESCE(column, '') wrapper around each nullable column, which is a more elegant way than the mentioned CASE or IF solutions from below.
    – t.h3ads
    Mar 25, 2021 at 13:33
  • Note that the first argument in CONCAT_WS is a separator
    – Kida
    Sep 12, 2023 at 9:05
4

You can concatenate the parens and the nickname, and then use the IFNULL to check to see if the result of that expression is NULL.

If it's null, replace the NULL with an empty string, and then concatenate that expression to the name column.

SELECT id
     , CONCAT(name,IFNULL(CONCAT(' (',nickname,')'),'')) AS name
  FROM user

NOTE: The IFNULL function is shorthand for:

IF(expr1 IS NULL,expr2,expr1)

or the ANSI equivalent:

CASE WHEN expr1 IS NULL THEN expr2 ELSE expr1 END 

There are other approaches that will work just as well. But they all basically need to do the same thing: check if nickname is NULL, and then conditionally include the parens and the nickname, or an empty string.

2

You can use a CASE statement:

SELECT id, 
  case 
    when nickname is not null 
    then CONCAT(name,' (',nickname,')')
    else name end as name 
FROM user;

See SQL Fiddle with Demo

Or:

SELECT id, 
  concat(name, if(nickname is not null, concat(' (',nickname,')'), '')) as name
FROM user;

See SQL Fiddle with Demo

Both give the same result:

| ID |              NAME |
--------------------------
|  1 |  Adam (Alpha Dog) |
|  2 |       Bob (Bobby) |
|  3 | Charles (Charlie) |
|  4 |              Doug |
0
2

Try this:

SELECT id, concat(name , if(nickname is not null, concat(' (', nickname, ')'), '')) as name FROM user;
1
  • In my case i need to check for empty string, not null so my code is if(nickname != '')
    – Ronald
    May 15, 2021 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.