I've recently run into this behaviour and whilst I can work around it, I'm quite curious as to why it occurs. There doesn't seem to be much documentation explaining why this happens.
Consider the following two snippets:
SELECT REPLACE('hello world', 'world', NULL)
SELECT REPLACE('hello world', 'foobar', NULL)
Both of these examples return NULL.
I don't really understand either, but with the first one I can see how if there is indeed some caveat I'm unaware of with NULLs that this will cause the outcome we see.
But with the second example I'm completely stumped. Even if there is some strange NULL replacing behaviour, why is my string being manipulated at all? The replacement string is not found in the target string.
NULLis not a meaningful operation to begin with. Think of it as the function validating its arguments, seeing something invalid and immediately bailing out.if (isNull(arg1) || isNull(arg2) || isNull(arg3)) return DB_NULL; /* all arguments are non-NULL, do regular replacement */. Getting the special cases out of the way greatly simplifies things. (And note thatNULLis not the null terminator or an otherwise blank string, so it is a special case.)