{"id":1942,"date":"2019-01-02T11:56:34","date_gmt":"2019-01-02T16:56:34","guid":{"rendered":"http:\/\/www.thefillmoregroup.com\/blog\/?p=1942"},"modified":"2019-01-12T14:08:29","modified_gmt":"2019-01-12T19:08:29","slug":"sql-to-aid-db2-for-z-os-ebcdic-to-db2-for-luw-unicode-conversion-ibmanalytics-ibmz","status":"publish","type":"post","link":"https:\/\/www.thefillmoregroup.com\/blog\/?p=1942","title":{"rendered":"SQL to Aid #Db2 for z\/OS EBCDIC to Db2 for LUW Unicode Conversion #IBMAnalytics #IBMz"},"content":{"rendered":"\r\n<p class=\"wp-block-paragraph\">Dr. Sheldon Cooper of &#8220;The Big Bang Theory&#8221; regularly records a YouTube video called &#8220;Fun with Flags&#8221; so I&#8217;m going to call this blog post &#8220;Fun with SQL&#8221;.\u00a0 Sheldon may be a future Nobel Prize winner, but I&#8217;ve been named an IBM Champion for Analytics and System z for 2019. \ud83d\ude42<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The problem statement: a large US government agency is contemplating migrating a packaged application from System z to Linux.\u00a0 The current and future database repositories\u00a0are Db2 for z\/OS and Db2 for LUW respectively.\u00a0 As a System z Champion I&#8217;ve presented on the continued relevancy and efficacy of the IBM mainframe, but that&#8217;s not the focus of this post.\u00a0 One of the challenges of porting data is the conversion of character encoding from <a href=\"https:\/\/en.wikipedia.org\/wiki\/EBCDIC#History\" target=\"_blank\" rel=\"noopener\">EBCDIC<\/a> (Extended Binary Coded Decimal Interchange Code) which is the legacy encoding protocol used on IBM mainframes and System i to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unicode\" target=\"_blank\" rel=\"noopener\">Unicode<\/a>.\u00a0 Db2 for z\/OS supports Unicode, but I&#8217;m not going to address anything other than EBCDIC encoding at the source\u00a0and Unicode at the target (Sorry, ASCII). \u00a0In brief, EBCDIC represents an 8 bit binary encoding scheme that can represent 256 different characters.\u00a0 For example: the letter &#8220;F&#8221; (one of my faves) is represented as <strong>1100 0110<\/strong> in binary and <strong>C6<\/strong> in hexadecimal. For national language support amid the variety of Single Byte Character Sets (SBCS) used around the world &#8211; think the German umlaut (i.e. <strong>\u00e4 \u00f6 \u00fc<\/strong>) &#8211; the 256 byte universe of EBCDIC wasn&#8217;t nearly enough.\u00a0 The Unicode solution is to use more than 1 byte to represent some characters.\u00a0 For example: the &#8220;<strong>\u00a2<\/strong>&#8221; cent sign takes one byte to represent in EBCDIC, but 2 bytes in Unicode.<\/p>\r\n\r\n\r\n\r\n<pre><strong>select length(ebcdic_str('\u00a2')) as EBCDIC, length(unicode_str('\u00a2')) as Unicode from sysibm.sysdummy1;<\/strong><\/pre>\r\n\r\n\r\n\r\n<pre><strong>EBCDIC\u00a0\u00a0\u00a0 UNICODE<\/strong><br \/><br \/><strong>\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2<\/strong><\/pre>\r\n\r\n\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This problem has manifested itself as\u00a0an error when moving a CHAR(5) column of data from Db2 for z\/OS to Db2 for LUW.\u00a0 If, say,\u00a0the pesky &#8220;\u00a2&#8221; is present in the string and all 5 characters are significant (i.e. no trailing blanks), the string won&#8217;t fit in the same column width on the target platform.\u00a0 Most customers find this out the hard way when the Db2 for LUW LOAD command or IMPORT utility posts errors.\u00a0 There are two possible solutions:<\/p>\r\n<ul>\r\n<li>data cleanup (Do we really need the pesky &#8216;\u00a2&#8217; in a Comments column?)<\/li>\r\n<li>lengthen the target columns to accommodate Unicode expansion<\/li>\r\n<\/ul>\r\n<p>Both can be time-consuming and expensive and will depend on a variety of factors beyond the scope of this blog post.\u00a0 The purpose here today is to identify the extent of the problem <em>before<\/em> you actually try to port the data.\u00a0 I&#8217;ve constructed this SQL statement to do just that.<!--more--><\/p>\r\n<pre><strong>select 'SELECT', rtrim(name)||', HEX('||rtrim(name)||') AS HEX, LENGTH(EBCDIC_STR('||rtrim(name)||')) AS L_EBCDIC, LENGTH(UNICODE_STR('||rtrim(name)||')) AS L_UNICODE FROM', rtrim(tbcreator)||'.'||rtrim(tbname), 'WHERE LENGTH(EBCDIC_STR('||rtrim(name)||')) &lt; LENGTH(UNICODE_STR('||rtrim(name)||')) AND LENGTH(UNICODE_STR('||rtrim(name)||')) &gt;', '( SELECT LENGTH FROM SYSIBM.SYSCOLUMNS WHERE TBCREATOR = '''||rtrim(tbcreator)||''' AND TBNAME = '''||rtrim(tbname)||''' AND NAME = '''||rtrim(name)||''' );' from sysibm.syscolumns where tbname like 'ICM%' and encoding_scheme &lt;&gt; 'E' and coltype in ('CHAR', 'VARCHAR');<\/strong><\/pre>\r\n\r\n\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">So what are we doing here?\u00a0 The SQL statement <em>above<\/em> scans the Db2 for z\/OS catalog and generates\u00a0SQL statements like the one <em>below<\/em> that compares the length of the data in a column represented in EBCDIC with the length of that same data represented in Unicode.\u00a0 It also compares the length of the Unicode representation of the data with the\u00a0maximum column length.<\/p>\r\n\r\n\r\n\r\n<pre><strong>SELECT DEFAULTVALUE, HEX(DEFAULTVALUE) AS HEX, LENGTH(EBCDIC_STR(DEFAULTVALUE)) AS L_EBCDIC, LENGTH(UNICODE_STR(DEFAULTVALUE)) AS L_UNICODE FROM XXXX.ICMSTCOMPATTRS WHERE LENGTH(EBCDIC_STR(DEFAULTVALUE)) &lt; LENGTH(UNICODE_STR(DEFAULTVALUE)) AND LENGTH(UNICODE_STR(DEFAULTVALUE)) &gt; ( SELECT LENGTH FROM SYSIBM.SYSCOLUMNS WHERE TBCREATOR = 'XXXX' AND TBNAME = 'ICMSTCOMPATTRS' AND NAME = 'DEFAULTVALUE' );<\/strong><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The columns that are evaluated are chosen by you in the last line of the first SQL statement that generates the list of candidates.\u00a0 This is where knowledge of the data is <em>crucial<\/em>.\u00a0 If, for example, you have a VARCHAR column of a maximum 500 bytes and there&#8217;s typically no more than 50 bytes of data, you won&#8217;t exceed the\u00a0maximum length of the column even with a slew of special characters and rampant expansion.\u00a0 As such you might only be interested in smaller, fixed length columns.\u00a0 I only chose columns with tables that had a certain name pattern (<strong>ICM%<\/strong>) and column types of CHAR and VARCHAR.\u00a0 My IBM Gold Consultant confr\u00e8res will point out that the only test that matters is wheter the maximum column length will be exceeded.\u00a0 The first predicate just determines if any expansion will occur.\u00a0 But remember: I&#8217;m channeling Dr. Sheldon Cooper and err on the side of completeness.\u00a0 Be sure to test this first in a non-Prod environment before turning it loose on Production.\u00a0 Also run it during times of light subsystem utilization.\u00a0 The first, master query runs quickly against the SYSIBM.SYSCOLUMNS table.\u00a0 The resulting scans will produce a lot of I\/O.<\/p>\r\n<p>Let me know if this has been helpful.\u00a0 <strong>And to all a happy, healthy, and prosperous 2019!<\/strong><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Dr. Sheldon Cooper of &#8220;The Big Bang Theory&#8221; regularly records a YouTube video called &#8220;Fun with Flags&#8221; so I&#8217;m going to call this blog post &#8220;Fun with SQL&#8221;.\u00a0 Sheldon may be a future Nobel Prize winner, but I&#8217;ve been named an IBM Champion for Analytics and System z for 2019. \ud83d\ude42 The problem statement: a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99,29,8,20,12,148],"tags":[214,236,272,273],"class_list":["post-1942","post","type-post","status-publish","format-standard","hentry","category-db2-for-i","category-db2-for-linux-unix-windows","category-wwwthefillmoregroupcomvsevmphp","category-db2-for-zos","category-wwwthefillmoregroupcomabouttfgphp","category-ibm-champion","tag-db2-for-luw","tag-db2-for-zos","tag-ebcdic","tag-unicode"],"_links":{"self":[{"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1942"}],"version-history":[{"count":13,"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1942\/revisions"}],"predecessor-version":[{"id":1976,"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1942\/revisions\/1976"}],"wp:attachment":[{"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thefillmoregroup.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}