{"id":2073,"date":"2018-07-03T22:39:36","date_gmt":"2018-07-03T22:39:36","guid":{"rendered":"http:\/\/nenadnoveljic.com\/blog\/?p=2073"},"modified":"2018-07-03T22:39:36","modified_gmt":"2018-07-03T22:39:36","slug":"sql-converting-decimal-integers-to-any-base","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/","title":{"rendered":"SQL for Converting Decimal Integers to Any Base"},"content":{"rendered":"<p>The following SQL statement converts an unsigned decimal integer <I>d<\/I> to a base <i>b<\/i>.<\/p>\n<pre><code>select &&d base10,\r\n    listagg(\r\n        substr(\r\n          case &&b when 32 then \r\n            '0123456789abcdfghjkmnpqrstuvwxyz'\r\n          else\r\n            '0123456789abcdefghjkmnpqrstuvwxyz'\r\n          end,\r\n          mod(\r\n            trunc(&&d\/power(&&b,level-1)),\r\n            &&b\r\n          ) + 1 ,\r\n          1\r\n        ) \r\n    ) within group (order by level desc) base&&b \r\n  from dual \r\n  connect by level <= ceil(log(&#038;&#038;b,&#038;&#038;d+1))\r\n;<\/code><\/pre>\n<p>You can specify any base to convert to, for example:<\/p>\n<pre><code>define d = 258\r\ncolumn base10 format 999999\r\n\r\ndefine b = 2\r\ncolumn base&&b format a9\r\n\r\nselect &&d base10,\r\n    listagg(\r\n        substr(\r\n          case &&b when 32 then \r\n            '0123456789abcdfghjkmnpqrstuvwxyz'\r\n          else\r\n            '0123456789abcdefghjkmnpqrstuvwxyz'\r\n          end,\r\n          mod(\r\n            trunc(&&d\/power(&&b,level-1)),\r\n            &&b\r\n          ) + 1 ,\r\n          1\r\n        ) \r\n    ) within group (order by level desc) base&&b \r\n  from dual \r\n  connect by level <= ceil(log(&#038;&#038;b,&#038;&#038;d+1))\r\n;\r\n\r\n BASE10 BASE2\r\n------- ---------\r\n    258 100000010\r\n\t\r\ndefine b = 8\r\ncolumn base&#038;&#038;b format a9\r\n\/\r\n\r\n BASE10 BASE8\r\n------- ---------\r\n    258 402\r\n\r\ndefine b = 16\r\ncolumn base&#038;&#038;b format a9\r\n\/\r\n\r\n BASE10 BASE16\r\n------- ---------\r\n    258 102\r\n\r\ndefine b = 32\r\ncolumn base&#038;&#038;b format a9\r\n\/\r\n\r\n BASE10 BASE32\r\n------- ---------\r\n    258 82<\/code><\/pre>\n<p>The conversion is performed by doing <a href=\"https:\/\/en.wikipedia.org\/wiki\/Euclidean_division\">Euclidean division<\/a> of the integer in the decimal notation with the new base. This process is iteratively applied to the quotient until it becomes zero. Finally, all of the remainders are concatenated in the reversed order with the single-set aggregate function <a href=\"https:\/\/docs.oracle.com\/en\/database\/oracle\/oracle-database\/12.2\/sqlrf\/LISTAGG.html#GUID-B6E50D8E-F467-425B-9436-F7F8BF38D466\">listagg<\/a>.<\/p>\n<p>As SQL doesn't explicitly support iterations, I used a hierarchical query for generating quotients and remainders instead. The total number of divisions n can be calculated as follows:<\/p>\n<p>n = ceil(log(b,d+1))<\/p>\n<p>A separate encoding table '0123456789abcdfghjkmnpqrstuvwxyz' is defined for the base32, because the table doesn't contain \"e\" in the Oracle database.<\/p>\n<p>References:<\/p>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Positional_notation#Base_conversion\">Positional Notation<\/a>, Wikipedia<\/li>\n<li><a href=\"https:\/\/blog.tanelpoder.com\/2009\/02\/22\/sql_id-is-just-a-fancy-representation-of-hash-value\/\">SQL_ID is just a fancy representation of hash value<\/a>, Tanel Poder<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A single SQL converts unsigned decimal integers to any other base. <a href=\"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2073","post","type-post","status-publish","format-standard","hentry","category-oracle"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL for Converting Decimal Integers to Any Base - All-round Database Topics<\/title>\n<meta name=\"description\" content=\"A single SQL converts unsigned decimal integers to any other base.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL for Converting Decimal Integers to Any Base - All-round Database Topics\" \/>\n<meta property=\"og:description\" content=\"A single SQL converts unsigned decimal integers to any other base.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-03T22:39:36+00:00\" \/>\n<meta name=\"author\" content=\"Nenad Noveljic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@NenadNoveljic\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nenad Noveljic\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-converting-decimal-integers-to-any-base\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-converting-decimal-integers-to-any-base\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"SQL for Converting Decimal Integers to Any Base\",\"datePublished\":\"2018-07-03T22:39:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-converting-decimal-integers-to-any-base\\\/\"},\"wordCount\":151,\"commentCount\":2,\"articleSection\":[\"Oracle\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-converting-decimal-integers-to-any-base\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-converting-decimal-integers-to-any-base\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-converting-decimal-integers-to-any-base\\\/\",\"name\":\"SQL for Converting Decimal Integers to Any Base - All-round Database Topics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2018-07-03T22:39:36+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"description\":\"A single SQL converts unsigned decimal integers to any other base.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-converting-decimal-integers-to-any-base\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-converting-decimal-integers-to-any-base\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-converting-decimal-integers-to-any-base\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL for Converting Decimal Integers to Any Base\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\",\"name\":\"All-round Database Topics\",\"description\":\"Nenad Noveljic\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\",\"name\":\"Nenad Noveljic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"caption\":\"Nenad Noveljic\"},\"sameAs\":[\"nenad-noveljic-9b746a6\",\"https:\\\/\\\/x.com\\\/NenadNoveljic\"],\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/author\\\/nenad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL for Converting Decimal Integers to Any Base - All-round Database Topics","description":"A single SQL converts unsigned decimal integers to any other base.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/","og_locale":"en_US","og_type":"article","og_title":"SQL for Converting Decimal Integers to Any Base - All-round Database Topics","og_description":"A single SQL converts unsigned decimal integers to any other base.","og_url":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/","og_site_name":"All-round Database Topics","article_published_time":"2018-07-03T22:39:36+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"SQL for Converting Decimal Integers to Any Base","datePublished":"2018-07-03T22:39:36+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/"},"wordCount":151,"commentCount":2,"articleSection":["Oracle"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/","url":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/","name":"SQL for Converting Decimal Integers to Any Base - All-round Database Topics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2018-07-03T22:39:36+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"description":"A single SQL converts unsigned decimal integers to any other base.","breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/sql-converting-decimal-integers-to-any-base\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SQL for Converting Decimal Integers to Any Base"}]},{"@type":"WebSite","@id":"https:\/\/nenadnoveljic.com\/blog\/#website","url":"https:\/\/nenadnoveljic.com\/blog\/","name":"All-round Database Topics","description":"Nenad Noveljic","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nenadnoveljic.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa","name":"Nenad Noveljic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","caption":"Nenad Noveljic"},"sameAs":["nenad-noveljic-9b746a6","https:\/\/x.com\/NenadNoveljic"],"url":"https:\/\/nenadnoveljic.com\/blog\/author\/nenad\/"}]}},"_links":{"self":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/comments?post=2073"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2073\/revisions"}],"predecessor-version":[{"id":2111,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2073\/revisions\/2111"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=2073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=2073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=2073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}