How to update bit value in SQL [duplicate] - sql

This question already has answers here:
Boolean 'NOT' in T-SQL not working on 'bit' datatype?
(7 answers)
Closed 6 years ago.
I have a table in which I have 2 Column ID Int and IsActive Bit. I want to update bit value. If bit is true then false and if bit is flase then true.
For This I have used below query and its works fine but is there any other way to do this task.
Update Table_1 set IsActive = Case when IsActive = 0 then 1 Else 0 End
Thanks,
Hitesh Paghadal

Duplicate of Boolean 'NOT' in T-SQL not working on 'bit' datatype? which gives the answer "col = ~ col" as the XOR operator - it also illustrates every other method of doing it.

Related

SQL Case statement - Null + Not Null - then return a column [duplicate]

This question already has answers here:
SQL is null and = null [duplicate]
(4 answers)
Closed 11 months ago.
I use SQL inside a 3rd party system (So don't know the type it is)
I am trying to make a CASE work on a column using data from 2 more.
I want to display a column call Channel that is calculated using the following logic:
If column O.Test is blank and column o.subsource is not blank, display 'RESEND', otherwise display the value of column o.Source.
This is part of the SQL showing the CASE I wrote to do this:
select
-- other columns
(CASE
WHEN o.Test = NULL AND o.Subsource IS NOT NULL THEN 'RESEND'
ElSE o.Source
END) o.Source AS 'Channel',
-- other columns
The SQL runs with no errors but the output always shows what is in o.Source.
X = NULL is not equal IS NULL X
Check this question,
SQL is null and = null
change this statement "o.Test = NULL" instead of "o.Test IS NULL"

What's the difference between the queries? "SELECT * FROM bookings where code = null" and "SELECT * FROM bookings where code IS null" [duplicate]

This question already has answers here:
What's the difference between " = null" and " IS NULL"?
(4 answers)
Closed 4 years ago.
I get different outputs for these queries. Did anyone face this?
The col= null would not work because
NULL means I don't know. which is a placeholder to say there is the absence of a value.
= evaluate for values, so that will not to work.
You need to use IS NULL to get NULL row value.
=NULL IS is an expression of a value and IS NULL is the preferred method of evaluating the condition of a variable being NULL.
Here is the detail description
Click here
The first statement,
SELECT * FROM bookings where code = null--
It Checks Directly if any row has the value null
The Second Statement,
SELECT * FROM bookings where code IS null
It Checks all the rows its a null or not..
That's completely normal. "X = NULL" is NULL, which amounts to false ("NOT X = NULL" is also false; NULL is falser than false). For most SQL dialects.

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)

VB.NET Short-hand way to define a variable based on a boolean expression (ternary) [duplicate]

This question already has answers here:
Is there a conditional ternary operator in VB.NET?
(5 answers)
Closed 4 years ago.
In C# I can do this:
string outcome = (success?"succeeded":"failed")
But in VB.NET is this syntax the only equivalent operation?:
If (success) Then
outcome = "succeeded"
Else
outcome = "failed"
End If
outcome = If(success,"succeeded","failed")

Output of "select 1 where NULL <> -1;" [duplicate]

This question already has answers here:
why is null not equal to null false
(7 answers)
Closed 6 years ago.
I guess I don't fully understand the meaning of NULL in SQL. I ran this SQL and I expected to see 1 as the output but I didn't see that:
select 1 where NULL <> -1;
Isn't NULL and -1 different? Can anyone explain why this clause of "NULL <> -1" is FALSE?
NULL Is not a value and therefore cannot be compared with any other value and get any other result than null. Docs. And how to compare nulls.
NULL means unknown. So with where NULL <> -1 you want to know whether the unknown value equals -1. The DBMS does not know (of course), so the result of the expression is neither TRUE nor FALSE; it is NULL.
Only rows for which the WHERE clause results in TRUE are selected. As your expression doesn't result in TRUE but in NULL, there is no row selected.