Dynamic Substring

Hi All,
I’m looking to see whether it is possible to make my substring function more dynamic. I have a string field which is a variable number of characters but the last four characters refer to a location. I’ve been trying to extract this information from the field but Rebus isn’t liking it. Has anyone generated this before or is there a better way to go about this?

I’ve tried creating this using created variables, so I know the formula is correct, but the substring doesn’t seem to be resolving these.

Works: substring(FIELD_NAME,4,4)
Doesn’t work: substring(FIELD_NAME,subtract(stringLength(FIELD_NAME),5),4)

Thanks

1 Like

FYI; this has been resolved, using the extractString function instead.

Thanks Sasha for this!

If you intend to use a substring function that uses calculated functions (dynamic), as opposed to just fields that are calculated in a previous stage, please use the following function:

extractString as opposed to substring

To get the last 4 characters from a string (similar to RIGHT function in EXCEL), use the following function:

extractString (FIELD_NAME,subtract(stringLength(FIELD_NAME),4),4)

Ideally, you would also check that the size of a string is at least 4 characters just to safeguard for occurrences where the field is less than 4, and the function would fail, as it expects both the start position and the length arguments to be non-negative integers:

condition(greaterThanEqual(stringLength(FIELD_NAME),4), extractString (FIELD_NAME,subtract(stringLength(FIELD_NAME),4),4),“STRING IS LESS THAN 4 CHARACTERS”)

2 Likes

Thanks for posting the solution Laurence!