Multiple SQL's

629
2
Jump to solution
05-27-2021 09:28 AM
CCWeedcontrol
Occasional Contributor III

I currently have two sql's queries in a script, is there a way to combine SQL1 & SQL2 so I am not doing two separate queries?

 

table1 = r"C:\Temp\Table"

from_date = DT.date.today() - DT.timedelta(days=730)
SQL1 = "AppSubmitted >= DATE '{}'".format(from_date.strftime('%Y-%m-%d')) #'%Y-%m-%d %H:%M:%S'

SQL2 = "PNum LIKE 'RZ%' OR PNum LIKE 'OR%' OR PNum LIKE 'CR%'"
arcpy.MakeTableView_management(table1, 'tableview', SQL) #Include SQL2

 

0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

You can use the AND or OR operator to combine the SQL statements depending on what you're trying to achieve.

sql_expression = "({}) and ({})".format(SQL1, SQL2)

or

sql_expression = "({}) or ({})".format(SQL1, SQL2)

View solution in original post

2 Replies
BlakeTerhune
MVP Regular Contributor

You can use the AND or OR operator to combine the SQL statements depending on what you're trying to achieve.

sql_expression = "({}) and ({})".format(SQL1, SQL2)

or

sql_expression = "({}) or ({})".format(SQL1, SQL2)
CCWeedcontrol
Occasional Contributor III

Sweet, thank you.

SQL1 = "AppSubmitted >= DATE '{}'".format(from_date.strftime('%Y-%m-%d')) #'%Y-%m-%d %H:%M:%S'

SQL2 = "PNum LIKE 'RZ%' OR PNum LIKE 'OR%' OR PNum LIKE 'CR%'"
sql_expression = "({}) and ({})".format(SQL1, SQL2)
arcpy.TableToTable_conversion(table1, workspace,'tableTEST', sql_expression)
0 Kudos