TSQL - Return Boolean result in select query - sql

SQL Server does not seem to support selecting Boolean result.
Example:
Select True;
Or
Select 1>2;
would return an Error.
However, in the majority of programming languages a similar operation is valid. Is there a reason for that?

SQL is not designed for deductive statements. however you can achieve similar results using a statement like this:
SELECT CASE WHEN 2 > 1 THEN 1 ELSE 0 END

Related

What does XI8 mean in a SQL query? (Teradata/Big Query)

At work I saw I saw a SQL query in Teradata that had the following:
CASE WHEN (sp_ce / ‘0000001000000000’XI8) MOD 2 = 1 THEN ‘Y’ ELSE ‘N’ END AS res_p
What is the meaning of XI8? What is it doing to the number string?
Also when I tried to run this in Big Query it’s not supported, it errors out with
Syntax error: Unexpected identifier \XI8\
How would I write that case statement in big query?
Using MOD on big query without the numbers as a string and without the XI8 yields different results.
Thank you

Oracle CASE missing right parenthesis for a "in" limit

I have a QRY im developing in Oracle for spotfire. In the where statement, I have a decision case statement and if its True, im trying to pass a list of items to match a column, below is what I have, but its throwing a missing right parenthesis error and I cannot determine why. In short, when a variable is determined True (in this case 9>8 for the example, I need it to result those items, else, result the entire column with no limits.
Note: This works fine when its only 1 item being passed, i.e. 'BOB' but as soon as its multiple, this error occurs.
and Column1 = (CASE When 9>8 Then ('BOB','TOM') Else Column1 END)
Case expressions are best avoided in the where clause. Instead, write the logic with AND and OR:
And (
(9>8 AND Column1 IN ('BOB','TOM'))
OR 9<=8 -- You say you check a variable here, don't forget to check for NULL
)
Oracle does not have a boolean type for use in SQL queries.
Instead, just use basic logic:
and ( (9 > 8 and Column1 in ('BOB','TOM')) or
9 <= 8
)

Conditional WHERE clause with operator in statement [duplicate]

This question already has answers here:
Conditional WHERE statement SQL Server
(4 answers)
Closed 4 years ago.
I have a stored procedure that I'm trying to do a conditional WHERE clause on. I believe the solution is easy but for some reason it's escaping me.
What I'm trying to achieve is: if a ID (#pbid in this case) is passed to the stored procedure I only want to return a single record matching that parameter otherwise I'd like to return all results in the table.
Here is the what I have come up with and it's obviously not going to work because I'm trying to do an equals evaluation at PBID (PBID =) and I'm looking to conditionally make that
where pb.PBID =
CASE WHEN #pbid is not null THEN
#pbid --just return results with this ID
ELSE
is not null --return all results
END
If it was C# or something I'd write it like:
if(intPBID > 0) --parameter set, only return results with that param
{
pb.PBID = intPBID
}
else
{
pb.PBID > 0 --return everything
}
I hope this isn't too confusing and I'd appreciate any feedback.
Thanks
I'm not sure about performance implications, but I typically end up doing something like:
SELECT *
FROM table
WHERE (#pbid IS NULL OR pb.PBID = #pbid)

Firebird If-Else with Queries

I have the following problem:
I'd like to use a specific query depending what number a user has set in Firebird SQL, so in pseudo code:
IF (TABLENAME.USERFLAG <> 5 WHERE TABLENAME.USER = 15555) THEN
EXECUTE QUERY 1
ELSE
EXECUTE QUERY 2
The problem is, that does not seem to work. What could I do to get it working?

Boolean expression as column value in transact sql

In most RDBMS:es, this work:
select (5 > 3)
and evaluates to true. It doesn't work in MS Transact SQL and the only workaround I've found is to write:
select case when 5 > 3 then 1 else 0 end
Which kind of sucks because it is much more verbose. Is there a better way to write the above kind of checks?
If the problem is arithmetic comparison:
select (5 - 3)
Then at the application level test for < or = or > 0.
You could write it as a scalar-valued function, but it will be very slow on large datasets.
If your program often requires such case constructs you could create your set of functions that will have user functions like Bool_IsGreater(left, right) that will return you your desired 0 or 1.
SQL Server doesn't support boolean value type anyway even for basic column use.
If you will need performance and those 5 and 3 values come naturally from some select query you might want to create a new column and set its value to 1 or 0 by trigger or something, which could help with performance.