Weird behaviour with TRIM() LEFT-TRIM() and RIGHT-TRIM() whe

Posted by slacroixak on 03-Oct-2019 13:33

Hi Forum , we are a bit surprised to find nothing about this weird behaviour in the KBase as the TRIM() RIGHT-TRIM() and LEFT-TRIM() are rather old 4GL Functions

MESSAGE TRIM(' AND ARTICLE.fieldA = "abc" AND ARTICLE.fieldB = "123"', " AND") VIEW-AS ALERT-BOX.
   
/* Notice 'ARTICLE' gets truncated to 'RTICLE'
---------------------------
Message (Press HELP to view stack trace)
---------------------------
RTICLE.fieldA = "abc" AND ARTICLE.fieldB = "123"
---------------------------
OK   Aide   
--------------------------- */

The impact is bad for building typical dynamic queries with " AND bladibla " expressions conditionally added to some cqueryString.  At a point one needs to change "WHERE AND" into "WHERE ".   The TRIM technique is nasty because it only affects tables with name beginning 'A'   (or 'O' with OR)

We already have a work-around** but I thought others might want to know about this nasty one.

   **The best is to use the SUBSTRING() Statement to remove a targeted ' AND'

Posted by Mike Fechner on 03-Oct-2019 13:41

TRIM treats your „ AND“ as a list of characters to remove from the beginning – not as a single literal. So that’s really expected. Trim cannot be used to remove a multi-character pattern from the beginning or the end of a string.

All Replies

Posted by Peter Judge on 03-Oct-2019 13:40

I thought it was in the doc, but I believe that TRIM will trim " ", "A", "N" and "D" (and not " AND").
 
               def var andPos as Int.
andPos = L-INDEX(string, " AND")  
 
is the way to go, IMO.
 

Posted by Mike Fechner on 03-Oct-2019 13:41

TRIM treats your „ AND“ as a list of characters to remove from the beginning – not as a single literal. So that’s really expected. Trim cannot be used to remove a multi-character pattern from the beginning or the end of a string.

Posted by slacroixak on 03-Oct-2019 13:50

Thank you Mike.  Indeed, the doc says "trim-chars" and not "trim-string".  Actually, I was not using this technique myself... I'll my my peers ;)

This thread is closed