snippetsqlMajor
How to cast an integer to a boolean in a MySQL SELECT clause?
Viewed 0 times
booleanclausecastmysqlhowselectinteger
Problem
I'm new here so be kind to me. I have the following scenario:
I have many tables which, for the sake of simplicity, are represented in a View in my MySQL database. My problem is that I need a value in this view representing if it is one kind of event or another (a simple boolean), which I tried to achieve with:
The result is represented as int, and so is read by Entity Framework. The problem is that I really need a boolean return value, which I tried to achieve with:
This resulted in an error, one that is not displayed to me in MySQL Workbench (I only receive that annoying "You have an error in ...").
Can you guys please help me out?
Tried to solve it in my application, but I really preffer this solved in the database, since it will be used by other software later.
I have many tables which, for the sake of simplicity, are represented in a View in my MySQL database. My problem is that I need a value in this view representing if it is one kind of event or another (a simple boolean), which I tried to achieve with:
`gu`.`StoppingUnitEventME` = `ese`.`MonitoringElement` AS `IsStopingEvent`The result is represented as int, and so is read by Entity Framework. The problem is that I really need a boolean return value, which I tried to achieve with:
CAST((`gu`.`StoppingUnitEventME` = `ese`.`MonitoringElement`) as boolean) AS `IsStopingEvent`This resulted in an error, one that is not displayed to me in MySQL Workbench (I only receive that annoying "You have an error in ...").
Can you guys please help me out?
Tried to solve it in my application, but I really preffer this solved in the database, since it will be used by other software later.
Solution
Try using the
or
Even without the
yield 0 or 1 using the mysql client
The problem is this:
Since
You could use the
IF() function:SELECT IF(`gu`.`StoppingUnitEventME`=`ese`.`MonitoringElement`, TRUE, FALSE)
FROM ...or
SELECT IF(`gu`.`StoppingUnitEventME`=`ese`.`MonitoringElement`, 1, 0)
FROM ...Even without the
IF() function, runningmysql> select ('rolando' = 'rolando') str_compare;
+-------------+
| str_compare |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
mysql>yield 0 or 1 using the mysql client
The problem is this:
CAST() and CONVERT() can only accept and return the following types:BINARY[(N)]
CHAR[(N)]
DATE
DATETIME
DECIMAL[(M[,D])]
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
Since
BOOLEAN is not in this list, it can never be returned by CAST() or CONVERT()You could use the
IF() function to generate stringsSELECT IF(`gu`.`StoppingUnitEventME`=`ese`.`MonitoringElement`, 'TRUE', 'FALSE')
FROM ...Code Snippets
SELECT IF(`gu`.`StoppingUnitEventME`=`ese`.`MonitoringElement`, TRUE, FALSE)
FROM ...SELECT IF(`gu`.`StoppingUnitEventME`=`ese`.`MonitoringElement`, 1, 0)
FROM ...mysql> select ('rolando' = 'rolando') str_compare;
+-------------+
| str_compare |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
mysql>SELECT IF(`gu`.`StoppingUnitEventME`=`ese`.`MonitoringElement`, 'TRUE', 'FALSE')
FROM ...Context
StackExchange Database Administrators Q#12569, answer score: 42
Revisions (0)
No revisions yet.