How to convert string to bitset in MySQL?

MySQLMySQLi Database

To convert string to bitset, use the CONV() method. Let us first create a table −

mysql> create table DemoTable
(
   stringValue BIT(4)
);
Query OK, 0 rows affected (3.50 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(CONV('1110', 2, 10) * 1);
Query OK, 1 row affected (0.62 sec)
mysql> insert into DemoTable values(b'1011');
Query OK, 1 row affected (0.14 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from DemoTable;

Following is the output that displays blank result because the type is bitset −

Following is the query to convert string to bitset and display the result −

mysql> select stringValue+0 AS DECIMAL_VALUE,BIN(stringValue) AS BINARY_VALUE from DemoTable;

This will produce the following output −

+---------------+--------------+
| DECIMAL_VALUE | BINARY_VALUE |
+---------------+--------------+
| 14            | 1110         |
| 11            | 1011         |
+---------------+--------------+
2 rows in set (0.04 sec)
raja
Updated on 30-Jul-2019 22:30:25

Advertisements