NVL Function in SQL - sql

what excatly 1 - NVL(up, 0) down this statement mean , is it giving me the contradiction of up ?

Presumably, up is a value that can take up to three values: 0, 1, and NULL.
The NVL() function is a database-specific function for COALESCE(). They do the same thing.
So, simply look at what the results are:
up 1 - coalesce(up, 0)
1 0
0 1
NULL 1
So, it is "flipping the switch". That is, when "up" is "true", then it switches it to "false". It treats 0 as "false" for this purpose.

Related

Coalesce different with case

i get confused in learning coalesce, I am new to sql.
example :
select case when value is null then 1
else value end as value
from table
and
select coalesce(value, 1)
from table
and in the tutorial I see in internet there are like
select coalesce (arg_1, arg_2, arg_3)
if I make
select coalesce(value, 1, 2)
how I can make to show the return value is 2?
Your query with the first and second will reproduce the same result, But you are wrong understanding the Coalesce concept.
Definition in Documentation Postgresql
The COALESCE function returns the first of its arguments that is not
null. Null is returned only if all arguments are null.
So it means it will return the first argument that is not null, it is not like case statement with condition like true or false
Let's try with example :
select coalesce(null, 1)
It will return 1 like the query you show, or
select coalesce(null, null, 1)
It will return 1 too even 1 in the arg_3 and how about there are 2 value not null?
select coalesce(null, 1, 2)
It will return 1. Why? Like in the documentation said "returns the first of its arguments that is not null" so when there is 2 value not null the first argument have not null value will get return
You can check this demo and try :
Demo<>Fiddle
Hope it helps

Is there boolean type for column in Oracle SQL?

I tried to:
select 1>2 from dual;
but got:
ORA-00923: FROM keyword not found where expected
Is there boolean type for column expression in Oracle SQL?
I able to do:
select case when 1>2 then 'T' else 'F' end from dual;
Originally I tried to compare date fields and the quickest way I found was getting difference and look to sign...
UPDATE I tried SIGN function, I don't know if it is vendor specific extension:
select SIGN(1-2) from dual;
select SIGN(DATE '2017-01-02' - DATE '2017-02-12') from dual;
but this trick doesn't work for strings...
No there is not, you can use 0 and 1 just as yes/no.
If you need to get the result 1 if something is true and 0 if it is false, you can use a case expression:
select case when (any_logical_condition_here) then 1 else 0 end as my_col
from ....
where ....
For example:
select case when 1 > 2 then 1 else 0 end as bool_result
from dual;
BOOL_RESULT
---------------------------------------
0
NOTE though - "Boolean" refers strictly to the TRUE/FALSE logic, it has no place for UNKNOWN. When you deal with null, as you must in SQL, you need three-valued logic. The case expression as written above returns 1 when the logical condition is true and 0 otherwise. Try it with 1 > null - the truth value is UNKNOWN, the case expression will return 0.

"!="/NOT perhaps not working properly in SQLite

I have a table with about a hundred rows. It has a column is_gallery that contains either 1, 0, or NULL. If I do...
SELECT * WHERE is_gallery != 1
or
SELECT * WHERE NOT (is_gallery = 1)
it excludes the rows where is_gallery is null. I can manage to get a proper response if I do
SELECT * WHERE (is_gallery = 0 OR is_gallery is null)
But shouldn't the "!=" or NOT work? Isn't there a way to just return the rows where is_gallery doesn't equal 1 without testing for every other possibility?
You can use the IS and IS NOT operators instead of = and !=. These treat NULL like a normal value.
SELECT * FROM yourTable WHERE is_gallery IS NOT 1
The best thing to use is coalesce as in:
SELECT *
WHERE coalesce(is_gallery,0) != 1;
what coalesce does, is replaces any null value in that column with the second parameter. In the example above, any nulls in the "is_gallery" column will be replaced with 0 before it is compared with 1. So will of course return true.
On NULL realize that a NULL value isn't equal to ANYTHING - not even NULL itself. It cannot be compared - so when "comparing", it always will return FALSE. On NULL, it has a special operator which is "IS NULL" or "IS NOT NULL"

Simple conversion of possibly-NULL value to flag

Is there a simple way to get 1 for a non-NULL column and 0 for a NULL column (e.g., in a SELECT), without adding a scalar function to do it?
My first option would be the already posted answer by the OP, but an alternative is
select isnull(column * 0 + 1, 0)
Basically, if column is not NULL, then column * 0 + 1 will be 1, otherwise it will be NULL. I'm assuming column is an integer column, as suggested in the comments on the question, but it's usable for any type so long as you've got a function that converts that type to an integer (and returns NULL if and only if its input is).
It's similar in spirit to Jayvee's answer, but avoids any problems with any division by zero, and should be a bit easier to understand.
Three basic options:
CASE WHEN:
CASE WHEN [TheColumn] IS NULL THEN 0 ELSE 1 END AS [MyFlag]
or if you want it as a BIT:
CAST(CASE WHEN [TheColumn] IS NULL THEN 0 ELSE 1 END AS BIT) AS [MyFlag]
Whether that's "simple" is a matter of opinion...
If you know non-NULL values will never be 0 (for an INT column) or 'FALSE' (for a character column), you can shorten that a bit:
CAST(COALESCE([TheColumn], 0) AS BIT) AS [MyFlag]
...but again note the assumption about 0/'FALSE'.
On SQL Server 2012+, you can use IIF (thank you, Martin Smith):
IIF(TheColumn IS NULL, 0, 1) AS [MyFlag]
another way:
select isnull(ascii([TheColumn])/ascii([TheColumn]),0)

Mysql query deducting 2 counts result less 0

I have a a query which retrieves 2 times a count from 2 tables.
Now in the same query it has (countresult1-countresult2) AS restresult
Now restresult is sometimes less than 0 (eq -10) but I want it to return 0 if it's under 0.
Uhm did I explan that right? Minimum value should be 0 not below.
Cheers!!!
GREATEST((countresult1-countresult2), 0) AS restresult
if (countresult1<countresult2, 0, countresult1-countresult2) as restresult
neither countresult1 nor countresult2 will return a negative number, so above should be safe
without seeing your query, you could have something like...
MAX( if( countresult1-countresult2 < 0, 0, countresult1-countresult2 )) as YourResult
Take the maximum of 0 and the value you calculated, like this:
SELECT GREATEST(your-restresult-query,0)
FROM ... (etc)
Until Now I didn't know there was if-else commands in SQL, but I found some.
you will want to use:
WHEN (countresult1-countresult2) < 0 THEN 0 ELSE (countresult1-countresult2)
Here is the source where I found the SQL information: http://www.tizag.com/sqlTutorial/sqlcase.php