# SimulationAids: View expression function usage

**URL:** <https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [July 16, 2019, 8:15am UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571 "2019-07-16T08:15:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 16, 2019, 8:15am UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571/1 "2019-07-16T08:15:00Z")

</div>

@pturmel  
I’m trying to use your View expression function in 7.9.12, but not entirely sure how to form my simple select query.

I’m trying this:

```auto
view("SELECT tagName, tagPath WHERE tagName LIKE '%" + lower({Root Container.TagBrowser.filter.text}) + "%'", {Root Container.TagBrowser.Power Table.raw_dataset})

```

but I’m getting the error:

```auto
Caused by: org.python.core.PySyntaxError: SyntaxError: ("mismatched input 'LIKE' expecting COLON", ('<expression:view>', 4, 13, " if tagName LIKE '%loca%':\n"))

```

What am I doing wrong? Or am I perhap reading too literally into the SQL structure of the selectString argument, and the LIKE keyword isn’t supported (converted into Python)?

Also, can I suggest adding in some examples into your doco to show exactly how these work 🙂

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 16, 2019, 8:50am UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571/2 "2019-07-16T08:50:52Z")

</div>

FYI: I’m using a regex to do this in a python script instead as I needed to get it running.

This is what I’m using to mimic the SQL LIKE operand:

```auto
def findLike(searchString, searchTerm):
	'''
	Description:
		Searches the search string for the search term, similar in function to the SQL 'LIKE' operator.
		Accepts wildcards % (any characters) and _ (any single character).
		If searchTerm does not contain surrounding wildcards, the start and end characters in both the searchString and searchTerm must match to produce a match.
	'''
	import re #regex
	# create the regex match string. Need the start and end characters added "^" and "$" as these anchor the match to the start and end of the string.
	# Without these, matches would be found in the middle of the search string surrounded by other characters even if there is no % or _ character at either end.
	# Also, replace SQL % and _ with equivalent regex search terms, .* and .
		
	re_match_string = "^" + searchTerm.replace("%",".*").replace("_",".") + "$"
	# if we replaced wildcard characters that were marked to be escaped (e.g. using [] brackets, then replace these back.
	re_match_string = re_match_string.replace("[.*]", "[%]").replace("[.]", "[_]")
	search_re = re.compile(re_match_string)
	
	return 1 if not search_re.match(searchString) is None else 0

print findLike('hello world!', 'hello%')

```

The smarts are courtesy of: [https://stackoverflow.com/questions/26148712/compare-strings-in-python-like-the-sql-like-with-and](https://stackoverflow.com/questions/26148712/compare-strings-in-python-like-the-sql-like-with-and)

Edit: made into a function instead and handled escaped % and \_ wildcards

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [July 16, 2019, 9:12am UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571/3 "2019-07-16T09:12:15Z")

</div>

Just as a side note (if you want to make this more universal), you may want to [`re.escape`](https://docs.python.org/3/library/re.html#re.escape) your string before using it as a regex (in order to avoid issues with special characters like periods or quotes).

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 16, 2019, 9:53am UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571/4 "2019-07-16T09:53:56Z")

</div>

Thanks Sander, I figured there would be other cases that I haven’t accounted for yet, but was going to cross that bridge when I needed to use it more diversely. Cheers for the info

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 16, 2019, 11:33am UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571/5 "2019-07-16T11:33:24Z")

</div>

Hi Nick,

You are correct: LIKE is not supported, as that is a SQL expression operator, not python. The PseudoSQL is SQL-ish for structure, but needs python expressions. Also, you should avoid assembling your first argument (the SQL) dynamically. Pass the relevant information as extra args and arrange it appropriately within the python expression. Something like this:

```auto
view("SELECT tagName, tagPath WHERE args[0].lower() in tagName",
  {Root Container.TagBrowser.Power Table.raw_dataset},
  {Root Container.TagBrowser.filter.text})

```

The best examples are in the sample project.

---

<div class="post-metadata">

**Author:** ![samer\_samy](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/samer_samy/32/52910_2.png) [@samer\_samy](https://forum.inductiveautomation.com/u/samer_samy)\
**Post date:** [May 1, 2024, 12:22pm UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571/6 "2024-05-01T12:22:07Z")

</div>

Hi ptunnel,  
How to use

```python
view('select Line,WEEK,100*um(unitGP)/sum(unit) as Yield where unitGP is not Null group by Line,WEEK,Year,unit,UnitGP order by WEEK',{value})

```

I can not use Null in view function

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 1, 2024, 12:23pm UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571/7 "2024-05-01T12:23:24Z")

</div>

These are pyExpressions. Use `is not None` as the condition.

---

<div class="post-metadata">

**Author:** ![samer\_samy](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/samer_samy/32/52910_2.png) [@samer\_samy](https://forum.inductiveautomation.com/u/samer_samy)\
**Post date:** [May 1, 2024, 12:32pm UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571/8 "2024-05-01T12:32:23Z")

</div>

after fixing has been

```python
view ('select Line,WEEK,sum(unitGP),100*sum(unitGP)/sum(unit) Where unitGP is not None group by Line,WEEK,Year',{value})

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 1, 2024, 12:48pm UTC](https://forum.inductiveautomation.com/t/simulationaids-view-expression-function-usage/26571/9 "2024-05-01T12:48:07Z")

</div>

Tip: You can use newline in Expressions to make it much more readable. Also, adding spaces after punctuation (commas) and before and after operators (+, -, \*, /) is a big help as it follows normal rules of writing.

```python
view('SELECT 
    Line,
    WEEK,
    SUM(unitGP),
    100 * SUM(unitGP) / SUM(unit) 
  WHERE unitGP is not None 
  GROUP BY Line, WEEK, Year',
  {value}
)

```
