<?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: I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188244#M14454</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;the expression should be modified to the following:&lt;BR /&gt;&lt;BR /&gt;Round(Round(NumberField, 0) / 2, 0) = Round(NumberField, 0) / 2&lt;BR /&gt;Round(Round(NumberField, 0) / 2, 0) &amp;lt;&amp;gt; Round(NumberField, 0) / 2&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wow...this is pretty cool.&amp;nbsp; I also vote for Richards answer.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 19 Jun 2013 14:03:35 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2013-06-19T14:03:35Z</dc:date>
    <item>
      <title>I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY</title>
      <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188237#M14447</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I want to construct an SQL Statement to select even or odd FIDs/OBJECTIDs using Arcpy, I tried MOD("FID", 2) = 1, this is not working from my python window but it is from my table view. Any suggestions please?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Jun 2013 11:43:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188237#M14447</guid>
      <dc:creator>OLANIYANOLAKUNLE</dc:creator>
      <dc:date>2013-06-19T11:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY</title>
      <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188238#M14448</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Not sure about a full SQL statement (maybe post up what you have so far), but this python function will check if the value you pass in is even or odd...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def EvenOrOdd(val):
&amp;nbsp; x = int(val)
&amp;nbsp; if x % 2 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return "EVEN"
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return "ODD"

msg = EvenOrOdd(6)
print msg
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:30:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188238#M14448</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T09:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY</title>
      <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188239#M14449</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I want to use Arcpy and not ordinary PYTHON&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Jun 2013 12:22:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188239#M14449</guid>
      <dc:creator>OLANIYANOLAKUNLE</dc:creator>
      <dc:date>2013-06-19T12:22:54Z</dc:date>
    </item>
    <item>
      <title>Re: I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY</title>
      <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188240#M14450</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I do not know of any arcpy functions to do this...You will need to use the method that James provided using the modulus.&amp;nbsp; You could do something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
fc = r'G:\Data\Geodatabase\Cedar_County.gdb\JURISDICTION\CORP_LIM'
oid = arcpy.Describe(fc).OIDFieldName
even = []
odd = []
rows = arcpy.SearchCursor(fc)
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; fid = row.getValue(oid)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if fid %2 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; even.append(fid)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; odd.append(fid)
del row, rows

field = arcpy.AddFieldDelimiters(fc, oid)
even_sql = ' OR '.join('%s = %s' %(field,i) for i in even)
odd_sql = ' OR '.join('%s = %s' %(field,i) for i in odd)

# Make Feature layers
lyr = arcpy.MakeFeatureLayer_management(fc, 'Temp_layer')

# Select Even
arcpy.SelectLayerByAttribute_management(lyr, 'NEW_SELECTION', even_sql)

# Select odd
arcpy.SelectLayerByAttribute_management(lyr, 'NEW_SELECTION', odd_sql) 
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;OR, if you have a lot of features it may not be practical to build a very long query.&amp;nbsp; You may want to temporarily add a field to tell whether the fid is even or odd (these fid's are subject to change with shapefiles so be careful) and select using that new field:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
fc = r'G:\Data\Geodatabase\Cedar_County.gdb\JURISDICTION\CORP_LIM'
oid = arcpy.Describe(fc).OIDFieldName

#Add Field to tell if even/odd
arcpy.AddField_management(fc, 'Type', 'TEXT',4)
rows = arcpy.UpdateCursor(fc)
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; fid = row.getValue(oid)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if fid %2 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Type = 'Even'
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Type = 'Odd'
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
del row, rows

typeField = arcpy.AddFieldDelimiters(fc, 'Type')
even_sql = "%s = 'Even'" %typeField
odd_sql = "%s = 'Odd'" %typeField

# Make Feature layers
lyr = arcpy.MakeFeatureLayer_management(fc, 'Temp_layer')

# Select Even
arcpy.SelectLayerByAttribute_management(lyr, 'NEW_SELECTION', even_sql)

# Select odd
arcpy.SelectLayerByAttribute_management(lyr, 'NEW_SELECTION', odd_sql)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:30:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188240#M14450</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T09:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY</title>
      <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188241#M14451</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I was working out the SQL portion and noticed that Caleb has an excellent solution!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks -- this is useful.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Edit: here's what I was working on.&amp;nbsp; I just incorporated the sql from Caleb's example...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def select_Even():
&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")
&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name=='thelayernameintheTOC':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; even = []
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor = arcpy.SearchCursor(lyr)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if int(row.OBJECTID) % 2 == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; even.append(int(row.OBJECTID))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; field = "OBJECTID"&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; even_sql = ' OR '.join('%s = %s' %(field,i) for i in even)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, 'NEW_SELECTION', even_sql)

&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("layer not found")

select_Even()
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:30:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188241#M14451</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T09:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY</title>
      <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188242#M14452</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I was working out the SQL portion and noticed that Caleb has an excellent solution!&lt;BR /&gt;&lt;BR /&gt;Thanks -- this is useful.&lt;BR /&gt;&lt;BR /&gt;Edit: here's what I was working on.&amp;nbsp; I just incorporated the sql from Caleb's example...&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def select_Even(): &amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") &amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] &amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd): &amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name=='thelayernameintheTOC': &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; even = [] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor = arcpy.SearchCursor(lyr)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if int(row.OBJECTID) % 2 == 0: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; even.append(int(row.OBJECTID)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; field = "OBJECTID"&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; even_sql = ' OR '.join('%s = %s' %(field,i) for i in even) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, 'NEW_SELECTION', even_sql)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("layer not found")&amp;nbsp; select_Even()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The SQL Modulus statement in the original post would work for some databases if it had been used with the SelectLayerByAttributes tool, but it would not work for all databases.&amp;nbsp; Two SQL statements that work for virtually every database that do not require use of the modulus operator (which varies widely in its format from database to database) would be the following (the first statement selects even numbers and second selects odd):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Round(NumberField / 2, 0) = NumberField / 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Round(NumberField / 2, 0) &amp;lt;&amp;gt; NumberField / 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just make that the SQL statement (obviously substituting your real field name with correct field delimiters for NumberField) and make it the query expression for the SelectLayerByAttribute tool.&amp;nbsp; The statements above assume the numbers you are evaluating are integers. If the numbers are doubles with fractional portions, the expression should be modified to the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Round(Round(NumberField, 0) / 2, 0) = Round(NumberField, 0) / 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Round(Round(NumberField, 0) / 2, 0) &amp;lt;&amp;gt; Round(NumberField, 0) / 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Appending ObjectIDs into an SQL list is not a good approach if your database is very large (100K+ records), because the list will become huge and will take a long time to process (assuming it does not just fail).&amp;nbsp; Plus by reading the database with a cursor you are effectively doubling the database read time, since the SelectLayerByAttribute tool will have to read the entire database again to select everything, making that approach very inefficient.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Jun 2013 13:25:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188242#M14452</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2013-06-19T13:25:43Z</dc:date>
    </item>
    <item>
      <title>Re: I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY</title>
      <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188243#M14453</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;...&lt;BR /&gt;Round(NumberField / 2, 0) = NumberField / 2&lt;BR /&gt;Round(NumberField / 2, 0) &amp;lt;&amp;gt; NumberField / 2&lt;BR /&gt;...&lt;BR /&gt;Appending ObjectIDs into an SQL list is not a good approach if your database is very large (100K+ records), because the list will become huge and will take a long time to process (assuming it does not just fail).&amp;nbsp; Plus by reading the database with a cursor you are effectively doubling the database read time, since the SelectLayerByAttribute tool will have to read the entire database again to select everything, making that approach very inefficient.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;For OID filtering this is the best answer here.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Jun 2013 13:54:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188243#M14453</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-06-19T13:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY</title>
      <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188244#M14454</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;the expression should be modified to the following:&lt;BR /&gt;&lt;BR /&gt;Round(Round(NumberField, 0) / 2, 0) = Round(NumberField, 0) / 2&lt;BR /&gt;Round(Round(NumberField, 0) / 2, 0) &amp;lt;&amp;gt; Round(NumberField, 0) / 2&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wow...this is pretty cool.&amp;nbsp; I also vote for Richards answer.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Jun 2013 14:03:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188244#M14454</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-06-19T14:03:35Z</dc:date>
    </item>
    <item>
      <title>Re: I WANT TO CONSTRUCT AN SQL STATEMENT TO SELECT EVEN OR ODD FIDs USING ARCPY</title>
      <link>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188245#M14455</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Wow...this is pretty cool.&amp;nbsp; I also vote for Richards answer.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;^this x2!&amp;nbsp; Voted -- thanks Richard!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Jun 2013 14:24:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/i-want-to-construct-an-sql-statement-to-select/m-p/188245#M14455</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2013-06-19T14:24:12Z</dc:date>
    </item>
  </channel>
</rss>

