{"id":516,"date":"2017-03-22T11:35:38","date_gmt":"2017-03-22T16:35:38","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=516"},"modified":"2017-03-22T11:45:25","modified_gmt":"2017-03-22T16:45:25","slug":"load-data-infile-into-a-bit-field","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=516","title":{"rendered":"LOAD DATA INFILE into a BIT field"},"content":{"rendered":"<p><a href=\"https:\/\/dev.mysql.com\/doc\/en\/load-data.html\">https:\/\/dev.mysql.com\/doc\/en\/load-data.html<\/a><\/p>\n<blockquote><p>BIT values cannot be loaded using binary notation (for example, b&#8217;011010&#8242;).<\/p><\/blockquote>\n<pre>$ cat test.txt\r\n\r\nb'101010'\r\n0b111000\r\n\r\n-----\r\n\r\nCREATE TABLE loadTest (b BIT(6));\r\n\r\nLOAD DATA LOCAL INFILE 'test.txt' INTO TABLE loadTest;\r\n\r\n\/*--------+------+---------------------------------------+\r\n| Level   | Code | Message                               |\r\n+---------+------+---------------------------------------+\r\n| Warning | 1406 | Data too long for column 'b' at row 1 |\r\n| Warning | 1406 | Data too long for column 'b' at row 2 |\r\n+---------+------+--------------------------------------*\/\r\n\r\n-- Note the wrong values:\r\n\r\nSELECT BIN(b) FROM loadTest;\r\n\/*-------+\r\n| BIN(b) |\r\n+--------+\r\n| 111111 |\r\n| 111111 |\r\n+-------*\/\r\n\r\nTRUNCATE loadTest;\r\n\r\nLOAD DATA LOCAL INFILE 'test.txt' INTO TABLE loadTest (@b) \r\n  SET b = CAST(CONV(TRIM('''' FROM SUBSTRING_INDEX(@b, 'b', -1)), 2, 10) AS UNSIGNED);\r\n\r\nSELECT BIN(b) FROM loadTest;\r\n\/*-------+\r\n| BIN(b) |\r\n+--------+\r\n| 101010 |\r\n| 111000 |\r\n+-------*\/\r\n<\/pre>\n<p>Deconstruction:<\/p>\n<pre>SUBSTRING_INDEX(@b, 'b', -1)<\/pre>\n<p> removes the leading &#8216;b&#8217; or &#8216;0b&#8217;.<\/p>\n<pre>TRIM('''' FROM ...)<\/pre>\n<p> removes the quotes if there are any.<\/p>\n<pre>CONV(..., 2, 10)<\/pre>\n<p> converts the string of 0s and 1s into a string of decimal digits.<\/p>\n<pre>CAST(... AS UNSIGNED)<\/pre>\n<p> turns the string of decimal digits into an integer.<br \/>\nMySQL automatically casts integers into bits when inserting into a BIT type.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/dev.mysql.com\/doc\/en\/load-data.html BIT values cannot be loaded using binary notation (for example, b&#8217;011010&#8242;). $ cat test.txt b&#8217;101010&#8242; 0b111000 &#8212;&#8211; CREATE TABLE loadTest (b BIT(6)); LOAD DATA LOCAL INFILE &#8216;test.txt&#8217; INTO TABLE loadTest; \/*&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+ | Level | Code | Message | +&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+ | Warning | 1406 | Data too long for column &#8216;b&#8217; at row 1 | [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,11],"tags":[],"class_list":["post-516","post","type-post","status-publish","format-standard","hentry","category-mysql","category-one-liners"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-8k","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=516"}],"version-history":[{"count":7,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/516\/revisions"}],"predecessor-version":[{"id":675,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/516\/revisions\/675"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}