<?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: Little SQL query help please in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385618#M30405</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Perfect Wayne! I'll keep this little function in my toolbox from now on. Thanks again for all your help&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jon&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 20 Dec 2012 16:43:56 GMT</pubDate>
    <dc:creator>JonPedder</dc:creator>
    <dc:date>2012-12-20T16:43:56Z</dc:date>
    <item>
      <title>Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385606#M30393</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I always seem to get hung up on these simple strings.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here again I don???t quite get the syntax for use in SelectLayerByAttribute_management&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I use the following with a field named ???Name??? the query works just fine&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;iQuery = ' "Name" = \'Joe Blogs\' ' arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", iQuery)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However when I try to use a variable I can never seem to get the syntax right&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;iPerson = ???Joe Blogs??? iQuery = '"Name" = "' + aCenterMapOn + '"' arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", iQuery)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Dec 2012 15:05:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385606#M30393</guid>
      <dc:creator>JonPedder</dc:creator>
      <dc:date>2012-12-19T15:05:55Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385607#M30394</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can try this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# your code
iQuery = ' "Name" = \'Joe Blogs\' '
print iQuery
# modified code
print "\"%s\" = '%s'" % ("Name", "Joe Blogs")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can substitute the Name and Value with a variable to make this more dynamic.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would also look at the &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/#/AddFieldDelimiters/018v0000006p000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;AddFieldDelimiters()&lt;/A&gt;&lt;SPAN&gt; in arcpy to ensure every field is formatted correctly.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:43:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385607#M30394</guid>
      <dc:creator>AndrewChapkowski</dc:creator>
      <dc:date>2021-12-11T17:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385608#M30395</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Sorry, you've totally lost me!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Dec 2012 16:26:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385608#M30395</guid>
      <dc:creator>JonPedder</dc:creator>
      <dc:date>2012-12-19T16:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385609#M30396</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here's an example, employing the AddFieldDelimiters method - and since using IDLE and an SDE-contained fc instead of a ready feature layer (the required input for SelectLayerByAttributes), I used a similar tool MakeFeatureLayer to demonstrate the use of query syntax, but I am not yet accustomed to var substitution, so I'd take a keen look at Andrew's example.&amp;nbsp; Here's my simpler one:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; arcpy.env.workspace = r'Database Connections\MCPA.sde'
&amp;gt;&amp;gt;&amp;gt; inFC = 'MCPA.DBO.PARCEL_PUBLIC'
&amp;gt;&amp;gt;&amp;gt; qryFieldName = arcpy.AddFieldDelimiters(inFC, 'RECHAR')
&amp;gt;&amp;gt;&amp;gt; print qryFieldName

"RECHAR"

&amp;gt;&amp;gt;&amp;gt; qry = qryFieldName + " = '" + aCenterMapOn + "'"
&amp;gt;&amp;gt;&amp;gt; print qry

"RECHAR" = '00166972-004500'

(syntax):
&amp;gt;&amp;gt;&amp;gt; # SelectLayerByAttribute_management (in_layer_or_view, {selection_type}, {where_clause})
&amp;gt;&amp;gt;&amp;gt; # MakeFeatureLayer_management (in_features, out_layer, {where_clause}, {workspace}, {field_info})
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; arcpy.MakeFeatureLayer_management(inFC, 'lyr', qry)

&amp;lt;Result 'lyr'&amp;gt;

(getting the count to verify the selection took place):
&amp;gt;&amp;gt;&amp;gt; arcpy.GetCount_management('lyr').getOutput(0)

u'1'
&amp;gt;&amp;gt;&amp;gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:43:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385609#M30396</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-11T17:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385610#M30397</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Sorry, you've totally lost me!&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Which part are you lost on?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Dec 2012 16:31:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385610#M30397</guid>
      <dc:creator>AndrewChapkowski</dc:creator>
      <dc:date>2012-12-19T16:31:26Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385611#M30398</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks very much! Now I "get it" the arcpy.AddFieldDelimiters and the help from Wayne did the trick. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks !!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Dec 2012 17:16:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385611#M30398</guid>
      <dc:creator>JonPedder</dc:creator>
      <dc:date>2012-12-19T17:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385612#M30399</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Great, and just so we 'get it more' while on the effort, always a good thing -- as suggested by Andrew, the 'variable string substitution' (not sure what it's called, but it's a purely Pythonic technique you can borrow to help make your query syntax more manageable), here's what you get when you combine or build on the above:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; qry2 = "%s = '%s'" % (qryFieldName, aCenterMapOn)
&amp;gt;&amp;gt;&amp;gt; print qry2

"RECHAR" = '00166972-004500'
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So to offer explanation, if you strip of the double quotes in the above expression component, you get:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;%s = '%s'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...then %s is the substitution in the order provided in the paren (after the 3rd unquoted %).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The field delimiters in this case are taken care of by AddFieldDelimiters and already stored in qryFieldName - that's a direct substitution (%s).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The second param required the single quotes as querying for text values requires ('%s').&amp;nbsp; Had we been querying for numeric, then that would be a direct substitution as well (%s).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks to Andrew at ESRI.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:43:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385612#M30399</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-11T17:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385613#M30400</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Guys, I'm hung up on one more of these, jeeze!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;querying a text field for fields that are not empty. My search string that works in the attribute table is &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;iField = arcpy.AddFieldDelimiters(fc, "Area_Name")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;iQuery = iField+ " IS NOT NULL"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This doesn't want to work in arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", iQuery)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Killin' me !&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2012 03:42:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385613#M30400</guid>
      <dc:creator>JonPedder</dc:creator>
      <dc:date>2012-12-20T03:42:41Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385614#M30401</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Have you tried IS NULL and get anything?&amp;nbsp; Are errors being returned or is it just not working?&amp;nbsp; If IS NULL works you can execute select by attribute again with SWITCH_SELECTION.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2012 05:29:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385614#M30401</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2012-12-20T05:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385615#M30402</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm getting a syntax error when using arcpy.SelectLayerByAttribute_management but the actual query works perfectly when running from within an attribute table and using Select By Attribute..&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jon&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2012 11:32:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385615#M30402</guid>
      <dc:creator>JonPedder</dc:creator>
      <dc:date>2012-12-20T11:32:27Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385616#M30403</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I would try working your way through it. Maybe start simple to figure out where your error is coming from. Try using the python window and use actual feature class names then do again declaring variables and see where the issue is coming from. That's usually how I work my way through&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2012 11:41:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385616#M30403</guid>
      <dc:creator>CarlSunderman</dc:creator>
      <dc:date>2012-12-20T11:41:42Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385617#M30404</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Okay, sorry for the delay - but see the below, what I did that works (employing the techniques already discussed above).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I used a gdb feature class that I added a 'blank' row to in order to introduce the null vals (so I know I should get results).&amp;nbsp; Comments in the code below (again, it's an IDLE session):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;IDLE 2.6.5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;gt;&amp;gt;&amp;gt; import arcpy &amp;gt;&amp;gt;&amp;gt; fc = r'C:\Documents and Settings\whitley-wayne\Desktop\stage.gdb\RightBankPoints'&amp;nbsp; # (make sure the fc exists) &amp;gt;&amp;gt;&amp;gt; if arcpy.Exists(fc):print 'true'&amp;nbsp;&amp;nbsp; true&amp;nbsp; # (could have entered the qry here for MFL, but you wanted to demo SelectLayerByAtt, which I think requires a layer) # (...so created a lyr first to feed into SelectLayerByAtt, a step only for demo purposes...) &amp;gt;&amp;gt;&amp;gt; arcpy.MakeFeatureLayer_management(fc, 'lyr') &amp;lt;Result 'lyr'&amp;gt;&amp;nbsp; # (verifying there are features in the lyr): &amp;gt;&amp;gt;&amp;gt; arcpy.GetCount_management('lyr').getOutput(0) u'198'&amp;nbsp; # (setting up the field with the correct delimiters [double quotes]): &amp;gt;&amp;gt;&amp;gt; selFld = arcpy.AddFieldDelimiters(fc,'RiverCode')&amp;nbsp; # (just checking...) &amp;gt;&amp;gt;&amp;gt; print selFld "RiverCode"&amp;nbsp; # (setting up the qry string): &amp;gt;&amp;gt;&amp;gt; qry = "%s %s" % (selFld, 'is not Null')&amp;nbsp; # (looks fine, ready for launch...) &amp;gt;&amp;gt;&amp;gt; print qry "RiverCode" is not Null&amp;nbsp; # (commence launch, the execution we've been waiting for): &amp;gt;&amp;gt;&amp;gt; arcpy.SelectLayerByAttribute_management('lyr', 'NEW_SELECTION', qry) &amp;lt;Result 'lyr'&amp;gt;&amp;nbsp; # (checking the count -- I entered 1 null record, so the count should be at least 1 less than before): &amp;gt;&amp;gt;&amp;gt; arcpy.GetCount_management('lyr').getOutput(0) u'197' &amp;gt;&amp;gt;&amp;gt;&amp;nbsp; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And it is.&amp;nbsp; Mission accomplished, hope that helps you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2012 13:03:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385617#M30404</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2012-12-20T13:03:05Z</dc:date>
    </item>
    <item>
      <title>Re: Little SQL query help please</title>
      <link>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385618#M30405</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Perfect Wayne! I'll keep this little function in my toolbox from now on. Thanks again for all your help&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jon&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Dec 2012 16:43:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/little-sql-query-help-please/m-p/385618#M30405</guid>
      <dc:creator>JonPedder</dc:creator>
      <dc:date>2012-12-20T16:43:56Z</dc:date>
    </item>
  </channel>
</rss>

