IF Statements for Strings in Column Expressions

Hi - I want to say
IF left( code , 4 = “DRAM” then “RKW”
ELSEIF left( code ,4 - “DRAQ” then “AQ”
ELSE “UNK” ENDIF

I have tried quite a few of the suggestions here (==, encasing them in $ signs, using curly brackets, etc.) to accomplish this and keep failing. Are strings not an option in Column Expressions? Or am I missing something obvious?

If it can’t be done in Column Expressions is there somewhere it can be done so a node for each individual one is not necessary? Thanks.

Hey,

General syntax is Java Script like:

If(condition1) {
  Value1
} else if (condition2) {
value2
} else {
Value3
}

You can have as many else ifs as you like before the last „catch all“ else.

In () the conditions can reference columns, variables or „hardcoded“ numbers / strings etc.

E.g.:

If(left(column(code),4)=="DRAM") {
"RKW"
} else if…
4 Likes

Thank you. I think this is saying IF is not an allowable function in Column Expressions. I don’t see it in the function dropdown - where am I going wrong?

1 Like

It looks correct but think you need to put code in quotation marks:

Column(“code“)

Correct if is not in the pre built formula, but Java script works …

Not sure if the code is case-sensitive as well - if the column topic doesn’t fix it try all lower case so if instead of If

If you want a node with pre-built if:

Use the new Expression node (available with 5.3 onwards I think )

2 Likes

@mgrau this article has several examples some with if then else. Note that the syntax might differ slightly

Thank you! It wanted quotes, not double commas or apostrophes. The devil is in the details. Expression node and the Knime snippets will be a big help too.
if (left(column(“code”),4)==“DRAM”){
“RNW”
}
else if (left(column(“code”),4)==“DRAQ”){
“AQ”
}
else{
“UNK”
}

2 Likes