<?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>Question Re: Using BETWEEN in SQL CASE where clause in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847821#M4878664</link>
    <description>&lt;P&gt;BETWEEN is a &lt;STRONG&gt;comparison&lt;/STRONG&gt; operator, it doesn't assign values, so it's not clear what you want to happen when Len(@Mat2) is not 0.&lt;/P&gt;
&lt;P&gt;Perhaps it would be best to explain how you want the WHERE clause to work.&lt;/P&gt;</description>
    <pubDate>Fri, 12 Dec 2014 16:05:59 GMT</pubDate>
    <dc:creator>justin_willey</dc:creator>
    <dc:date>2014-12-12T16:05:59Z</dc:date>
    <item>
      <title>Using BETWEEN in SQL CASE where clause</title>
      <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaq-p/13847820</link>
      <description>&lt;P&gt;I want a query something similar to the below condition . This does not work...how should I do?&lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE&gt;WHERE
    tblMaterial.mat_type IN(1,3,7) AND

    CASE Len(@Mat2)
    WHEN 0 THEN
        tblMaterial.mat_nr = @Mat1
    ELSE
        tblMaterial.mat_nr Between @Mat1 And @Mat2
    END

ORDER BY
    tblMaterial.mat_nr&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Dec 2014 15:22:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaq-p/13847820</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-12-12T15:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Using BETWEEN in SQL CASE where clause</title>
      <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847821#M4878664</link>
      <description>&lt;P&gt;BETWEEN is a &lt;STRONG&gt;comparison&lt;/STRONG&gt; operator, it doesn't assign values, so it's not clear what you want to happen when Len(@Mat2) is not 0.&lt;/P&gt;
&lt;P&gt;Perhaps it would be best to explain how you want the WHERE clause to work.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Dec 2014 16:05:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847821#M4878664</guid>
      <dc:creator>justin_willey</dc:creator>
      <dc:date>2014-12-12T16:05:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using BETWEEN in SQL CASE where clause</title>
      <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847824#M4878667</link>
      <description>&lt;P&gt;If @Mat2 has no value, should the condition be: tblMaterial.matnr = @Mat1. 
If there is any value in @Mat2 should the condition be: tblMaterial.matnr Between @Mat1 And @Mat2&lt;/P&gt;</description>
      <pubDate>Fri, 12 Dec 2014 16:15:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847824#M4878667</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-12-12T16:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using BETWEEN in SQL CASE where clause</title>
      <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847822#M4878665</link>
      <description>&lt;P&gt;Sadly, the CASE operator must return an expression of some valid SQL Anywhere data type, something that (in theory) can be assigned to a variable of that data type, and there is no boolean (TRUE/FALSE/UNKNOWN) data type that can be stored in a variable.&lt;/P&gt;
&lt;P&gt;That means you can't code this:&lt;/P&gt;
&lt;PRE&gt;WHERE
    tblMaterial.mat_type IN(1,3,7) AND
    CASE Len(@Mat2)
    WHEN 0 THEN
        tblMaterial.mat_nr = @Mat1
    ELSE
        tblMaterial.mat_nr Between @Mat1 And @Mat2
    END
&lt;/PRE&gt;

&lt;P&gt;The following should work, and although it looks quite different from the CASE, it closely agrees with your English explanation of the semantics:&lt;/P&gt;
&lt;PRE&gt;WHERE
    tblMaterial.mat_type IN(1,3,7) 
    AND (    ( Len(@Mat2) = 0  AND tblMaterial.mat_nr = @Mat1 )
          OR ( Len(@Mat2) &amp;lt;&amp;gt; 0 AND tblMaterial.mat_nr Between @Mat1 And @Mat2 ) )
&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 Dec 2014 11:30:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847822#M4878665</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2014-12-13T11:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: Using BETWEEN in SQL CASE where clause</title>
      <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847825#M4878668</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;If @Mat2 has no value&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The above will work unless "@Mat2 has no value" should read "@Mat2 is null" - in the latter case len(@Mat2) will return NULL, not 0.&lt;/P&gt;
&lt;P&gt;In case @Mat2 cannot be null, Breck's condition might be simplified to&lt;/P&gt;
&lt;PRE&gt;WHERE
    tblMaterial.mat_type IN(1,3,7) 
    AND (    tblMaterial.mat_nr = @Mat1
          OR ( Len(@Mat2) &amp;lt;&amp;gt; 0 AND tblMaterial.mat_nr Between @Mat1 And @Mat2 ) )
&lt;/PRE&gt;

&lt;P&gt;since then "tblMaterial.mat_nr = @Mat1" is valid independent of the length of @Mat2.&lt;/P&gt;
&lt;P&gt;Note: I do not say that the shorter condition is easier to understand or maintain, YMMV:)&lt;/P&gt;</description>
      <pubDate>Mon, 15 Dec 2014 04:14:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847825#M4878668</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2014-12-15T04:14:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using BETWEEN in SQL CASE where clause</title>
      <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847826#M4878669</link>
      <description>&lt;P&gt;I've deleted my code sample as it was incorrect - see Breck's / Volker's suggestion&lt;/P&gt;</description>
      <pubDate>Mon, 15 Dec 2014 05:36:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847826#M4878669</guid>
      <dc:creator>justin_willey</dc:creator>
      <dc:date>2014-12-15T05:36:26Z</dc:date>
    </item>
    <item>
      <title>Re: Using BETWEEN in SQL CASE where clause</title>
      <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847827#M4878670</link>
      <description>&lt;P&gt;I have problems with this when I use numeric fields. 
The logic I wish is:&lt;/P&gt;
&lt;P&gt;If @Mat2 is null, should the condition be: tblMaterial.matnr = @Mat1. If there is any value in @Mat2 should the condition be: tblMaterial.matnr Between @Mat1 And @Mat2.  If both @Mat1 and @Mat2 is null will it not be any condition on tblMaterial.matnr.&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;   WHERE
    tblMaterial.mat_type IN(1,3,7) 
    AND (    ( IsNull(@Mat2, 0) = 0  AND tblMaterial.mat_nr = @Mat1 )
          OR ( IsNull(@Mat2, 0) &amp;lt;&amp;gt; 0 AND tblMaterial.mat_nr Between @Mat1 And @Mat2 ) )&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 29 Feb 2016 13:29:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847827#M4878670</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2016-02-29T13:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: Using BETWEEN in SQL CASE where clause</title>
      <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847828#M4878671</link>
      <description>&lt;P&gt;Abowe works only if the value is on @Mat1 and/or @Mat2. If both are null it will not work. If both @Mat1 and @Mat2 are null there will not be any condition on tblMaterial.mat_nr. How do I do that?&lt;/P&gt;</description>
      <pubDate>Mon, 29 Feb 2016 15:00:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847828#M4878671</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2016-02-29T15:00:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using BETWEEN in SQL CASE where clause</title>
      <link>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847823#M4878666</link>
      <description>&lt;P&gt;You can apply the logic you are attempting, but it is done without the CASE. Instead, you need to create logical groupings of OR/AND to combine the BETWEEN with the other matching condition from your case.&lt;/P&gt;
&lt;P&gt;This is because CASE is designed to return a value, rather than to dynamically construct the SQL inside it.&lt;/P&gt;
&lt;P&gt;SELECT *
FROM  table_name
WHERE
  pricekey = 'JUF'
  AND (
    -- Condition 1
    (to_char(to_date(sysdate,'DD-M&lt;A href="http://www.traininginsholinganallur.in/amazon-web-services-training-in-chennai.html"&gt;O&lt;/A&gt;N-YY'), 'DY') = 'MON' AND pydate BETWEEN to_date(sysdate-12,'DD-MON-YY') AND to_date(sysdate-2,'DD-MON-YY'))
    -- Condition 2
    OR (to_char(to_date(sysdate,'DD-MON-YY'), 'DY')='TUE' AND pydate BETWEEN to_date(sysdate-11,'DD-MON-YY') AND to_date(sysdate-1,'DD-MON-YY'))
    -- ELSE case, matching neither of the previous 2
    OR (to_char(to_date(sysdate,'DD-MON-YY'), 'DY') NOT IN ('MON', 'TUE') AND pydate = 'sysdate')
  )&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 00:52:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/using-between-in-sql-case-where-clause/qaa-p/13847823#M4878666</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2016-03-01T00:52:08Z</dc:date>
    </item>
  </channel>
</rss>

