<?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 Best practices for writing SQL selection expressions in Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524685#M41136</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Here is the correct where clause:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for dat in datelst:
&amp;nbsp; arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "\"date\" = " + "'" + dat + "'")
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Python string substitution makes this kind of thing a LOT easier to write and and debug:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
whereExpr =&amp;nbsp; "\"date\" = \'%s\'" % dat&amp;nbsp; # I use outside double-quotes always for stylistic reasons
whereExpr =&amp;nbsp; '"date" = \'%s\'' % dat&amp;nbsp;&amp;nbsp;&amp;nbsp; # but this is equivalent, using ' to preserve " inside the string
arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION",whereExpr)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The "new" way to do this in Python is to &lt;/SPAN&gt;&lt;A href="http://docs.python.org/library/string.html#format-examples" rel="nofollow noopener noreferrer" target="_blank"&gt;use the .format method.&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
whereExpr = "\"date\" = \'{0}\'".format(dat)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I prefer the "old" "% syntax" way myself for simple expression creation. Both are still fully supported.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;In arcpy (and also when using gp = arcgisscripting.create(9.3)), you shouldn't need to use the "\" things in SQL expresions anymore&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think it's still good practice to double quote field names. If you have field names that cause SQL heartburn, the quotes will protect your field names from being parsed as SQL code and breaking your expression.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note when you're working with personal geodatabases, you need to use special field name delimeters ([]). ArcPy has a handy &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000004n000000" rel="nofollow noopener noreferrer" target="_blank"&gt;AddFieldDelimiters method&lt;/A&gt;&lt;SPAN&gt; to make your expressions .mdb-safe ("wks" below is the workspace where the table view you are querying against lives):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
whereExpr = "{0} = \'{1}\'".format(arcpy.AddFieldDelimiters(datefield,wks),dat)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Depending on the wks type, you get from this something like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;gdb, shape: "DATE" = '2012-01-02'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mdb:&amp;nbsp; [DATE] = '2012-01-02'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One more thing, since you were mentioning dates: be wary of date fields, they have all kinds of gotchas.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;More details:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Arc 10 help: &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s500000033000000" rel="nofollow noopener noreferrer" target="_blank"&gt;SQL reference for query expressions used in ArcGIS&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 22:52:45 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2021-12-11T22:52:45Z</dc:date>
    <item>
      <title>Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524671#M41122</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to iterate through each date in a shapefile using a list of dates. I am having trouble phrasing my Where clause to select only one date. If I use the following Python code, all dates are selected in each iteration:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;datelst = ['2002-06-06','2002-06-07','2002-06-08']&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.MakeFeatureLayer_management ("E:/hotspots.shp", "hotspots") &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for dat in datelst:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", ("'date' = 'dat'"))&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have tried rephrasing the Where clause in many different ways, but the result is that all records are selected, no records are selected, or an error message is returned. The query is successful if I use the string itself (e.g. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "date = '2002-06-06'")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;but then it is not possible to iterate through all of the dates in the file.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Jan 2011 23:01:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524671#M41122</guid>
      <dc:creator>KirstenBarrett</dc:creator>
      <dc:date>2011-01-04T23:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524672#M41123</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You need to use the string operator as such:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", ("'date' = '" + dat + "'"))&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You may be able to select the full set all at once as well:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;datelst = ['2002-06-06','2002-06-07','2002-06-08']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;dateliststring = ",".join("'%s'" % dat for dat in datelst)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", ("'date' in (%s)" % dateliststring))&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Jan 2011 23:16:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524672#M41123</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2011-01-04T23:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524673#M41124</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for your reply. When I use the code you suggested &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", ("'date' = '" + dat + "'"))&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;it produces the same result, i.e., the entire dataset is selected, not just the date specified by 'dat'.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Jan 2011 16:09:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524673#M41124</guid>
      <dc:creator>KirstenBarrett</dc:creator>
      <dc:date>2011-01-05T16:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524674#M41125</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Why is your where clause in parenths? That could be what is messing you up. I dont know why it would but it might be.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Jan 2011 16:29:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524674#M41125</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2011-01-05T16:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524675#M41126</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The Tool Help suggests that you put the clause in parentheses. I've tried it without the parentheses:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "'date' = 'dat'")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and I get the same result (all dates selected).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Jan 2011 16:40:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524675#M41126</guid>
      <dc:creator>KirstenBarrett</dc:creator>
      <dc:date>2011-01-05T16:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524676#M41127</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here is how I do this in my code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for dat in datelist:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "'date' = '%s'"%dat)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:52:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524676#M41127</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2021-12-11T22:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524677#M41128</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks, but using your code (clm42) it is still selecting all records.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Jan 2011 17:57:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524677#M41128</guid>
      <dc:creator>KirstenBarrett</dc:creator>
      <dc:date>2011-01-05T17:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524678#M41129</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm using 9.3.1, so I don't know if the arcpy module would have a problem with this suggestion...&amp;nbsp; If it was me, I would just calculate the where clause on a separate line, above the select tool:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for dat in datelist:
&amp;nbsp;&amp;nbsp;&amp;nbsp; whereclause = "'date' = " + dat
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", whereclause)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've run into where clause issues involving quotes plenty of times.&amp;nbsp; If whereclause = "'date' = " + dat doesn't work, I would next try something like whereclause = "date = " + dat or whereclause = "'date' = '" + dat + "'"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:52:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524678#M41129</guid>
      <dc:creator>ZoeZaloudek</dc:creator>
      <dc:date>2021-12-11T22:52:37Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524679#M41130</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the suggestion. Only the last where clause example 'works' in that it selects records, but similar to everything else I've tried, it selects all records in the dataset (including the dates that were not specified in the where clause).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jan 2011 17:21:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524679#M41130</guid>
      <dc:creator>KirstenBarrett</dc:creator>
      <dc:date>2011-01-06T17:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524680#M41131</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, someone smarter than I am figured it out.&amp;nbsp; Here is the correct where clause:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for dat in datelst:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "\"date\" = " + "'" + dat + "'")&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jan 2011 22:55:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524680#M41131</guid>
      <dc:creator>KirstenBarrett</dc:creator>
      <dc:date>2011-01-06T22:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524681#M41132</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You should also just be able to use this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for dat in datelst:
&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "date = '" + dat + "'") &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In arcpy (and also when using gp = arcgisscripting.create(9.3)), you shouldn't need to use the "\" things in SQL expresions anymore.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think your original problem was that you had single quotes ('MY_FIELD') arount your field name, and you don't need to.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:52:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524681#M41132</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T22:52:40Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524682#M41133</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you for asking the question!&amp;nbsp; I have been trying all kinds of different combinations for this problem as well.&amp;nbsp; ESRI needs to re-write their help so as not to confuse the issue further.&amp;nbsp; Adding parentheses and telling people they need double-quotes around field names to query shapefiles are both misleading pieces of advice.&amp;nbsp; Neither is true in this situation!!!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Jan 2011 16:31:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524682#M41133</guid>
      <dc:creator>sofoo</dc:creator>
      <dc:date>2011-01-20T16:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524683#M41134</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm looking at doing something very very similar, the only difference is that my list contains numeric values and I want to compare a field to each value in the unique list.&amp;nbsp; However, I'm having trouble linking the field to the current value in the list using the MakeFeatureLayer tool.&amp;nbsp; I tried doing it as per this thread, but it appears that I need another way to compare the current value other than using it as a string (since survey_month is a double field).&amp;nbsp; Any help with linking the numerical value within the loop to my feature class field would be greatly appreciated!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;valueList = []&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = arcpy.SearchCursor(inFC)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; aVal = row.getValue(inField)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; if aVal not in valueList:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; valueList.append(aVal)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;del row, rows&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print valueList&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for value in valueList:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; #make feature layer where survey_month field equals the current value in the unique list&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; lyrbase = "MonthLyr"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; val = str(value)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; lyrname = lyrbase + val&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; where = "survey_month = value"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;##Also tried where = "survey_month = " + val&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; arcpy.MakeFeatureLayer_management(inFC, lyrname, where)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; arcpy.RefreshTOC()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; #update symbology based on layer in mxd&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd, lyrname,df):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.UpdateLayer(df, lyrname, sourcelayer, True)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; #change the text element to read the month of the current unique value&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Month"):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; elm.text = "Month: " + val&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; #export to PDF with settings&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; outfile = outloc + val&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; arcpy.mapping.ExportToPDF(mxd, outfile, data_frame="PAGE_LAYOUT", resolution=300, image_quality="BEST", colorspace="CMYK", image_compression="LZW", convert_markers="True", embed_fonts="True")&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 05 Mar 2012 19:15:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524683#M41134</guid>
      <dc:creator>EvaJenkins</dc:creator>
      <dc:date>2012-03-05T19:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524684#M41135</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello everyone,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Very helpful thread. Thanks. One thing I changed in my &lt;/SPAN&gt;&lt;STRONG style="font-style: italic;"&gt;for &lt;/STRONG&gt;&lt;SPAN&gt;loop was to use "ADD_TO_SELECTION" rather than use the "NEW_SELECTION". In my case the &lt;/SPAN&gt;&lt;STRONG style="font-style: italic;"&gt;for &lt;/STRONG&gt;&lt;SPAN&gt;loop would overwrite each previous selection which of course is not the intended result. The "ADD_TO_SELECTION" parameter acts like "NEW_SELECTION" for the first iteration. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is my code for future reference to anyone else having similar problems. I have three seperate lists because I use them elsewhere in my function and my table contains 48 records. Of the 48 records only 37 are matched in the table as per my criteria. The idea behind this chunk of code within my function is to create a temporary or one time lookup table based on an area of interest. My goal was to select attributes I needed and delete the rows I did not need to complete my table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; sList = ['BG','PP','IDFxh1','IDFxh1a','IDFxh2','IDFxh2a']
&amp;nbsp;&amp;nbsp;&amp;nbsp; mLIST = ['IDFdk1','IDFdk1a','IDFdk2','IDFdk3','IDFunk','MS']
&amp;nbsp;&amp;nbsp;&amp;nbsp; dList = ['ESSF','ICH','CWH']
&amp;nbsp;&amp;nbsp;&amp;nbsp; mergeList = sList+mLIST+dList

&amp;nbsp;&amp;nbsp;&amp;nbsp; # This is using python list comprehension to add the '%' wildcard to each item in my list
&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp;&amp;nbsp; because the attributes BG, PP, MS, ESSF, ICH, CWH contain up to 4 more characters
&amp;nbsp;&amp;nbsp;&amp;nbsp; mergeList = [x[:1]=='%' and x or x+'%' for x in mergeList] 
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "The merged lists look like this:\n " + str(mergeList)

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeTableView_management("becFreqTbl","tempBecTbl")

&amp;nbsp;&amp;nbsp;&amp;nbsp; for list in mergeList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'iterating through list item:', list
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where_clause = "MAP_LABEL LIKE '"+list+"'"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("tempBecTbl","ADD_TO_SELECTION",where_clause)

&amp;nbsp;&amp;nbsp;&amp;nbsp; newSel = int(arcpy.GetCount_management("tempBecTbl").getOutput(0))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "The new selection yieled "+str(newSel)+" fields selected"

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("tempBecTbl","SWITCH_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; switchSel = int(arcpy.GetCount_management("tempBecTbl").getOutput(0))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "The switch selection yielded "+str(switchSel)+" fields selected"
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.DeleteRows_management("tempBecTbl")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Deleted "+str(switchSel)+" rows from table that did not match the list criteria"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The output looks like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The merged lists look like this:&lt;BR /&gt; ['BG%', 'PP%', 'IDFxh1%', 'IDFxh1a%', 'IDFxh2%', 'IDFxh2a%', 'IDFdk1%', 'IDFdk1a%', 'IDFdk2%', 'IDFdk3%', 'IDFunk%', 'MS%', 'ESSF%', 'ICH%', 'CWH%']&lt;BR /&gt;iterating through list item: BG%&lt;BR /&gt;iterating through list item: PP%&lt;BR /&gt;iterating through list item: IDFxh1%&lt;BR /&gt;iterating through list item: IDFxh1a%&lt;BR /&gt;iterating through list item: IDFxh2%&lt;BR /&gt;iterating through list item: IDFxh2a%&lt;BR /&gt;iterating through list item: IDFdk1%&lt;BR /&gt;iterating through list item: IDFdk1a%&lt;BR /&gt;iterating through list item: IDFdk2%&lt;BR /&gt;iterating through list item: IDFdk3%&lt;BR /&gt;iterating through list item: IDFunk%&lt;BR /&gt;iterating through list item: MS%&lt;BR /&gt;iterating through list item: ESSF%&lt;BR /&gt;iterating through list item: ICH%&lt;BR /&gt;iterating through list item: CWH%&lt;BR /&gt;The new selection yieled 37 fields selected&lt;BR /&gt;The switch selection yielded 11 fields selected&lt;BR /&gt;Deleted 11 rows from table that did not match the list criteria&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:52:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524684#M41135</guid>
      <dc:creator>DanaDiotte1</dc:creator>
      <dc:date>2021-12-11T22:52:43Z</dc:date>
    </item>
    <item>
      <title>Best practices for writing SQL selection expressions in Python</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524685#M41136</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Here is the correct where clause:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for dat in datelst:
&amp;nbsp; arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION", "\"date\" = " + "'" + dat + "'")
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Python string substitution makes this kind of thing a LOT easier to write and and debug:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
whereExpr =&amp;nbsp; "\"date\" = \'%s\'" % dat&amp;nbsp; # I use outside double-quotes always for stylistic reasons
whereExpr =&amp;nbsp; '"date" = \'%s\'' % dat&amp;nbsp;&amp;nbsp;&amp;nbsp; # but this is equivalent, using ' to preserve " inside the string
arcpy.SelectLayerByAttribute_management ("hotspots", "NEW_SELECTION",whereExpr)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The "new" way to do this in Python is to &lt;/SPAN&gt;&lt;A href="http://docs.python.org/library/string.html#format-examples" rel="nofollow noopener noreferrer" target="_blank"&gt;use the .format method.&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
whereExpr = "\"date\" = \'{0}\'".format(dat)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I prefer the "old" "% syntax" way myself for simple expression creation. Both are still fully supported.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;In arcpy (and also when using gp = arcgisscripting.create(9.3)), you shouldn't need to use the "\" things in SQL expresions anymore&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think it's still good practice to double quote field names. If you have field names that cause SQL heartburn, the quotes will protect your field names from being parsed as SQL code and breaking your expression.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note when you're working with personal geodatabases, you need to use special field name delimeters ([]). ArcPy has a handy &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000004n000000" rel="nofollow noopener noreferrer" target="_blank"&gt;AddFieldDelimiters method&lt;/A&gt;&lt;SPAN&gt; to make your expressions .mdb-safe ("wks" below is the workspace where the table view you are querying against lives):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
whereExpr = "{0} = \'{1}\'".format(arcpy.AddFieldDelimiters(datefield,wks),dat)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Depending on the wks type, you get from this something like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;gdb, shape: "DATE" = '2012-01-02'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mdb:&amp;nbsp; [DATE] = '2012-01-02'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One more thing, since you were mentioning dates: be wary of date fields, they have all kinds of gotchas.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;More details:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Arc 10 help: &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s500000033000000" rel="nofollow noopener noreferrer" target="_blank"&gt;SQL reference for query expressions used in ArcGIS&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:52:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/524685#M41136</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T22:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Attributes using a variable in the Where clause</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/1277133#M67393</link>
      <description>&lt;P&gt;This is so simple and effective I can't believe I didn't think to use python to make the variable to just pass as the whole where_clause. I've been working forever on this and finally saw your post!&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2023 23:48:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-using-a-variable-in-the-where/m-p/1277133#M67393</guid>
      <dc:creator>Sean_Perks</dc:creator>
      <dc:date>2023-04-10T23:48:29Z</dc:date>
    </item>
  </channel>
</rss>

