<?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: Using a variable (in SQL) to select by attribute with Python in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749769#M24583</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;Expression = '"MER"= ' + MER + ' AND "TWP" = ' + TWP + ' AND "RNG" = ' + RNG + ' AND "SEC"= ' + SEC&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 01 Jun 2012 10:36:09 GMT</pubDate>
    <dc:creator>FabianBlau</dc:creator>
    <dc:date>2012-06-01T10:36:09Z</dc:date>
    <item>
      <title>Using a variable (in SQL) to select by attribute with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749764#M24578</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi there,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to select out any polygons from a feature class that are greater than the standard deviation of the entire feature class.&amp;nbsp; I firstly calculate the standard deviation, store that in a variable and then am getting stuck on how I need to incorporate that into my sql query.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It keeps giving me error 000358 when it gets to the SelectByAttribute part and no matter what I change it doesn't appear to make a difference.&amp;nbsp; I am pretty new to this so sorry if it's massively inefficient.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have posted the code below and any help would be greatly appreciated!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import sys, arcgisscripting&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp=arcgisscripting.create()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.OverWriteOutput = True&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Variables&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;table = "C:\\temp_Statistics"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;field="STD_AREA"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Process&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows=gp.searchcursor(table,"","",field)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;row=rows.next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;while row: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; queryValue = row.STD_AREA&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print queryValue # check that the number is stored in this variable&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row=rows.next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Make feature layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;in_features = "C:\\tester.shp"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;out_layer = "temp_layer"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.MakeFeatureLayer_management (in_features, out_layer, "#", "#", "#")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print "made feature layer"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Select by attribute based upon the value obatained from standard deviation calc.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;where_clause = "area" &amp;gt;= queryValue&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;out_feature_class = "C:\\test_Copy.shp"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.SelectLayerByAttribute_management(out_layer, "NEW_SELECTION", where_clause)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print "selected features"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Copy the selected features to a new layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;gp.CopyFeatures ("temp_layer", out_feature_class, "#", "#", "#")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print "features copied"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Dec 2010 15:14:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749764#M24578</guid>
      <dc:creator>MichelleConvey</dc:creator>
      <dc:date>2010-12-02T15:14:42Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable (in SQL) to select by attribute with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749765#M24579</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try changing this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;where_clause = "area" &amp;gt;= queryValue&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;to&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;where_clause = "\"area\" &amp;gt;= " + queryValue&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Dec 2010 15:44:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749765#M24579</guid>
      <dc:creator>AndrewChapkowski</dc:creator>
      <dc:date>2010-12-02T15:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable (in SQL) to select by attribute with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749766#M24580</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Andrew!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That did the trick - all I had to do after that was to change my variable to a string:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;where_clause = "\"area\" &amp;gt;= " + str(queryValue)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Michelle&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Dec 2010 13:36:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749766#M24580</guid>
      <dc:creator>MichelleConvey</dc:creator>
      <dc:date>2010-12-03T13:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable (in SQL) to select by attribute with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749767#M24581</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Michelle,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Just an additional thought. I have had similar problems in getting the syntax right, so have in some circumstances I have created what I need to to in a basic model using modelbuilder, then exporting that as a python script. Opening the script generally gives you the syntax you are looking for.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;cheers ~ Rory&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Dec 2010 03:58:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749767#M24581</guid>
      <dc:creator>RoryHall</dc:creator>
      <dc:date>2010-12-09T03:58:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable (in SQL) to select by attribute with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749768#M24582</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi how about passing morethan one variable. Iam getting an error message&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; part of the code :-&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;MER&amp;nbsp; = sys.argv[1]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;TWP&amp;nbsp; = sys.argv[2]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;RNG&amp;nbsp; = sys.argv[3]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SEC&amp;nbsp; = sys.argv[4]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression ="MER= "+MER "AND" "TWP= "+TWP "AND" "RNG= "+RNG "AND" "SEC= "+SEC&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Process: Select Layer By Attribute&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", Expression)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;any idea,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 May 2012 21:20:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749768#M24582</guid>
      <dc:creator>DanielAbera</dc:creator>
      <dc:date>2012-05-31T21:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable (in SQL) to select by attribute with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749769#M24583</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;Expression = '"MER"= ' + MER + ' AND "TWP" = ' + TWP + ' AND "RNG" = ' + RNG + ' AND "SEC"= ' + SEC&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jun 2012 10:36:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749769#M24583</guid>
      <dc:creator>FabianBlau</dc:creator>
      <dc:date>2012-06-01T10:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable (in SQL) to select by attribute with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749770#M24584</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If any of your variables are strings instead of integers, they need to be surrounded by single quotes in the expression, which makes things a bit more complicated. I like to use string substitution:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;Expression = "\"MER\"= '%s' AND \"TWP\" = '%s' AND \"RNG\" = '%s' AND \"SEC\"= '%s'" % (MER, TWP, RNG, SEC)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jun 2012 11:45:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749770#M24584</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2012-06-01T11:45:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable (in SQL) to select by attribute with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749771#M24585</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Or even better. Through this method you don't have to worry about what format the variables go through as. Add field/attribute delimiters as needed.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;Expression = "MER = {0} AND TWP = {1} AND RNG = {2} AND SEC = {3}".format(MER,TWP,RNG,SEC)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I should also add this was only implemented in Python 2.6, so ArcGIS 10 and above.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jun 2012 12:31:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749771#M24585</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-06-04T12:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using a variable (in SQL) to select by attribute with Python</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749772#M24586</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks guys !!!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 Aug 2012 16:49:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/using-a-variable-in-sql-to-select-by-attribute/m-p/749772#M24586</guid>
      <dc:creator>DanielAbera</dc:creator>
      <dc:date>2012-08-03T16:49:17Z</dc:date>
    </item>
  </channel>
</rss>

