What should I use to join two or more strings together? Append (,) or Concatenate (||) ?
A question I have been asked a few times, is it better to use append (,) or concatenate (||) to join two strings together?
The quick answer is what result do you expect if one of the operands is NULL. With concatenation (||), if one of the operands is NULL then the result will be NULL as well. So for a quick test, note difference in output of ...
However the answer I like to give back is that are you aware that there is a 3rd option?, that is the SFMT operator. IMHO this leads to code that is easier to read.
Read more at https://4js.com/ask-reuben/ig-3/
Ask different:
Why do two distinct forms of string concatenation exist? 1st: ',' and 2nd: '||'
Answer:
The first form of string concatenation is a statement:
LET string-variable = expr ( ',' expr )...
Read as: "LET string-variable = comma separated expression list"
The second form of string concatenation is an expression:
This is a fragment of the expression grammar:
expr := expr '||' expr
The concatenation result can directly be used wherever 4gl accepts expressions (example: function parameters).
Another remark:
The first form of the string concatenation produces a formatted string: numeric, date, date-time, interval and char() operands will be formatted with a fix width. See the print statement of reports. The first form of the string concatenation has the same semantics as the print statement.
Like Reuben I personally avoid the concat operator due to the NULL semantics.
If one still does, use the NVL() operator to avoid the NULL pitfall.
CONSTANT c="hello"
MAIN
DEFINE a STRING
CALL echo(c||":"||NVL(a,"a is NULL"))
END MAIN
FUNCTION echo(s STRING)
DISPLAY s
END FUNCTION