Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: Ask Reuben 3 - Append (,) or Concatenate (||)  (Read 8723 times)
Reuben B.
Four Js
Posts: 1116


« on: April 14, 2020, 02:18:06 am »

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/

Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Rene S.
Four Js
Posts: 112


« Reply #1 on: April 14, 2020, 12:28:35 pm »

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.
Leo S.
Four Js
Posts: 129


« Reply #2 on: April 14, 2020, 12:55:04 pm »

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.
Code
  1. CONSTANT c="hello"
  2. MAIN
  3.  DEFINE a STRING
  4.  CALL echo(c||":"||NVL(a,"a is NULL"))
  5. END MAIN
  6.  
  7. FUNCTION echo(s STRING)
  8.  DISPLAY s
  9. END FUNCTION
  10.  
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines