Initialize number by NULL [duplicate] - sql

This question already has answers here:
What is NULL in SQL?
(5 answers)
Closed 4 years ago.
What is the difference between initializing a number type variable by null and by Zero in PLSQL?
example-
l_person_id NUMBER :=NULL;
and
l_person_id NUMBER :=0;
Does this effect anywhere in a code?

There is a difference.
If you have comparison operations in your code then you can get unexpected behaviour:
5 > l_person_id_null -- false
5 > l_person_id_0 -- true
Also, with NULL you'll have to use IS NULL and IS NOT NULL operators instead of =.

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"

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.

How to update bit value in SQL [duplicate]

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.

difference between like and = [duplicate]

This question already has answers here:
Equals(=) vs. LIKE
(16 answers)
WHERE clause on SQL Server "Text" data type
(7 answers)
Closed 7 years ago.
what is the differences between = to like ?
thanks for help :)
by the way if it's help , the error i get is: the data types text and varchar are incompatible in the equal to operator [msg 402]
1:
Like
The LIKE operator is used to search for a specified pattern in a column.
Where
The WHERE clause is used to extract only those records that fulfill a specified criterion.
In your case the first query do not work is because no records match the criteria you are searching.

Count null values using CASE WHEN [duplicate]

This question already has answers here:
SQL is null and = null [duplicate]
(4 answers)
Closed 8 years ago.
I am trying to count the number of missing postal codes using CASE to flag null values as 1, otherwise 0.
CASE WHEN ([postcode]=NULL)
THEN 1
ELSE 0 END AS pc_missing
Even though I know there are lots of missing data, none of them receive the value of 1.
What have I done wrong?
null is not a value - it's the lack of a value. null returns "unknown" (which is not true) when evaluated against any value operator - =, !=, >, <, etc. Instead, you should check it with the is [not] null operator:
CASE WHEN ([postcode] IS NULL)
THEN 1
ELSE 0 END AS pc_missing