Using Calculate value for an sql statement

385
1
Jump to solution
02-01-2023 12:00 PM
Labels (2)
KitTompkins
New Contributor II

I'm trying to use calculate value to create an sql expression for the Make Query Table Tool, and I've reached a point where I don't know why it's not working. 

Here's my calculate value:

Expression = sqEX("%Name%")

def sqEX(Table):
	fEx= "{}.Owner IS NOT NULL AND {}.Owner NOT LIKE '%FLO%'".format(*Table)
	return fEx

And this is the error message I'm getting back:

Executing (Calculate Value):

CalculateValue sqEX("ssSystemValve") "def sqEX(Table):
fEx= "{}.Owner IS NOT NULL AND {}.Owner NOT LIKE '%FLO%'".format(*Table)
return fEx"

"SQL Expression"
Start Time: Wednesday, February 1, 2023 11:41:31 AM


Value = s.Owner IS NOT NULL AND s.Owner NOT LIKE '%FLO%'

As you can see, the expression is showing the variable from the name field correctly, but then not passing it correctly to the string... I think it has something to do with the asterisk, but if I don't have that, it fails because there are two arguments in my format... thing. Do I need to write Table twice? Is there some other less stupid way to fix this problem?

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor

With python 3, f strings seem to work much more reliably with string substitutions like this.

Expression = sqEX("%Name%")

def sqEX(Table):
	fEx= f"{Table}.Owner IS NOT NULL AND {Table}.Owner NOT LIKE '%FLO%'"
	return fEx

Since your code is assigning %Name% = 'ssSystemValve', I just substituted that for testing:

RhettZufelt_0-1675283517985.png

 

R_

View solution in original post

0 Kudos
1 Reply
RhettZufelt
MVP Frequent Contributor

With python 3, f strings seem to work much more reliably with string substitutions like this.

Expression = sqEX("%Name%")

def sqEX(Table):
	fEx= f"{Table}.Owner IS NOT NULL AND {Table}.Owner NOT LIKE '%FLO%'"
	return fEx

Since your code is assigning %Name% = 'ssSystemValve', I just substituted that for testing:

RhettZufelt_0-1675283517985.png

 

R_

0 Kudos