<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SQL in Python where statement using LIKE in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136621#M10709</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you! That seems to be working fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Do you have any idea how one might use error checking in this code? For example if I can't split a string, is there a way I can just skip it? It's giving me a few errors with certain folders which don't follow the format with the hyphen!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 16 Jul 2013 14:56:20 GMT</pubDate>
    <dc:creator>LaurenYee</dc:creator>
    <dc:date>2013-07-16T14:56:20Z</dc:date>
    <item>
      <title>SQL in Python where statement using LIKE</title>
      <link>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136616#M10704</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all, I'd like to construct a SQL statement for a search cursor using a LIKE with a wildcard instead of an =...but I'm having some difficulty as I find SQL in python with all it's backslashes, apostrophes, double quotes and escapes to be rather confusing. I don't know where to put the wildcard "%" and how to escape it in the code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is what I've tried (couldn't get the script to work with "LIKE" but since I'm trying to find road names "LIKE" is necessary.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy import os import re&amp;nbsp; basedir = r"C:\Test" fc = r"G:\GIS\ParcelsPublic.shp" field1 = "ADD1" field2 = "STREETNAME"&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp; for fn in os.listdir(basedir): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add = re.sub(r"\D"," ", fn) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add = str.lstrip(add) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add = str.upper(add) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; street = re.sub(r"[^a-zA-Z]"," ",fn) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; street = str.lstrip(street) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; street = str.upper(street) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print (add + " " + street) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = '"' + field1 + '"= ' + "'%s'" %add &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression2 = '"' + field2 +'"= ' + "'%s'" %street &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression3 = expression + " "+ "AND" +" " + expression2 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc, fields="STREETNAME; ARN; ADD1", where_clause=expression3)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;And this was provided to me on stackexchange but I haven't been able to implement a wildcard: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;expression3 = '"{field1}" = \'{add}\' AND "{field2}" LIKE \'{street}\''.format(field1=field1, add=add, field2=field2, street=street)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 13:47:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136616#M10704</guid>
      <dc:creator>LaurenYee</dc:creator>
      <dc:date>2013-07-16T13:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: SQL in Python where statement using LIKE</title>
      <link>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136617#M10705</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm not sure I follow what you are trying to do. Something like this?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;"{0} LIKE '%{1}'".format(field, value_suffix)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 14:03:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136617#M10705</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-07-16T14:03:56Z</dc:date>
    </item>
    <item>
      <title>Re: SQL in Python where statement using LIKE</title>
      <link>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136618#M10706</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This code is splitting an address in a file name at a hyphen, and getting variables for the number and street. This works fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Then it is passing these variables into the Parcel layer and using a search cursor to find the matching address to retrieve the roll number for the parcel (not shown in code), and replace the filename with the roll number. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Filename : Main St - 444&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;add = 444&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;street = Main St&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The fieldname for the address is ADD1 and STREETNAME for street. And ARN for Roll #.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I basically want a SQL query similar to "ADD1 = 444 AND STREETNAME LIKE %MAIN ST%"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;but to pass the variables in place.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have tested a different SQL query using = and no LIKE or wildcard and this works if the streetname is exactly matching the one found in the parcels shapefile, but there are many streets such as "Main St S", "Main St N" which wouldn't match. Therefore I'd like to use the wildcard and LIKE to match to these addresses.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;After matching the parcel the filename would then be changed to the matching ARN so filename = "34493850294023948"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I hope this cleared up and confusion.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 14:30:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136618#M10706</guid>
      <dc:creator>LaurenYee</dc:creator>
      <dc:date>2013-07-16T14:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: SQL in Python where statement using LIKE</title>
      <link>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136619#M10707</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Would this work?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;expression3 = '"{field1}" = \'{add}\' AND "{field2}" LIKE \'%{street}%\''.format(field1=field1, add=add, field2=field2, street=street)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 14:41:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136619#M10707</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-07-16T14:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: SQL in Python where statement using LIKE</title>
      <link>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136620#M10708</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The lines that built expression and expression2 were messed up (missing escape character to make single quote around your address and street literal) and expression3 badly written.&amp;nbsp; They are corrected below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os
import re

basedir = r"C:\Test"
fc = r"G:\GIS\ParcelsPublic.shp"
field1 = "ADD1"
field2 = "STREETNAME"

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for fn in os.listdir(basedir):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add = re.sub(r"\D"," ", fn)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add = str.lstrip(add)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add = str.upper(add)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; street = re.sub(r"[^a-zA-Z]"," ",fn)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; street = str.lstrip(street)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; street = str.upper(street)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print (add + " " + street)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = '"' + field1 + '" = ' + add
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression2 = '"' + field2 + '" LIKE \'%' + street + '%\''
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression3 = expression + " AND " + expression2
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(fc, fields="STREETNAME; ARN; ADD1", where_clause=expression3)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I assumed ADD1 is a numeric field?&amp;nbsp; If it is a string field rewrite the line that builds expression as:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = '"' + field1 + '" = \'' + add + '\''&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And if you want the LIKE syntax applied to the address string use:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = '"' + field1 + '" = \'' + add + '%\''&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:36:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136620#M10708</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T07:36:12Z</dc:date>
    </item>
    <item>
      <title>Re: SQL in Python where statement using LIKE</title>
      <link>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136621#M10709</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you! That seems to be working fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Do you have any idea how one might use error checking in this code? For example if I can't split a string, is there a way I can just skip it? It's giving me a few errors with certain folders which don't follow the format with the hyphen!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 14:56:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136621#M10709</guid>
      <dc:creator>LaurenYee</dc:creator>
      <dc:date>2013-07-16T14:56:20Z</dc:date>
    </item>
    <item>
      <title>Re: SQL in Python where statement using LIKE</title>
      <link>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136622#M10710</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Something like this might work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

if "-" in Filename:
&amp;nbsp; then do your split
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So it would only do the split if the hyphen is in the string.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:36:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136622#M10710</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-11T07:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: SQL in Python where statement using LIKE</title>
      <link>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136623#M10711</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am a nonprogrammer and very new to Python.&amp;nbsp; I would like to build a script tool for parcel searching.&amp;nbsp; The tool will search a countywide parcel FC named Parcels in a FDS named Cadastre residing in a SQL enterprise GDB named EnterpriseGIS located on a network server named somcty06.&amp;nbsp; The tool will require the user to select a municipality from a drop down, enter block number as text, lot as text, then zoom to the parcel.&amp;nbsp; If no match it should return a No Parcel Found Matching Criteria message.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 Jul 2013 14:06:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-in-python-where-statement-using-like/m-p/136623#M10711</guid>
      <dc:creator>JamesGirvan</dc:creator>
      <dc:date>2013-07-31T14:06:30Z</dc:date>
    </item>
  </channel>
</rss>

