{"id":36,"date":"2016-08-18T16:04:00","date_gmt":"2016-08-18T16:04:00","guid":{"rendered":"\/colin\/morethanfour\/post\/More-pitfalls-with-NVARCHAR2-vs-VARCHAR2"},"modified":"2016-08-18T16:04:00","modified_gmt":"2016-08-18T16:04:00","slug":"more-pitfalls-with-nvarchar2-vs-varchar2","status":"publish","type":"post","link":"https:\/\/spacefold.com\/colin\/morethanfour\/2016\/08\/18\/more-pitfalls-with-nvarchar2-vs-varchar2\/","title":{"rendered":"More pitfalls with NVARCHAR2 vs VARCHAR2"},"content":{"rendered":"<p>Consider the following POSTAL_CODE column:<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"border-top-style: none; font-size: 8pt; overflow: visible; border-left-style: none; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4\"><span style=\"color: #0000ff\">create<\/span> <span style=\"color: #0000ff\">table<\/span> Test1 (<br>   POSTAL_CODE NVARCHAR2(10)<br>);<br> <br>insert <span style=\"color: #0000ff\">into<\/span> Test1 <span style=\"color: #0000ff\">values<\/span> (<span style=\"color: #006080\">'12345-1234'<\/span>);<br><span style=\"color: #0000ff\">commit<\/span>;<br> <br><span style=\"color: #0000ff\">select<\/span> * <span style=\"color: #0000ff\">from<\/span> Test1;<br> <br>POSTAL_CODE<br>--------------------<br>12345-123<\/pre>\n<p><\/div>\n<p>That&#8217;s right &#8211; the NVARCHAR2(10) column appears to lop of 10th character and doesn&#8217;t store it.<\/p>\n<p>This behavior must be related to multi-byte character, and Unicode, somehow. The <a href=\"https:\/\/docs.oracle.com\/cd\/E11882_01\/server.112\/e10729\/toc.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Oracle DB docs on Globalization Support<\/a> can get quite detailed, but I still haven&#8217;t found a concise explanation of this silent truncation behavior.<\/p>\n<h3>So, just make the column larger, right?<\/h3>\n<p>We tried that, altering the column from 10 to 15 characters. Our column can now correctly store the full 10 characters of a postal code string.<\/p>\n<p>But then, another problem showed up when we needed to match on the first 5 characters in the ZIP code.<\/p>\n<p>Consider:<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"border-top-style: none; font-size: 8pt; overflow: visible; border-left-style: none; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4\"><span style=\"color: #0000ff\">create<\/span> <span style=\"color: #0000ff\">table<\/span> Test2 ( V1 varchar2(15), N1 nvarchar2(15) ) ;<br>insert <span style=\"color: #0000ff\">into<\/span> Test2 <span style=\"color: #0000ff\">values<\/span> ( <span style=\"color: #006080\">'1234567'<\/span>, <span style=\"color: #006080\">'1234567'<\/span> );<br>insert <span style=\"color: #0000ff\">into<\/span> Test2 <span style=\"color: #0000ff\">values<\/span> ( <span style=\"color: #006080\">'123456'<\/span>,  <span style=\"color: #006080\">'123456'<\/span> );<br>insert <span style=\"color: #0000ff\">into<\/span> Test2 <span style=\"color: #0000ff\">values<\/span> ( <span style=\"color: #006080\">'12345'<\/span>,   <span style=\"color: #006080\">'12345'<\/span> );<br>insert <span style=\"color: #0000ff\">into<\/span> Test2 <span style=\"color: #0000ff\">values<\/span> ( <span style=\"color: #006080\">'1234'<\/span>,    <span style=\"color: #006080\">'1234'<\/span> );<br>insert <span style=\"color: #0000ff\">into<\/span> Test2 <span style=\"color: #0000ff\">values<\/span> ( <span style=\"color: #006080\">'123'<\/span>,     <span style=\"color: #006080\">'123'<\/span> );<br><span style=\"color: #0000ff\">commit<\/span>;<br> <br><span style=\"color: #0000ff\">SQL<\/span>&gt; <span style=\"color: #0000ff\">select<\/span> V1, substr( V1, 1,5 ), N1, substr( N1, 1, 5 ) <span style=\"color: #0000ff\">from<\/span> Test2;<br> <br>V1              SUBSTR(V1,1,5) N1                             SUBSTR(N1,1,5)<br>-------------<span style=\"color: #008000\">-- -------------- ------------------------------ --------------<\/span><br>1234567          12345         1234567                        1234<br>123456           12345         123456                         1234<br>12345            12345         12345                          1234<br>1234             1234          1234                           1234<br>123              123           123                            123<br><\/pre>\n<p><\/div>\n<p>Yup, that&#8217;s right. SUBSTR( NVATCHAR2 ) does not return the full 5 characters! <\/p>\n<p>As a workaround, we can use SUBSTRC():<\/p>\n<div id=\"codeSnippetWrapper\">\n<pre id=\"codeSnippet\" style=\"border-top-style: none; font-size: 8pt; overflow: visible; border-left-style: none; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4\"><span style=\"color: #0000ff\">SQL<\/span>&gt; <span style=\"color: #0000ff\">select<\/span> V1, substrc( V1, 1,5 ), N1, substrc( N1, 1, 5 ) <span style=\"color: #0000ff\">from<\/span> Test2;<br> <br>V1              SUBSTRC(V1,1,5)  N1                            SUBSTRC(N1,1,5)<br>-------------<span style=\"color: #008000\">-- --------------- ------------------------------ ------------------------------<\/span><br>1234567         12345           1234567                        12345<br>123456          12345           123456                         12345<br>12345           12345           12345                          12345<br>1234            1234            1234                           1234<br>123             123             123                            123<\/pre>\n<p><\/div>\n<p>Now the values retrieved from the NVARCHAR2 column are working as expected.<\/p>\n<h3>Bottom Line:<\/h3>\n<ul>\n<li>Don&#8217;t use NVARCHAR2 for size-limited columns, unless you absolutely have to<\/li>\n<li>If you have to use NVARCHAR2, us SUBSTRC() for partials.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Consider the following POSTAL_CODE column: create table Test1 ( POSTAL_CODE NVARCHAR2(10)); insert into Test1 values (&#8216;12345-1234&#8242;);commit; select * from Test1; POSTAL_CODE&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;12345-123 That&#8217;s right &#8211; the NVARCHAR2(10) column appears to lop of 10th character and doesn&#8217;t store it. This behavior must be related to multi-byte character, and Unicode, somehow. The Oracle DB docs on Globalization Support [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-oracle","post-preview"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}