I have a sql code with filter conditions as below
Select * from TABLE A
Where Col1<>0
and(col2 is not null or col3 is not null)
Please explain why i do not see any records in the output when a record has NULL on both col2 and col3.
How is this is evaluated??
Null check for Col1 should be the first in condition. if Col1 is null then checking null with a value e.g. Col1 = 0 or Col1 <> 0 will always return false. Below an alternate way to deal with nulls in Col1.
Select * from TABLE A
Where isnull(Col1, 0) <> 0
and col2 is not null
Related
What happens if I compare a column with itself and it is NULL ? Is this similar to floating point values where x == x only is false if the value is a NaN ?
It depends on the comparison you do with itself.
If you do
WHERE Col = Col
any rows where Col IS NULL will have the WHERE clause evaluate to UNKNOWN (rather than TRUE or FALSE) and the row will not be returned.
So WHERE Col = Col is equivalent to WHERE Col IS NOT NULL
If you do (not available in all RDBMS but standard SQL)
WHERE Col IS NOT DISTINCT FROM Col
Then this will evaluate to true
The comparsion with = is not NULL safe, which means the result is UNKNOWN
So you should check your database for NULL safe comparisons
On MySQL it is <=>
Postgres uses col1 IS DISTINCT FROM col2
SQL Server hasn't one till Version 2022, you can uses the last option in query. Since Version 2022 it also supports col1 IS DISTINCT FROM col2
SELECT col1 = col2,col1 <=> col2, col1 = col2 OR (col1 IS NULL AND col2 IS NULL) FROM tab1
col1 = col2
col1 <=> col2
col1 = col2 OR (col1 IS NULL AND col2 IS NULL)
null
1
1
fiddle
I am trying to return all Col1 values IF the respective Col2 value = NULL
I have thought of IF Col2 = NULL Return Col2 END IF, But I am unsure of how to format this, because when I run it I get errors saying invalid relational operator. Thank you for your time.
SELECT Col1
FROM mytable
WHERE IF NULL Col2 = 0;
IF Col2 = NULL
RETURN COL1
ELSE
END IF;
================================================================
EDIT
I apologize for the lack of information/Show of research. First post, So I have been searching google for different IF then statements to deal with NULL values. Everything I have found this far has only showed how to Ignore/convert to 0 so that you can easily work with them. Nothing on how to use them to return values from another col1
use COALESCE. it resturns the first non null value
SELECT
COALESCE(col1,col2) AS FINAL_COLUMN
FROM [TABLE NAME]
SELECT Col1
FROM mytable
WHERE Col2 IS NULL
No IF is required. WHERE is the SQL IF for row selection. This returns only the rows where Col2 is null.
If you want to return all the rows, but want to return Col1 when Col2 is null and NULL otherwise, use
SELECT
CASE WHEN Col2 IS NULL THEN Col1 ELSE NULL END as myResultColumn
FROM mytable
I have table like below:
I want result like even one column is null or empty string those records should not be display.
I want results like below:
select * from WHERE col1 IS NOT NULL AND col2 IS NOT NULL AND col3 IS NOT NULL AND col4 IS NOT NULL AND col5 IS NOT NULL....AND col9 IS NOT NULL
If you wanted an efficient way to do this, then one method would be a persistent computed column:
alter table t
add numNulls as ( (case when col1 is null then 1 else 0 end) +
(case when col2 is null then 1 else 0 end) +
(case when col3 is null then 1 else 0 end) +
. . .
) persisted;
You can index this column:
create index t_numNulls on t(numNulls);
And then you can use this in the select:
select t.*
from t
where num_nulls = 0;
That said, I suspect that your real problem is the data model. I am guessing that those 90 columns are really an "array" -- that is, all the same entity. These should be implemented as separate rows in a junction table.
I have a table which has three columns with integer values. In first two columns I have some values. Now I need to update the third column with the value which is minimum of other two columns.
I have tried using
update table set col3 = (SELECT CASE WHEN (col2 is null OR col1 < col2 )
THEN col1
ELSE col2
END AS col3
FROM table
WHERE col1 is not null or col2 is not null)`.
But I am getting an error like below:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
You can use LEAST in order to update:
UPDATE mytable
SET col3 = LEAST(COALESCE(col1, col2), COALESCE(col2, col1))
WHERE col1 IS NOT NULL OR col2 IS NOT NULL
You don't have to query your table table to get col2, col1 values. You can use them directly in the SET part of your UPDATE query.
Note: COALESCE is used to handle NULL values. If both col1, col2 are NULL then col3 is also set to NULL.
In SQL Server you can use:
UPDATE mytable
SET col3 = CASE
WHEN col2 IS NULL OR col1 < col2 THEN col1
ELSE col2
END
WHERE col1 IS NOT NULL OR col2 IS NOT NULL
In SQL Server 2012, or later, you can use IIF:
UPDATE mytable
SET col3 = IIF(col2 IS NULL OR col1 < col2, col1, col2)
WHERE col1 IS NOT NULL OR col2 IS NOT NULL
Demo here
You don't need to join to the table it self, you can use the data from the columns in the same query
update table
set col3 = CASE WHEN (col2 is null OR col1 < col2 )
THEN col1 ELSE col2 END
WHERE col1 is not null or col2 is not null
The problem with your query is that you are using a correlated query, that selects col1 and col2(which is basically fine but unnecessary) and you are not filtering the results(which col1,col2 to take?) so for each col3 you want to update, you have a 'bunch' of other values, when you can only have 1. If you want your query to work all you have to do is add a relation condition.
I want in SQL : if a field is equal to a specific value, I want this record if not I want the record with this field equal to null.
I can try to that:
SELECT TOP 1 COL1, COL2, COL3
FROM TABLE1 WHERE (COL2 = MY_SPECIFIC_VALUE OR COL2 IS NULL) AND COL3 = '42'
AND COL1 = 3
But, what is the result returned? The smallest id? Or it is not specified?
Assuming that this is the real question:
If a field is equal to a specific value, I want this record if not I want the record with this field equal to null.
You can do this as:
SELECT TOP 1 COL1, COL2, COL3
FROM TABLE1
WHERE (COL2 = MY_SPECIFIC_VALUE or COL2 IS NULL) AND
COL3 = '42' AND COL1 = 3
ORDER BY (CASE WHEN COL2 = MY_SPECIFIC_VALUE THEN 1 ELSE 2 END);