{"id":275,"date":"2016-11-15T18:10:05","date_gmt":"2016-11-15T17:10:05","guid":{"rendered":"http:\/\/www.riedmann.it\/blog\/?p=275"},"modified":"2017-06-09T05:17:05","modified_gmt":"2017-06-09T04:17:05","slug":"firebird-specials-in-table-structure-for-varchar-and-utf-8","status":"publish","type":"post","link":"https:\/\/blog.riedmann.it\/?p=275","title":{"rendered":"Firebird specials in table structure for varchar and UTF-8"},"content":{"rendered":"<p>In my Visual Objects applications based on DBFs I have a routine that checks the structure of the DBF tables, compares them with the structure that is required and adjusts the structure.<\/p>\n<p>I&#8217;m now working on a similar functionality for my Firebird based .NET applications. To retrieve the list of the currently existing user tables, I&#8217;m using such a statement:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nselect rdb$relation_name as tablename from rdb$relations\r\nwhere rdb$view_blr is null and \r\n(rdb$system_flag is null or rdb$system_flag = 0)\r\n<\/pre>\n<p>and to retrieve the actual structure of such a table I use the following statement:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nselect rf.rdb$field_position as fieldpos, \r\nTRIM(rf.rdb$field_name) as fieldname, \r\nf.rdb$field_type as fieldtype, \r\nf.rdb$field_length as fieldlen,\r\nf.rdb$field_scale as fielddec, \r\n(f.rdb$null_flag = 1) OR (rf.rdb$null_flag = 1) as canbenull \r\nfrom rdb$relation_fields rf \r\nJOIN rdb$fields f ON f.rdb$field_name = rf.rdb$field_source \r\nWHERE upper( rf.rdb$relation_name ) = upper( 'mytable' ) \r\norder by rf.rdb$field_position\r\n<\/pre>\n<p>To have no limitations with foreign languages, I have opted to use the UTF-8 character set for all varchar columns.<\/p>\n<p>And now I was very surprised: <strong>the select statement returned 4 times the actual field width<\/strong>: for a field defined as varchar(20) the query returned 80 as field length.<\/p>\n<p>My conclusion is very simple: since every UTF-8 character can take up to 4 bytes, Firebird reserves 4 bytes for every character.<\/p>\n<p>I have now found the following StackOwerflow article about this:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/822837\/firebird-utf8-varchar-size\" target=\"_blank\" rel=\"noopener\">http:\/\/stackoverflow.com\/questions\/822837\/firebird-utf8-varchar-size<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my Visual Objects applications based on DBFs I have a routine that checks the structure of the DBF tables, compares them with the structure that is required and adjusts the structure. I&#8217;m now working on a similar functionality for my Firebird based .NET applications. To retrieve the list of the currently existing user tables, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,23],"tags":[],"class_list":["post-275","post","type-post","status-publish","format-standard","hentry","category-firebird-database","category-x-programming"],"_links":{"self":[{"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/posts\/275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=275"}],"version-history":[{"count":9,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/posts\/275\/revisions"}],"predecessor-version":[{"id":289,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=\/wp\/v2\/posts\/275\/revisions\/289"}],"wp:attachment":[{"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.riedmann.it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}