Convert TinyInt to Boolean MySQL has now become a simple process. Bobcares, as a part of our MySQL Support Services offers solutions to every MySQL queries that comes our way.
Convert TinyInt To Boolean In MySQL
A TINYINT is an 8-bit integer value, a BIT field can store between 1 bit, BIT(1), and 64 bits, BIT(64). For boolean values, BIT(1) is pretty common. TINYINT uses 1 byte of storage and is the smallest integer data type. (1) in the Tinyint(1) is for formatting options, which are typically ignored. It would be irrelevant if we created it as tinyint(100).
While determining whether something is TRUE or FALSE, any int (int, tinyint, smallint, bigint) value converts to a boolean value. If it’s 0, it’s considered FALSE; if not, it’s considered TRUE. So, 2 is also TRUE here. BOOL, BOOLEAN
are synonyms for TINYINT(1).
mysql> SELECT IF(0, 'true', 'false'); +------------------------+ | IF(0, 'true', 'false') | +------------------------+ | false | +------------------------+
mysql> SELECT IF(1, 'true', 'false'); +------------------------+ | IF(1, 'true', 'false') | +------------------------+ | true | +------------------------+
mysql> SELECT IF(2, 'true', 'false'); +------------------------+ | IF(2, 'true', 'false') | +------------------------+ | true | +------------------------+
However, as shown here, the values TRUE and FALSE are merely aliases for 1 and 0, respectively.
mysql> SELECT IF(0 = FALSE, 'true', 'false'); +--------------------------------+ | IF(0 = FALSE, 'true', 'false') | +--------------------------------+ | true | +--------------------------------+
mysql> SELECT IF(1 = TRUE, 'true', 'false'); +-------------------------------+ | IF(1 = TRUE, 'true', 'false') | +-------------------------------+ | true | +-------------------------------+
mysql> SELECT IF(2 = TRUE, 'true', 'false'); +-------------------------------+ | IF(2 = TRUE, 'true', 'false') | +-------------------------------+ | false | +-------------------------------+
mysql> SELECT IF(2 = FALSE, 'true', 'false'); +--------------------------------+ | IF(2 = FALSE, 'true', 'false') | +--------------------------------+ | false | +--------------------------------+
Tinyint(1) in MySQL is the equivalent of boolean. Although it can store values between -127 and 127, by default, SQL Runner will display the values as “true” for non-zero values and “false” for zero values.
How To Convert TinyInt To Boolean In MySQL?
- By using a cast:
SELECT cast(tiny_int_value as signed) FROM table
- Another way is by updating the database connection. Then add this as an additional parameter:
tinyInt1isBit=false
[Looking for a solution to another query? We are available 24/7.]
Conclusion
In this article, our skilled Support Engineers at Bobcares took us through converting tinyint to boolean in MySQL.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments