Should we use the concatenation operator ||
or the format()
function in trigger functions?
Is there any advantage of using one over the other or does it come down to personal preference and readability?
Would you say for simple concatenation, use the operator but for more complex concatenation, use the format function?
There are basically 4 standard tools for concatenating strings. Simplest / cheapest first:
||
(the standard SQL concatenation operator) ...
null
if any operand is null
. (May or may not be desirable.)format()
or concat()
.||
operators, and the input types need to be unambiguous for operator type resolution.IMMUTABLE
, which allows their safe use in indexes or other places where immutable volatility is required.concat()
...
null
if one argument is null
. (May or may not be desirable.)text
.STABLE
(because it takes "any"
input type and coerces the input to text, and some of these conversions depend on locale or time-related settings). So not suitable where immutable volatility is required. See:
concat_ws()
("with separator") ...
concat()
.format()
...
null
if any of the input parameters are null
. (May or may not be desirable.)STABLE
.Further reading: