Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert bool to int in MySql

Tags:

mysql

I'm new to MySql. So I don't know many things like Casting of data types. How can I convert bool to int in MySql. And also how can I convert decimal to Int in MySql.

like image 439
Fasal Avatar asked Jun 07 '11 04:06

Fasal


People also ask

Is there a bool data type in MySQL?

MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.

How do you set a boolean to true in MySQL?

You can update boolean value using UPDATE command. If you use the BOOLEAN data type, MySQL internally convert it into tinyint(1). It can takes true or false literal in which true indicates 1 to tinyint(1) and false indicates 0 to tinyint(1).


1 Answers

Typecasting boolean to integer:

SELECT CAST(1=1 AS SIGNED INTEGER); /* 1 */

Same for decimal and strings:

SELECT CAST("1.23" AS SIGNED INTEGER); /* 1 */
like image 172
BenMorel Avatar answered Sep 17 '22 20:09

BenMorel