<?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: ArcGIS 10.2 - SQL syntax with quote in Python to build a layer? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31135#M2470</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks, it did help but I could not make the syntax work, so I wrote:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;"NAME " + "LIKE " + "'" +strField_NAME.replace("'", "''") + "'"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;...but I could have replaced the single quote with anything because this SQL is just to make a feature layer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, I could use the same technique with any other character Python and/or SQL would not like.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 03 Feb 2015 13:50:30 GMT</pubDate>
    <dc:creator>VincentLaunstorfer</dc:creator>
    <dc:date>2015-02-03T13:50:30Z</dc:date>
    <item>
      <title>ArcGIS 10.2 - SQL syntax with quote in Python to build a layer?</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31129#M2464</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a python tool in which an SQL query string make a layer:&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"NAME " + "LIKE " + "'" + strField_NAME + "'"&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, it appears that I have some names containing quotes in my dataset! For example, Jimmy's Peak containing a single quote and thus crashing my script...&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any sort of escape character I could use in a Python script in order to build an SQL query?&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the time being, the query builds as NAME LIKE + 'Jimmy' because the name contains a single quote... and it should be NAME LIKE + Jimmy's Peak&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Feb 2015 15:35:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31129#M2464</guid>
      <dc:creator>VincentLaunstorfer</dc:creator>
      <dc:date>2015-02-02T15:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS 10.2 - SQL syntax with quote in Python to build a layer?</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31130#M2465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I believe you can use a &lt;A href="http://en.wikipedia.org/wiki/Grave_accent"&gt;backtick (grave accent)&lt;/A&gt; instead of a single quotation mark to wrap the text value in your query.&amp;nbsp; So, your python tool SQL query string would become:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;"NAME " + "LIKE " + "`" + strField_NAME + "`"&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Feb 2015 16:32:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31130#M2465</guid>
      <dc:creator>ErinBrimhall</dc:creator>
      <dc:date>2015-02-02T16:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS 10.2 - SQL syntax with quote in Python to build a layer?</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31131#M2466</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks but unfortunately, it does'nt work!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It looks silly but I don't know how to select text value which include quotation marks...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Feb 2015 17:19:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31131#M2466</guid>
      <dc:creator>VincentLaunstorfer</dc:creator>
      <dc:date>2015-02-02T17:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS 10.2 - SQL syntax with quote in Python to build a layer?</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31132#M2467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If I manually perform this query in a fgdb featureclass, then it creates a SQL like this:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;NAME LIKE 'Jimmy''s Peak'&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So I assume in Python you could do something like:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;name = "Jimmy's Peak"
sql = "NAME LIKE '{0}'".format(name.replace("'", "''"))
print sql

# returns:
&amp;gt;&amp;gt; NAME LIKE 'Jimmy''s Peak'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:15:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31132#M2467</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-10T21:15:03Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS 10.2 - SQL syntax with quote in Python to build a layer?</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31133#M2468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It is helpful to review the Help for&lt;SPAN class="Apple-converted-space"&gt; &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00s50000002t000000" rel="nofollow" style="padding: 0px calc(12px + 0.35ex) 0px 0px; color: #287433; font-family: inherit; font-size: 14px; font-style: inherit; font-weight: inherit;" target="_blank"&gt;Building a query expression&lt;/A&gt;, where it states:&lt;/P&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;SPAN style="color: #4d4d4d; text-indent: 0px; font-family: inherit; font-size: 14px; font-style: inherit; font-weight: inherit;"&gt;If the string contains a single quote you will first need to use another single quote as an escape character.&lt;/SPAN&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In Python, the backslash is the dominant escape character, but there are some additional escape sequences. ArcGIS wants users to escape a single quote by using another single quote.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I see that &lt;A href="https://community.esri.com/migrated-users/3100"&gt;Xander Bakker&lt;/A&gt;‌ beat me to the punch with the actual code.&amp;nbsp; That's what getting distracted while replying gets me. : )&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Feb 2015 17:43:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31133#M2468</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-02-02T17:43:24Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS 10.2 - SQL syntax with quote in Python to build a layer?</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31134#M2469</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The least I can do is mark your answer as helpful... &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Feb 2015 17:45:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31134#M2469</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-02-02T17:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS 10.2 - SQL syntax with quote in Python to build a layer?</title>
      <link>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31135#M2470</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks, it did help but I could not make the syntax work, so I wrote:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;"NAME " + "LIKE " + "'" +strField_NAME.replace("'", "''") + "'"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;...but I could have replaced the single quote with anything because this SQL is just to make a feature layer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, I could use the same technique with any other character Python and/or SQL would not like.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Feb 2015 13:50:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcgis-10-2-sql-syntax-with-quote-in-python-to/m-p/31135#M2470</guid>
      <dc:creator>VincentLaunstorfer</dc:creator>
      <dc:date>2015-02-03T13:50:30Z</dc:date>
    </item>
  </channel>
</rss>

