<?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: Auto convert and join in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829159#M4860002</link>
    <description>&lt;P&gt;While a little late to the thread . . . A simpler rewrite might be this one:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select *  from TestData  left outer join JoinId
        on    CAST(JoinId.Id as varchar(10))
     = if TestData.Fieldname = 'Id' then TestData.TestData else null endif;&lt;/LI-CODE&gt;


&lt;P&gt;The CASTing (or alternatively a Convert()) of JoinId.Id to a character string
avoids the implicit/automatic conversion that is exposing this issue.   &lt;/P&gt;
&lt;P&gt;I'm assuming 12.0.x did something similar when evaluating the original predicate.&lt;/P&gt;
&lt;P&gt;Both types of automatic conversion are feasible here, ie:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;  - automatically converting a numeric term to character in a character expression
  - automatically converting a character term to a numeric in a numeric expression&lt;/LI-CODE&gt;


&lt;P&gt;and since this predicate 'could operate' either way (as a numeric comparison or
a character comparison) there seems to be no preferred bias to either.&lt;/P&gt;
&lt;P&gt;I'm not quite certain if this is exactly a bug or not. Treating all such comparisons
as numeric comparisons whenever possible/feasible can be significantly more efficient 
than assuming every such expression to be a character string comparison.&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jul 2016 17:53:25 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2016-07-11T17:53:25Z</dc:date>
    <item>
      <title>Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaq-p/13829157</link>
      <description>&lt;P&gt;If you have a table with a varchar column containing different kinds of values (int, date, string, float) all converted to a string and you then want to join the int value to a different table. I'm getting errors like "Cannot convert "some date value" to a numeric".&lt;/P&gt;
&lt;P&gt;Setup the tables:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;CREATE TABLE "DBA"."TestData" (
    "FieldName" VARCHAR(10) NULL,
    "TestData" VARCHAR(10) NULL
) IN "system";

INSERT INTO "DBA"."TestData" ("FieldName","TestData") VALUES('Id','1');
INSERT INTO "DBA"."TestData" ("FieldName","TestData") VALUES('OtherId','1');
INSERT INTO "DBA"."TestData" ("FieldName","TestData") VALUES('Date','2015-01-01');
INSERT INTO "DBA"."TestData" ("FieldName","TestData") VALUES('Text','Bla');
INSERT INTO "DBA"."TestData" ("FieldName","TestData") VALUES('Float','1.15')

CREATE TABLE "DBA"."JoinId" (
    "Id" INTEGER NULL
) IN "system";

INSERT INTO "DBA"."JoinId" ("Id") VALUES(1);
INSERT INTO "DBA"."JoinId" ("Id") VALUES(2);

CREATE FUNCTION "DBA"."IsIntegerValue1"(string VarChar(10))
returns bit
begin
  declare isInt bit;

  if string = '1' then 
    set isInt = 1
  else
    set isInt = 0
  endif;

  return isInt;
end;&lt;/LI-CODE&gt;


&lt;P&gt;The result I am looking for:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;FieldName | TestData   | Id
-----------------------------
Id        | 1          | 1
OtherId   | 1          | NULL
Date      | 2015-01-01 | NULL
Text      | Bla        | NULL
Float     | 1.15       | NULL&lt;/LI-CODE&gt;


&lt;P&gt;The queries:&lt;/P&gt;
&lt;P&gt;SQL 1:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select *
  from TestData
       left outer join JoinId
                    on JoinId.Id = if TestData.Fieldname = 'Id' then TestData.TestData else null endif&lt;/LI-CODE&gt;


&lt;P&gt;SQL 2:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select *
  from TestData
       left outer join JoinId
                    on TestData.Fieldname = 'Id'
                   and JoinId.Id = if IsNumeric(TestData.TestData) = 1 then TestData.TestData else null endif&lt;/LI-CODE&gt;


&lt;P&gt;SQL 3:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select *
  from TestData
       left outer join JoinId
                    on TestData.Fieldname = 'Id'
                   and JoinId.Id = if IsNumeric(TestData.TestData) = 1 then TestData.TestData else null endif&lt;/LI-CODE&gt;


&lt;P&gt;SQL 4:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select *
  from TestData
       left outer join JoinId
                    on TestData.Fieldname = 'Id'
                   and Id = if IsIntegerValue1(TestData) = 1 then TestData else null endif&lt;/LI-CODE&gt;


&lt;P&gt;Result 1, 2, 3 and 4:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;Cursor not in a valid state
SQLCODE=-853, ODBC 3 State="24000"

Cannot convert '2015-01-01' to a numeric
SQLCODE=-157, ODBC 3 State="07006"&lt;/LI-CODE&gt;


&lt;P&gt;SQL 5:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select *
  from TestData
       left outer join JoinId
                    on TestData.Fieldname = 'Id'
                   and JoinId.Id = if IsNumeric(TestData.TestData) = 1 and IsDate(TestData.TestData) = 0 then TestData.TestData else null endif&lt;/LI-CODE&gt;


&lt;P&gt;Result 5: This doesn't crash but it doesn't give the result I am looking for (as expected because IsDate('1') returns true)&lt;/P&gt;
&lt;P&gt;My questions:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Why are SQL 1, 2, 3 and 4 crashing and not SQL 5? &lt;/LI&gt;
&lt;LI&gt;How can I get the desired result? &lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Extra note: In the actual case I'm using this for I could really use the available PK/FK indexes for performance reasons and I would rather not create additional indexes like "cast(JoinId.Id as varchar)" as suggested in Volkers answer.&lt;/P&gt;
&lt;P&gt;Tested with ASA 16.0.0.2283&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 03:36:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaq-p/13829157</guid>
      <dc:creator>Chris26</dc:creator>
      <dc:date>2016-07-11T03:36:01Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829158#M4860001</link>
      <description>&lt;P&gt;What version do you use?&lt;/P&gt;
&lt;P&gt;With 12.0.1.4403, all 5 queries do succeed and do return the desired result (although queries 2 and 3 look similar to me...).&lt;/P&gt;
&lt;P&gt;That being said, in general when the query engine seems to try to do undesired casts and fails, I'd usually try to adapt the "comparison goal" accordingly, such as&lt;/P&gt;
&lt;PRE&gt;select *
   from TestData
      left outer join JoinId
         on TestData.Fieldname = 'Id'
         and cast(JoinId.Id as varchar) = TestData.TestData
&lt;/PRE&gt;

&lt;P&gt;That would even work if you would attempt to join over a different row that has no int value, such as TestData.Fieldname = 'Date'.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 03:47:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829158#M4860001</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2016-07-11T03:47:07Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829160#M4860003</link>
      <description>&lt;P&gt;Tested with ASA 16.0.0.2283&lt;/P&gt;
&lt;P&gt;I should have added that in the actual case I'm using this for I could really use indexes for performance reasons and I would rather not create an index on "cast(JoinId.Id as varchar)".&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 04:15:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829160#M4860003</guid>
      <dc:creator>Chris26</dc:creator>
      <dc:date>2016-07-11T04:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829161#M4860004</link>
      <description>&lt;P&gt;Ah, I see, with 16.0.0.2270 I do the errors for queries 1-4, too.&lt;/P&gt;
&lt;P&gt;However, for me query 5 the result seems fitting.&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;Here's another attempt by &lt;STRONG&gt;building a derived query just with the "comparable rows" and then join&lt;/STRONG&gt; that with JoinId. That may or may not help in your real case, that's difficult to tell without knowing that case...&lt;/P&gt;
&lt;PRE&gt;select TestData.*, JoinId.*
   from TestData
      left join
         (select * from TestData where Fieldname = 'Id') TestDataWithId
          on TestData.FieldName = TestDataWithId.FieldName
      left outer join JoinId
         on TestDataWithId.TestData = JoinId.Id
&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Jul 2016 04:55:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829161#M4860004</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2016-07-11T04:55:18Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829162#M4860005</link>
      <description>&lt;P&gt;In the result for query 5 I get the "Id" column contains only "NULL" values. Where I need a value "1" in that column for the record with "FieldName" = "Id".&lt;/P&gt;
&lt;P&gt;Your attempt looks promising. I would need to see if that leads to acceptable results (if it's not too slow)&lt;/P&gt;
&lt;P&gt;However I hope that someone from SAP will react on this, and explain what is happening here and why this seems to work with version 12 and not with 16.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 05:47:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829162#M4860005</guid>
      <dc:creator>Chris26</dc:creator>
      <dc:date>2016-07-11T05:47:33Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829163#M4860006</link>
      <description>&lt;P&gt;FWIW, with 16.0.0.2270, query 5 returns the desired result set for me:&lt;/P&gt;
&lt;P&gt; &lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Re Auto convert and join"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/189495i3D48A0DCB8B35A80/image-size/large?v=v2&amp;amp;px=999" role="button" title="Re Auto convert and join" alt="Re Auto convert and join" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 07:59:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829163#M4860006</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2016-07-11T07:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829164#M4860007</link>
      <description>&lt;P&gt;Thats strange... In that first row I'm getting Id = NULL&lt;/P&gt;
&lt;P&gt;If I try&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select IsNumeric('1') as IsNumeric, IsDate('1') as IsDate&lt;/LI-CODE&gt;


&lt;P&gt;The result is&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;IsNumeric | IsDate
------------------
1         | 1&lt;/LI-CODE&gt;


&lt;P&gt;Then I'm guessing IsDate('1') is returning 0 for you?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 06:35:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829164#M4860007</guid>
      <dc:creator>Chris26</dc:creator>
      <dc:date>2016-07-11T06:35:30Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829165#M4860008</link>
      <description>&lt;P&gt;Yes, I'm getting &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;1,0&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;here. Note, I'm running with German locale but with default date_format = 'YYYY-MM-DD' and default date_order = 'YMD'.&lt;/P&gt;
&lt;P&gt;According, the following cast fails with SQLCODE -157:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select cast('1' as date)&lt;/LI-CODE&gt;


&lt;P&gt;So that difference seems to explain the different query results...&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 06:52:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829165#M4860008</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2016-07-11T06:52:52Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829166#M4860009</link>
      <description>&lt;P&gt;I'm indeed running with different settings: date_format = 'DD-MM-YYYY' and date_order = 'DMY'.
The result for cast('1' as date) then is '01-07-2016'.&lt;/P&gt;
&lt;P&gt;If I change them back to the default, I get the same results as you.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 07:42:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829166#M4860009</guid>
      <dc:creator>Chris26</dc:creator>
      <dc:date>2016-07-11T07:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829167#M4860010</link>
      <description>&lt;P&gt;This also means that if i choose 10 as the value for TestData in the first row we would have seen the same behaviour (1-1-1000 vs 10-07-2016)&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 07:49:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829167#M4860010</guid>
      <dc:creator>Chris26</dc:creator>
      <dc:date>2016-07-11T07:49:44Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829159#M4860002</link>
      <description>&lt;P&gt;While a little late to the thread . . . A simpler rewrite might be this one:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;select *  from TestData  left outer join JoinId
        on    CAST(JoinId.Id as varchar(10))
     = if TestData.Fieldname = 'Id' then TestData.TestData else null endif;&lt;/LI-CODE&gt;


&lt;P&gt;The CASTing (or alternatively a Convert()) of JoinId.Id to a character string
avoids the implicit/automatic conversion that is exposing this issue.   &lt;/P&gt;
&lt;P&gt;I'm assuming 12.0.x did something similar when evaluating the original predicate.&lt;/P&gt;
&lt;P&gt;Both types of automatic conversion are feasible here, ie:&lt;/P&gt;
&lt;LI-CODE lang="sql"&gt;  - automatically converting a numeric term to character in a character expression
  - automatically converting a character term to a numeric in a numeric expression&lt;/LI-CODE&gt;


&lt;P&gt;and since this predicate 'could operate' either way (as a numeric comparison or
a character comparison) there seems to be no preferred bias to either.&lt;/P&gt;
&lt;P&gt;I'm not quite certain if this is exactly a bug or not. Treating all such comparisons
as numeric comparisons whenever possible/feasible can be significantly more efficient 
than assuming every such expression to be a character string comparison.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2016 17:53:25 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829159#M4860002</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2016-07-11T17:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Auto convert and join</title>
      <link>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829168#M4860011</link>
      <description>&lt;P&gt;Yes, the explicit cast in the JOIN condition is probably the simplest rewrite, see the suggestion in my answer...&lt;/P&gt;
&lt;P&gt;However, as Christian has stated, the cast may prevent the usage of an index on the according column JoinId.Id here, right?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jul 2016 02:36:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/auto-convert-and-join/qaa-p/13829168#M4860011</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2016-07-12T02:36:41Z</dc:date>
    </item>
  </channel>
</rss>

