Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation with a null seems to nullify the entire string - is that desired behavior in Postgres?

In Postgres:

select 'test' || null  returns null 

I was expecting it would otherwise return 'test'.

Is this desired behavior? Seems weird that string concatenation with a null value would nullify the entire string...

Referring to pg docs: http://www.postgresql.org/docs/9.1/static/functions-string.html

"Note: Before PostgreSQL 8.3, these functions would silently accept values of several non-string data types as well, due to the presence of implicit coercions from those data types to text. Those coercions have been removed because they frequently caused surprising behaviors. However, the string concatenation operator (||) still accepts non-string input, so long as at least one input is of a string type, as shown in Table 9-6. For other cases, insert an explicit coercion to text if you need to duplicate the previous behavior."

Given that, using their concat string function example:

concat(str "any" [, str "any" [, ...] ]) text Concatenate all arguments. NULL arguments are ignored. concat('abcde', 2, NULL, 22) >> abcde222

Should I just get used to this behavior with '||' concatenation or is this something that should be fixed?

like image 882
Reinsbrain Avatar asked Jan 21 '16 21:01

Reinsbrain


People also ask

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.

Is string concatenation bad?

Due to this, mixing the StringBuilder and + method of concatenation is considered bad practice. Additionally, String concatenation using the + operator within a loop should be avoided. Since the String object is immutable, each call for concatenation will result in a new String object being created.


2 Answers

Quote from documentation

Concatenate all arguments. NULL arguments are ignored.

Example

concat('abcde', 2, NULL, 22)  

Returns

abcde222 

See more in Documentation

like image 97
Yevgeniy Afanasyev Avatar answered Oct 07 '22 13:10

Yevgeniy Afanasyev


It's not a bug and it's not "weird".

The SQL standard requires any expression that involves null yields null. This is not limited to string concatenation, it also applies to computations e.g.: 42 * null returns null.

This also applies to comparisons: 42 > null yields null. So the comparison it's neither true nor false. Although in reality this has the effect of "false", but more because it's "not true", rather then false. But negating such an expression yields null again, not "true".

Because null is so special, the only way to check if something is null is to use the operator IS NULL or IS NOT NULL. x is null yields either true or false, it never yields null, so expressions using the is null or is not null operator never return null - so this is an exception to my statement above (thanks Jonathan for pointing that out).


Another - maybe surprising - fact about null values is how they are handled by aggregate functions. While the expression 4 + 5 + null yields null, the sum() over those (column) values would yield 9, because aggregates ignore null values.

Given the following table:

    col1 --------        1        2        3       null 

sum(col1) will return 6, and avg(col1) will return 2 (sum = 6, number of elements added: 3)

like image 25
a_horse_with_no_name Avatar answered Oct 07 '22 14:10

a_horse_with_no_name