Is there any way in a simple query to return a Boolean value using an SQL EXISTS statement without using a WHERE clause?
All of the 2008 R2 SQL Server Books Online examples show another WHERE clause and two tables. Website examples show either a WHERE or an IF-THEN-ELSE in a procedure.
I was hoping to do the following on one table:
EXISTS (SELECT cx.id FROM fdd.admissions_view as cx WHERE cx.id=1111 and cx.campus='MEXI')
The SELECT statement works fine and returns the ID. I just want to add EXISTS to return a BOOLEAN, but the syntax above is not valid.
Can I do something like this? If so, what am I missing syntax-wise? If not, what other technique may work?
Please advise. Thanks.
Details of sql differ. SQL Server does not support a Boolean type e.g. SELECT WHEN CAST(1 AS BIT) THEN 'YES' END AS result -- results in an error i.e. CAST(1 AS BIT) is not the same logical TRUE.
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).
The SQL NOT condition (sometimes called the NOT Operator) is used to negate a condition in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement.
A Boolean expression is a logical statement that is either TRUE or FALSE . Boolean expressions can compare data of any type as long as both parts of the expression have the same basic data type. You can test data to see if it is equal to, greater than, or less than other data.
Here is one which uses EXISTS
with CASE WHEN ... THEN .. ELSE ... END
, tested with MySQL and Oracle:
SELECT CASE WHEN EXISTS (SELECT cx.id FROM fdd.admissions_view as cx WHERE cx.id=1111 and cx.campus='MEXI') THEN 1 ELSE 0 END FROM DUAL
Found some related Q/A:
How about something like
select case when count(cx.id) > 0 then 1 else 0 end FROM fdd.admissions_view as cx WHERE cx.id=1111 and cx.campus='MEXI'
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With