Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL using If Not Null on a Concatenation

If I have the table

enter image description here

SELECT (Firstname || '-' || Middlename || '-' || Surname)  AS example_column FROM example_table 

This will display Firstname-Middlename-Surname e.g.

John--Smith Jane-Anne-Smith 

The second one (Jane’s) displays correct, however since John doesn’t have a middlename, I want it to ignore the second dash.

How could I put a sort of IF Middlename = NULL statement in so that it would just display John-Smith

like image 213
BiscuitCookie Avatar asked May 19 '16 17:05

BiscuitCookie


People also ask

IS NULL with concatenate in SQL?

In SQL Server, NULL is a special pointer that specifies a value that is undefined or does not exist. When passing a NULL value as a parameter to the CONCAT function, the NULL values are converted to an empty string.

IS NOT NULL in if condition SQL?

The IS NOT NULL condition is used in SQL to test for a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

Can you concatenate a string with NULL?

Using the String.concat() method is a good choice when we want to concatenate String objects. The empty String returned by the getNonNullString() method gets concatenated to the result, thus ignoring the null objects.

Can we concatenate NULL values?

Concatenating Data When There Are NULL ValuesTo resolve the NULL values in string concatenation, we can use the ISNULL() function. In the below query, the ISNULL() function checks an individual column and if it is NULL, it replaces it with a space.


2 Answers

Here would be my suggestions:

PostgreSQL and other SQL databases where 'a' || NULL IS NULL, then use COALESCE:

SELECT firstname || COALESCE('-' || middlename, '') || '-' || surname ... 

Oracle and other SQL databases where 'a' || NULL = 'a':

SELECT first name || DECODE(middlename, NULL, '', '-' || middlename) || '-' || surname... 

I like to go for conciseness. Here it is not very interesting to any maintenance programmer whether the middle name is empty or not. CASE switches are perfectly fine, but they are bulky. I'd like to avoid repeating the same column name ("middle name") where possible.

As @Prdp noted, the answer is RDBMS-specific. What is specific is whether the server treats a zero-length string as being equivalent to NULL, which determines whether concatenating a NULL yields a NULL or not.

Generally COALESCE is most concise for PostgreSQL-style empty string handling, and DECODE (*VALUE*, NULL, ''... for Oracle-style empty string handling.

like image 53
Andrew Wolfe Avatar answered Sep 22 '22 05:09

Andrew Wolfe


If you use Postgres, concat_ws() is what you are looking for:

SELECT concat_ws('-', Firstname, Middlename, Surname)  AS example_column FROM example_table 

SQLFiddle: http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/8812

To treat empty strings or strings that only contain spaces like NULL use nullif():

 SELECT concat_ws('-', Firstname, nullif(trim(Middlename), ''), Surname)  AS example_column  FROM example_table 
like image 24
a_horse_with_no_name Avatar answered Sep 20 '22 05:09

a_horse_with_no_name