DB2 convert digits to binary format


 
Thread Tools Search this Thread
Top Forums Programming DB2 convert digits to binary format
# 1  
Old 05-03-2018
DB2 convert digits to binary format

Dear Team

We use DB2 v10.5 and using DBArtisan tool

Can someone please guide how to convert digits to binary numbers using db2 feature.
HTML Code:
Ex> for number 9 , binary should be 1001 ( 8+1)
Any help appreciated. Thanks
# 2  
Old 05-05-2018
The following was tested on DB2 Express-C Version 11.1 on a Centos 7 Virtual Machine.

Code:
[db2inst1@r2d2-centos7 data]$ 
[db2inst1@r2d2-centos7 data]$ cat -n dec2bin.sql
     1    --
     2    WITH inp(num) as (
     3        -- Supply the input decimal number here
     4        SELECT 9
     5          FROM SYSIBM.SYSDUMMY1
     6    ),
     7    t(num, bit, iter, bin) AS (
     8        -- This is a recursive SQL that keeps dividing inp.num by 2 until it is
     9        -- reduced to 0. The bit column is the remainder (either 0 or 1) per
    10        -- iteration. Each bit column is prepended successively and set in the bin
    11        -- column. Eventually, when num is 0, the recursion ends and we have the
    12        -- binary representation of inp.num in the bin column.
    13        SELECT num, NULL, 0, CAST('' AS VARCHAR(40))
    14          FROM inp
    15        UNION ALL
    16        SELECT num/2, num%2, iter+1, STRIP(num%2 || bin)
    17          FROM t
    18         WHERE iter < 1000
    19           AND num > 0
    20    )
    21    SELECT inp.num AS dec,
    22           t.bin AS bin
    23      FROM inp, t
    24     WHERE t.num = 0
    25    ;
    26    
    27    
[db2inst1@r2d2-centos7 data]$ 
[db2inst1@r2d2-centos7 data]$ db2 -tmf dec2bin.sql

DEC         BIN                                     
----------- ----------------------------------------
          9 1001                                    

  1 record(s) selected.


[db2inst1@r2d2-centos7 data]$ 
[db2inst1@r2d2-centos7 data]$

And another test with a larger input number:
Code:
[db2inst1@r2d2-centos7 data]$ 
[db2inst1@r2d2-centos7 data]$ cat -n dec2bin.sql 
     1    --
     2    WITH inp(num) as (
     3        -- Supply the input decimal number here
     4        SELECT 786955301775
     5          FROM SYSIBM.SYSDUMMY1
     6    ),
     7    t(num, bit, iter, bin) AS (
     8        -- This is a recursive SQL that keeps dividing inp.num by 2 until it is
     9        -- reduced to 0. The bit column is the remainder (either 0 or 1) per
    10        -- iteration. Each bit column is prepended successively and set in the bin
    11        -- column. Eventually, when num is 0, the recursion ends and we have the
    12        -- binary representation of inp.num in the bin column.
    13        SELECT num, NULL, 0, CAST('' AS VARCHAR(40))
    14          FROM inp
    15        UNION ALL
    16        SELECT num/2, num%2, iter+1, STRIP(num%2 || bin)
    17          FROM t
    18         WHERE iter < 1000
    19           AND num > 0
    20    )
    21    SELECT inp.num AS dec,
    22           t.bin AS bin
    23      FROM inp, t
    24     WHERE t.num = 0
    25    ;
    26    
    27    
[db2inst1@r2d2-centos7 data]$ 
[db2inst1@r2d2-centos7 data]$ db2 -tmf dec2bin.sql

DEC                  BIN                                     
-------------------- ----------------------------------------
        786955301775 1011011100111010001100001111001110001111

  1 record(s) selected.


[db2inst1@r2d2-centos7 data]$ 
[db2inst1@r2d2-centos7 data]$

These 2 Users Gave Thanks to durden_tyler For This Post:
# 3  
Old 05-28-2018
Hi durden_tyler
Tanks a lot . Works fine

Last edited by Don Cragun; 05-28-2018 at 09:19 AM.. Reason: Remove duplicated text.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

DB2 Query -Convert multi values from column to rows

Hi Team I am using DB2 artisan tool and struck to handle multi values present in columns that are comma(,) separated. I want to convert those column values in separate rows . For example : Column 1 Column2 Jan,Feb Hold,Sell,Buy Expected Result Column1 ... (3 Replies)
Discussion started by: Perlbaby
3 Replies

2. Shell Programming and Scripting

Convert jpg file to binary format

Hi Team, Here's the requirement. I have a image file in jpg format in unix. Now I need to i. convert the jpg format to binary format ii. followed by loading the binary file to Oracle db. Can anyone help me out? Thanks Krishnakanth Manivannan (4 Replies)
Discussion started by: kmanivan82
4 Replies

3. Shell Programming and Scripting

Perl Script remove digits and convert

Hello Guy's Quick question which im sure many can answer in seconds. Basically I have a perl script which is running commands to an element and then taking some of the the output and printing it to the screen. One of the outputs is a variable Hex Number. What I would like to do is strip... (1 Reply)
Discussion started by: mutley2202
1 Replies

4. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

5. Shell Programming and Scripting

summing the digits of a binary nuMBER

please help me write a perl program to find the difference of 1 and zeros of a 6 digit binary number. eg If input is 111100 expected output +2 if input is 000011 expected output -2 input is 000111 expected output 0 (2 Replies)
Discussion started by: dll_fpga
2 Replies

6. Shell Programming and Scripting

Need to represent a number in 99999999 format(8 digits)

Hi all, i have to create a file having an 8-digit sequence number, that will start by name file_00000001.cvs at first time, the next day the file will be named file_00000002.cvs and so on. How can i do this in my script please, specially that i will need a counter that increments this number... (10 Replies)
Discussion started by: Eman_in_forum
10 Replies

7. Shell Programming and Scripting

Get the places of binary digits in the korn shell script

TO THE ALMIGHTY FORUM , though i have already posted the same question on hex to binary thread , i am posting here also for other beginners who may benefit from this thread... I have a 32 bit binary containing a series of 1' and 0's , and i am stuck... (2 Replies)
Discussion started by: venu
2 Replies

8. Shell Programming and Scripting

Is there any script which convert binary file to CSV format

Dear guys; I have a binary file and I need to convert its data to csv format ...appreciating your help. Best Regards (14 Replies)
Discussion started by: ahmad.diab
14 Replies

9. Shell Programming and Scripting

How to convert a column to last five digits

Hi Suppose I have a input.csv file like this Fort, 2034440-3333 Honda, b293489289 Toyota, 23423000 How to convert the second column to the following format, there are only two cases: a.if the last five character is a hyphen plus four numeric digit, keep the the last five digits... (6 Replies)
Discussion started by: grossgermany
6 Replies

10. UNIX for Dummies Questions & Answers

To convert multi format file to a readable ascii format

Hi I have a file which has ascii , binary, binary decimal coded,decimal & hexadecimal data with lot of special characters (like öƒ.ƒ.„İİ¡Š·œƒ.„İİ¡Š· ) in it. I want to standardize the file into ASCII format & later use that as source . Can any one suggest a way a logic to convert such... (5 Replies)
Discussion started by: gaur.deepti
5 Replies
Login or Register to Ask a Question