0

I have the following data in a table like so:

create table tutor(
   id int,
   accessto varchar(8)
);

The data looks like this:

+-------+----------+
| id    | accessto |
+-------+----------+
|    69 | b'1011'  |
|   162 | b'1011'  |
|   232 | b'1011'  |
|   257 | b'0010'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1110'  |
|   258 | b'1001'  |
|   258 | b'0011'  |
|   258 | b'1001'  |
+-------+----------+

I want to convert this into binary. How is it possible?

I tried these queries but getting the same result:

select id, cast(accessto as BINARY) from tutor;
select id, convert(accessto,binary) from tutor;

How to do this?

My expected result is should be like this:

 +-------+----------+
    | id    | accessto |
    +-------+----------+
    |    69 | 11  |
    |   162 | 11  |
    |   232 | 11  |
    |   257 | 2  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 14  |
    |   258 | 9  |
    |   258 | 3  |
    |   258 | 9  |
    +-------+----------+
1
  • What's your expect result? Commented Sep 3, 2018 at 7:25

4 Answers 4

2

Since the accepted answer is unnecessarily complex, here a concise answer:

TLDR;

The result can be achieved in one step:

SELECT CONV(TRIM("'" FROM SUBSTRING("0b'1011'", 3)), 2, 16);

Explanation

Starting point is a string containing 0b1011:

SET @string = "0b'1011'"; -- (string) 0b'1011'

We can apply string operations to get it closer to a number:

SET @plainnumber = TRIM("'" FROM SUBSTRING(@string, 3)); -- (string) 1011

We can then convert the string into a real number, without sign because there is none: (You cannot use binary as a type here, because it is a string type. This step is optional because MySQL will cast implicitly.)

SET @number = CAST(@plainnumber AS UNSIGNED); -- (unsigned) 1011

You can now do whatever you want with it. The OP wanted to get the decimal representation which can be acquired with CONV:

SELECT CONV(@number, 2, 10);

Explanation for hexadecimal

If your starting point is a string containing a hexadecimal representation:

SET @string = "41"; -- 0x41

You can use the UNHEX function to get a binary string:

SELECT UNHEX(@string);

Or you can use CONV to get a numerical representation:

SELECT CONV(@string, 16, 10); -- decimal (65)
SELECT CONV(@string, 16, 2); -- binary (0100 0001)
Sign up to request clarification or add additional context in comments.

3 Comments

See also this answer if you are converting UUIDs
There should be some sort of bonus for excavating and providing a solution to such an old question..
1

You may have to do this the 'hard' way and create a bit field either in tutor or another table, internal conversion seems to work better than cast and convert

drop table if exists t;
create table t(
   id int,
   accessto varchar(8),
   abit bit(8)
);


insert into t(id,accessto) values
(    69 , b'1011') , 
(   162 , b'1011') , 
(   232 , b'1011') , 
(   257 , b'0010') , 
(   258 , b'1011')  ,
(   258 , b'1011') , 
(   258 , b'1011') , 
(   258 , b'1011') , 
(   258 , b'1011') , 
(   258 , b'1011') , 
(   258 , b'1110') , 
(   258 , b'1001') , 
(   258 , b'0011') , 
(   258 , b'1001') ;

update t
set abit = accessto;

    MariaDB [sandbox]> select bin(abit),cast(abit  as unsigned) from t;
+-----------+-------------------------+
| bin(abit) | cast(abit  as unsigned) |
+-----------+-------------------------+
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 10        |                       2 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1110      |                      14 |
| 1001      |                       9 |
| 11        |                       3 |
| 1001      |                       9 |
+-----------+-------------------------+
14 rows in set (0.00 sec)

2 Comments

yes absolutely. Since "b'1011'" is already in bit notation for insertion. Thanks.
This can be done without an additional table, see my answer.
-1

You should using CONV mysql function

select id, CONV(BINARY(accessto), 2, 10) from tutor;

Comments

-1

-- Replace 'binary' with 'UNSIGNED'

select id, cast(accessto as UNSIGNED) from tutor;

1 Comment

All converted to 0 :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.