Skip to content

'a' || NULL gives 'a', SQL gives NULL #2811

Open

Description

@mathiasrw

|| treats NULL as empty text: 'a' || NULL is 'a', and NULL || NULL is ''. In SQL-92 (and SQLite) a concatenation with a NULL operand is NULL.

Seen in the NIST SQL Test Suite run in yts812.sql (WHERE TX2 || TX3 IS NOT NULL counts 5 rows instead of 3). Reproduced on develop (38bf3c3):

alasql('CREATE TABLE x (a CHAR(2), b CHAR(2))');
alasql("INSERT INTO x VALUES ('a', NULL), (NULL, NULL), ('a', 'b')");
alasql('SELECT a || b AS c FROM x');                              // [{c: 'a'}, {c: ''}, {c: 'ab'}]
alasql('SELECT COUNT(*) AS n FROM x WHERE a || b IS NOT NULL');   // [{n: 3}]

Expected (SQL-92 and SQLite): NULL, NULL, 'ab', and a count of 1.

Breaking?

Yes. Code can rely on 'Mr ' || middle_name giving text when the middle name is missing. It belongs with the other NULL changes.

Related: #2588, #2597.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions