Thread: replace() using NULL

replace() using NULL

From
Susanne Holzgraefe
Date:
Hello,

tested by using postgresql 13.5:

SELECT REPLACE('xyz‘, ’y’, ’a’); => xaz => expected
SELECT REPLACE('xyz', 'a‘, 'b‘); => xyz => expected

SELECT REPLACE('xyz‘, 'a‘, NULL); => NULL

Also tested by using MariaDB. Same behavior like in PG.
Tested with Oracle, result is ‚xyz‘.

That is what I would expect because xyz includes no a.

I neither can find any documentation about this nor a bug report.

Regards,

miracee (Susanne)

Attachment

Re: replace() using NULL

From
Julien Rouhaud
Date:
Hi,

On Wed, Apr 13, 2022 at 10:38:28AM +0200, Susanne Holzgraefe wrote:
>
> tested by using postgresql 13.5:
>
> SELECT REPLACE('xyz‘, ’y’, ’a’); => xaz => expected
> SELECT REPLACE('xyz', 'a‘, 'b‘); => xyz => expected
>
> SELECT REPLACE('xyz‘, 'a‘, NULL); => NULL
>
> Also tested by using MariaDB. Same behavior like in PG.
> Tested with Oracle, result is ‚xyz‘.
>
> That is what I would expect because xyz includes no a.
>
> I neither can find any documentation about this nor a bug report.

The function is declared as STRICT, which means that it won't be called at all
and just returns NULL if any of its parameters is NULL.

Maybe oracle supports such calls as it supports NULL bytes in strings (I
think), but postgres doesn't.



Re: replace() using NULL

From
Tom Lane
Date:
Julien Rouhaud <rjuju123@gmail.com> writes:
> On Wed, Apr 13, 2022 at 10:38:28AM +0200, Susanne Holzgraefe wrote:
>> SELECT REPLACE('xyz‘, 'a‘, NULL); => NULL

> The function is declared as STRICT, which means that it won't be called at all
> and just returns NULL if any of its parameters is NULL.

Right.  The SQL standard doesn't seem to have a REPLACE function, so it's
hard to settle this by appealing to the standard; but it's reasonable to
suppose that if they did define it then it would be specified to work like
the existing substring manipulation functions --- and those are all strict
per spec.  For example, the spec for SUBSTRING says

   d) If at least one of C, S, and L is the null value, then the result of
      the <character substring function> is the null value.

and there is equivalent verbiage in every other subparagraph of
SQL:2021 6.32 <string value function>.

>> Tested with Oracle, result is ‚xyz‘.

> Maybe oracle supports such calls as it supports NULL bytes in strings (I
> think), but postgres doesn't.

Oracle is a very suspect reference when it comes to behaviors involving
NULL, because they are not even a little bit spec-compliant there.
My understanding is that they can't really represent a NULL string at
all, and fake it as being an empty string ('') ... which of course
yields that result in this case.

            regards, tom lane