<?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 syntax for Python API FeatureLayer.calculate in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877458#M4977</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Not a typo. It&amp;nbsp;is&amp;nbsp;the real shape length that is&amp;nbsp;usually found in&amp;nbsp;a geodatabase feature class. If you open the attached image you will see a snapshot of the attribute table with the field names.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 08 Mar 2018 21:13:01 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2018-03-08T21:13:01Z</dc:date>
    <item>
      <title>SQL syntax for Python API FeatureLayer.calculate</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877454#M4973</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I need to update the values of a column in an ArcGIS online feature service with the value of another row. See the table below where Length(ft) (data type short int) is the target column and the Shape__Length column (data type = double) is the source.&lt;/P&gt;&lt;P&gt;Since this will need to be done for thousands of records periodically I want to create a script that does this using the Python API for ArcGIS. It looks like the arcgis.features module has a &lt;A href="https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#featurelayer"&gt;FeatureLayer.calculate&lt;/A&gt; method that should do this.&lt;/P&gt;&lt;P&gt;To test it I ran the&amp;nbsp;lines below which successfully updates the value of the Length (ft) column.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;From arcgis import GIS, features&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;mygis = GIS (“arcgis.com”, username, password)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;GMitem = mygis.content.get(itemid)&amp;nbsp; &amp;nbsp; &amp;nbsp;###where item is a feature layer collection&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;test = GMitem.layers[9]&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ###get the 10&lt;SUP&gt;th&lt;/SUP&gt; layer in the feature layer collection (see attribute table above)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;print (test.calculate(where= "1=1",calc_expression={"field": "Length", "value": "1"}))&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, when I alter the SQL to the below it throws an error saying it can’t convert “Shape__Length” to a small int so I know it’s reading my column name as text.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;print (test.calculate(where= "1=1",calc_expression={"field": "Length", "value": "Shape__Length"}))&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What is the correct syntax to get&amp;nbsp;a field to accept the values of another field using SQL expressions in the Python API??&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Mar 2018 16:09:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877454#M4973</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2018-03-08T16:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: SQL syntax for Python API FeatureLayer.calculate</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877455#M4974</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sounds like your Length fields was defined as a small integer, whereas the shapefield and hence shape_length would be a double type.&amp;nbsp; Try making a new input field of the correct type since the shape_length is probably too big and/or of the wrong format to fit into a small integer field.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Mar 2018 17:07:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877455#M4974</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2018-03-08T17:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: SQL syntax for Python API FeatureLayer.calculate</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877456#M4975</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I created a new field with type double and tried but got the same result. It seems like a syntax problem where "Shape__Length" is being recognized as a nvarchar (text) rather the field name from which I want to copy values.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Mar 2018 19:33:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877456#M4975</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2018-03-08T19:33:48Z</dc:date>
    </item>
    <item>
      <title>Re: SQL syntax for Python API FeatureLayer.calculate</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877457#M4976</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I thought the double underscore __ was a typo before, so it isn't a real Shape_length as one would expect in a featureclass in a gdb then?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Mar 2018 20:25:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877457#M4976</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2018-03-08T20:25:59Z</dc:date>
    </item>
    <item>
      <title>Re: SQL syntax for Python API FeatureLayer.calculate</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877458#M4977</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Not a typo. It&amp;nbsp;is&amp;nbsp;the real shape length that is&amp;nbsp;usually found in&amp;nbsp;a geodatabase feature class. If you open the attached image you will see a snapshot of the attribute table with the field names.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Mar 2018 21:13:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877458#M4977</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2018-03-08T21:13:01Z</dc:date>
    </item>
    <item>
      <title>Re: SQL syntax for Python API FeatureLayer.calculate</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877459#M4978</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The ArcGIS API for Python is mostly a Python wrapper for the ArcGIS REST API.&amp;nbsp; See &lt;A class="link-titled" href="https://developers.arcgis.com/rest/services-reference/calculate-feature-service-layer-.htm" title="https://developers.arcgis.com/rest/services-reference/calculate-feature-service-layer-.htm"&gt;Calculate (Feature Service/Layer)—ArcGIS REST API: Services Directory | ArcGIS for Developers&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;test&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;calculate&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;where&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"1=1"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;calc_expression&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"field"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Length"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"sqlExpression"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Shape__Length"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Mar 2018 21:24:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877459#M4978</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2018-03-08T21:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: SQL syntax for Python API FeatureLayer.calculate</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877460#M4979</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Edit&lt;/STRONG&gt; - See Joshua's answer. That is the doc page I was looking for. This still works though.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;You are correct in saying that it is reading the field name as a string rather than a 'field name'. So, it is trying to put that string for all the rows which it cannot do because it is an int type. Unfortunately the documentation is not great here. The docs tell us to '&lt;/SPAN&gt;&lt;SPAN style="color: #404040; background-color: #fcfcfc;"&gt;See Calculate a field for more information on supported expressions' with no link to where that is! Here is my workaround though:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;GMitem &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; mygis&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;content&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;get&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;itemid&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
data &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; GMitem&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;layers&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;9&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;query&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="comment token"&gt;# Get the feature collection&lt;/SPAN&gt;
df &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; data&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;df &lt;SPAN class="comment token"&gt;# Put it into a pandas dataframe&lt;/SPAN&gt;
df&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'field1'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; df&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'field2'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="comment token"&gt;# Calculate the field and apply updates in next line&lt;/SPAN&gt;
GMitem&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;layers&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;9&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;edit_features&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;updates&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;arcgis&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;features&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;FeatureSet&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;from_dataframe&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;df&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If this succeeded you should see something like this returned:&lt;/P&gt;&lt;P style="color: #000000; background-color: #ffffff; border: 0px; font-size: 14px;"&gt;{'addResults': [], 'deleteResults': [], 'updateResults': [{'globalId': 'cbb4d354-2e16-4d05-8943-f80280643188', 'objectId': 1, 'success': True},...&lt;BR /&gt;&lt;BR /&gt;You can double check this by re-querying the layer and displaying it as a dataframe and checking the column, or by accessing the AGO item with a browser refresh. This is a few extra lines of code but working with pandas is fast and powerful! Hope this helps! Also, I used Jupyter Notebooks for this and that is where I tested this code.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 11:01:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877460#M4979</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T11:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: SQL syntax for Python API FeatureLayer.calculate</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877461#M4980</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks, that is the documentation I&amp;nbsp;hoping existed. It worked.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 09 Mar 2018 12:55:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877461#M4980</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2018-03-09T12:55:45Z</dc:date>
    </item>
    <item>
      <title>Re: SQL syntax for Python API FeatureLayer.calculate</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877462#M4981</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If your question has been answered, or mostly answered, please mark one of the responses correct to close out the question.&amp;nbsp; Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 09 Mar 2018 14:42:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/sql-syntax-for-python-api-featurelayer-calculate/m-p/877462#M4981</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2018-03-09T14:42:07Z</dc:date>
    </item>
  </channel>
</rss>

