# SQL to Knime Case

**URL:** <https://forum.knime.com/t/sql-to-knime-case/64932>\
**Category:** KNIME Analytics Platform\
**Created:** [April 6, 2023, 1:18am UTC](https://forum.knime.com/t/sql-to-knime-case/64932 "2023-04-06T01:18:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoshiki3](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/y/5daacb/32.png) [@yoshiki3](https://forum.knime.com/u/yoshiki3)\
**Post date:** [April 6, 2023, 1:18am UTC](https://forum.knime.com/t/sql-to-knime-case/64932/1 "2023-04-06T01:18:12Z")

</div>

Hi there, I got a code that I need to run on a node,

If the first two digits are: “20”,“11” or “22”, then Provider\_type =’ E’’  
If the first two digits are “23” or “24”, then Provider\_type = ‘L’

SQL code below:

case  
when substr(table.column,0, 2) in (‘20’,‘21’,‘22’) then ‘E’  
when substr(table.column,0, 2) in (‘23’,‘24’) then ‘L’  
else ‘NA’  
end

What nodes/code would you recommend?

---

<div class="post-metadata">

**Author:** ![ipazin](https://forum-cdn.knime.com/user_avatar/forum.knime.com/ipazin/32/806_2.png) [@ipazin](https://forum.knime.com/u/ipazin)\
**Post date:** [April 6, 2023, 8:31am UTC](https://forum.knime.com/t/sql-to-knime-case/64932/2 "2023-04-06T08:31:23Z")

</div>

Hello @yoshiki3,

you can use [Column Expressions](https://nodepit.com/node/org.knime.expressions.base.node.formulas.FormulasNodeFactory) node. There you’ll find both case syntax and _substr()_ function. Also you can use classic if/else which might be simpler for your case.

Check here workflow example with if/else:

> **[If Else in Column Expression – ipazin](https://hub.knime.com/ipazin/spaces/Public/latest/2020_09_23_If_Else~srzVYM5I6NUM53eR)**
>
> This workflow demonstrates how to do different If/Else constructions in Column Expressions node.

Br,  
Ivan

---

<div class="post-metadata">

**Author:** ![ArjenEX](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/97f17d/32.png) [@ArjenEX](https://forum.knime.com/u/ArjenEX)\
**Post date:** [April 6, 2023, 8:44am UTC](https://forum.knime.com/t/sql-to-knime-case/64932/3 "2023-04-06T08:44:50Z")

</div>

Just wondering, the solutions provided in the previous topic that you created with the exact same question were not sufficient?

> [@Using Case, and subsr in Knime](https://forum.knime.com/t/using-case-and-subsr-in-knime/64287/1):
>
> Hi, I got a string that starts with numbers, If the code starts with “10”,“11” or “12”, then ORDER TYPE = ‘VI’ If the code starts with “13” or “14”, then ORDER TYPE = ‘MI’ case when substr(table.column,0, 2) in (‘10’,‘11’,‘12’) then ‘VI’ when substr(table.column,0, 2) in (‘13’,‘14’) then ‘’ else ‘NA’ end How can I implement this in Knime?

---

<div class="post-metadata">

**Author:** ![yoshiki3](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/y/5daacb/32.png) [@yoshiki3](https://forum.knime.com/u/yoshiki3)\
**Post date:** [April 6, 2023, 12:20pm UTC](https://forum.knime.com/t/sql-to-knime-case/64932/4 "2023-04-06T12:20:25Z")

</div>

Arjen, sorry if this troubled you. I was only looking for alternate solutions. Best regards

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [April 11, 2023, 9:10pm UTC](https://forum.knime.com/t/sql-to-knime-case/64932/5 "2023-04-11T21:10:05Z")

</div>

Hi @yoshiki3 ,

For your specific example (in both your questions) I would probably opt for the Rule Engine approach.

If you are still looking for an alternative, you can use the “almost undocumented” syntax of the String Manipulation node to achieve the desired result.

i.e. Assuming you have a table with “column1” containing your data, then the following in String Manipulation would also return the required result:

```auto
regexMatcher($column1$,"20.*|11.*|22.*" ).equals( "True" )
?"E"
:regexMatcher($column1$,"23.*|24.*" ).equals("True" )
?"L"
:"NA"

```

I think it can also be written as

```auto
regexMatcher($column1$,"20.*|11.*|22.*" ) == "True" 
?"E"
:regexMatcher($column1$,"23.*|24.*" ) == "True" 
?"L"
:"NA"

```

The syntax for the conditionals is as follows:

`condition` **?** `result-if-true` **:** `result-if-false`

In this case it says that if the column matches the regex beginning 20, 11 or 22, then return “E” otherwise if it matches the regex beginning 23 or 24 return “L” otherwise return “NA”

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/4/d/4d2d313997ea6c203f51419e212f45d115c4558c.png)

![image](https://forum-cdn.knime.com/uploads/default/original/3X/b/0/b0edfc3286a6949081eb75df5028618fbcc5e235.png)

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [May 2, 2023, 1:16am UTC](https://forum.knime.com/t/sql-to-knime-case/64932/6 "2023-05-02T01:16:34Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
