<?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: quotes and double quotes in an SQL expression in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144874#M11281</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Zdenek,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I believe your expression should be the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14543387718963799 jive_text_macro" data-renderedposition="92_8_912_16" jivemacro_uid="_14543387718963799"&gt;&lt;P&gt;sqlprikaz = "Layer LIKE '%PLOCH%' AND Layer &amp;lt;&amp;gt; '%_'"&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The entire expression will be in double quotes, and each string you are searching for (or not search for) will be in single quotes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 01 Feb 2016 14:59:39 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2016-02-01T14:59:39Z</dc:date>
    <item>
      <title>quotes and double quotes in an SQL expression</title>
      <link>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144873#M11280</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I have a layer with a string attribute Layer and I want to select every record that contains string PLOCH anywhere in the whole string and doesn't end on character _ using Python script.&lt;/P&gt;&lt;P&gt;My expression is sqlprikaz='"Layer" LIKE ' + "'%PLOCH%'" and "Layer" &amp;lt;&amp;gt; "%_"&lt;/P&gt;&lt;P&gt;First I had only the first part of the expression and it worked but when I add the second part I have a problem where quotes and double quotes should to be and the expression doesn't work.&lt;/P&gt;&lt;P&gt;Can anyone explain me the rules of quotes and double quotes in expressions with more conditions please?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Feb 2016 14:23:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144873#M11280</guid>
      <dc:creator>ZdeněkSoldán</dc:creator>
      <dc:date>2016-02-01T14:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: quotes and double quotes in an SQL expression</title>
      <link>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144874#M11281</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Zdenek,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I believe your expression should be the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14543387718963799 jive_text_macro" data-renderedposition="92_8_912_16" jivemacro_uid="_14543387718963799"&gt;&lt;P&gt;sqlprikaz = "Layer LIKE '%PLOCH%' AND Layer &amp;lt;&amp;gt; '%_'"&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The entire expression will be in double quotes, and each string you are searching for (or not search for) will be in single quotes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Feb 2016 14:59:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144874#M11281</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2016-02-01T14:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: quotes and double quotes in an SQL expression</title>
      <link>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144875#M11282</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think you also need to use NOT LIKE instead of &amp;lt;&amp;gt; so the percentage sign works in the second part.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sqlprikaz = &lt;SPAN class="string"&gt;"Layer LIKE '%PLOCH%' AND Layer NOT LIKE '%_'"&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Feb 2016 15:27:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144875#M11282</guid>
      <dc:creator>BenLeslie1</dc:creator>
      <dc:date>2016-02-01T15:27:51Z</dc:date>
    </item>
    <item>
      <title>Re: quotes and double quotes in an SQL expression</title>
      <link>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144876#M11283</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;Hi Zdenek,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;I propose that you use the following:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="_jivemacro_uid_14543551462403483 jive_macro_code jive_text_macro" data-renderedposition="92_8_912_16" jivemacro_uid="_14543551462403483"&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;sqlprikaz = "Layer LIKE '%PLOCH%' AND NAME NOT LIKE '%_'"&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I recommend you have a look at&amp;nbsp; &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//03q300000091000000"&gt;AddFieldDelimiters&lt;/A&gt; and&amp;nbsp; &lt;A href="http://www.pythonforbeginners.com/regex/regular-expressions-in-python"&gt;Regular Expressions&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Feb 2016 19:48:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144876#M11283</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2016-02-01T19:48:32Z</dc:date>
    </item>
    <item>
      <title>Re: quotes and double quotes in an SQL expression</title>
      <link>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144877#M11284</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could use the modern &lt;STRONG&gt;format()&lt;/STRONG&gt; unction for strings to more easily assemble SQL strings from variables.&lt;/P&gt;&lt;P&gt;This is easy to then just use single quotes around SQL string expressions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to enclose sql variable names in double quotes, use&lt;SPAN style="text-decoration: underline;"&gt; treble quotes&lt;/SPAN&gt; to enclose the expression.&lt;/P&gt;&lt;P&gt;You don't need to use double quotes if the variable names are valid names, no spaces and not reserved words and are not using Microsoft Access. Who uses personal geodatabases any more? I don't bother with the field delimeter function personally because I know my target database is not Access.&lt;/P&gt;&lt;P&gt;If you always use treble quotes it is nicely highlighted in my IDE (pyscripter) differently from other strings.&lt;/P&gt;&lt;P&gt;Note that the format statement allows multiple references to one variable, reducing duplication.&lt;/P&gt;&lt;P&gt;It is also easier to strip unicode prefixes from strings in the function using a encode function if necessary.&lt;/P&gt;&lt;P&gt;The format expression is much easier to read because it is closer to the literal string and is easier to debug by removing most of the quote characters.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;sqlprikaz='"Layer" LIKE ' + "'%PLOCH%'" and "Layer" &amp;lt;&amp;gt; "%_"&lt;/SPAN&gt;

&lt;SPAN style="font-family: courier new,courier;"&gt;layer = 'Layer_name'&lt;/SPAN&gt;
&lt;SPAN style="font-family: courier new,courier;"&gt;search_string = 'PLOCH'&lt;/SPAN&gt;
&lt;SPAN style="font-family: courier new,courier;"&gt;sqlprikaz = """{0} LIKE '%{1}%' AND {0} NOT LIKE '%_'""".format(layer,search_string)&lt;/SPAN&gt;
&lt;SPAN style="font-family: courier new,courier;"&gt;print sqlprikaz&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:55:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/quotes-and-double-quotes-in-an-sql-expression/m-p/144877#M11284</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2021-12-11T07:55:48Z</dc:date>
    </item>
  </channel>
</rss>

