I have two columns as below:
Column A
Column B
A1
NULL
A1
A1
B1
C1.
When i query these columns as below :
SELECT Column A, Column B
from table
where Column A != Column B
i am expecting the following result:
Column A
Column B
A1
NULL
B1.
C1
But my query is only giving me the second line as result.
Consider below
select *
from your_table
where columnA is distinct from columnB
with output
Null values do not participate in equality operations as there is no value to compare with.
You will need to include a check for null into your comparisoon statement.
SELECT Column A, Column B from table where ISNULL(Column A,0) <> ISNULL(Column B,0)
null in SQL equals unknown
It could also have A1 as value, therefore it will not show up if you check if A1 is not null
You can use below workaround
select * from your_table
except distinct
select * from your_table
where columnA = columnB
with output
SELECT * from Your_table where IFNULL(`Column A`, 'NULL') != IFNULL(`Column B`, 'NULL')
IFNULL(expression, alt_value) function returns the value of the expression if it's not NULL and returns alt_value when expression is NULL.
So, from the above query, Column A and Column B will have the string value of 'NULL' when the column has the NULL value so they can still be comparable.
Related
I am looking to move B to the above row. It can either be placed where the Null value is in Column B or another column can be created. The value of B is linked to value A through an ID. The ID for value B is always X + 2 (the values in the ID column are integers).
I can’t just move the value up because the table I am working with has thousands of rows. It must be linked to the ID’s.
Please let me know if you have any questions. Any assistance is much appreciated. Thank you.
ID
Column A
Column B
X
A
NULL
X+2
NULL
B
Keep in mind I am very new to SQL. Below is what I tried. It created a new column that only contains NULL values.
Select
Column_B
From
Table_Name
Where
Table_Name.ID = Table_Name.ID +2 ) AS Col_B_Value
You can use a conditional subselect for that
UPDATE Table_Name T1
SET Column_B = (Select
Column_B
From
Table_Name
Where
Table_Name.ID = T1.ID +2 )
WHERE Column_B IS NULL
Some databases could have a problem so you can make
UPDATE Table_Name T1
SET Column_B = (Select
T2.Column_B
From
(SELECT ID,Column_B FROM Table_Name) T2
Where
T2.ID = T1.ID +2 )
WHERE Column_B IS NULL
You could just do it with 2 updates statements
UPDATE Table
SET Column B = 'B'
WHERE ID = 'X'
UPDATE Table
SET Column B = NULL
WHERE ID = 'X+2'
If you need to do it in a select statement you could do it with a case statement too
SELECT ID,
Column A,
CASE WHEN ID = X AND Column B = NULL THEN 'B'
ELSE Column B END
FROM Table
I want to filter the table without the row c
column 1
column 2
a
100
b
200
c
50
null
200
Desired output
column 1
column 2
a
100
b
200
null
200
I tried
select *
from table
where column1 <> 'c'
But since I can compare with null, I'm getting the wrong output. How do I deal with this?
You need to handle the null as follows:
select * from table where column1 <> 'c' or column1 is null
Or you can use the coalesce function as follows:
select * from table where coalesce(column1,'cc') <> 'c'
Coalesce will replace the null value in column1 with the value provided as the second argument. I have used the value which is not equal to 'c' so records with column1 as null will pass this condition
ANSI SQL, DISTINCT predicate.
select *
from table
where column1 is distinct from 'c'
However, not supported by all dbms products.
I have created a SQL scipt, I used the function NVL with condition "not null".
Can it check, row by row, which row is not null and which one is null?
select * from table where nvl(Column A,column B) is not null
Because maybe the column A is null and column not null.
To select A or B
select
case when A is not null then A else B end as AorB,
...
from table
Note that AorB returns null if both A and B are null. If you want to filter such records out:
select ...
from
(
select
case when A is not null then A else B end as AorB,
...
from table
)
where AorB is not null;
Does anyone know how to fill null value in PostgreSQL?
For example, Null values are replaced with X
column
A
B
Null
C
D
Null
should be:
column
A
B
x
C
D
x
you can use coalesce() to return a different value if a column is null
select coalesce(the_column, 'x') as the_column
from the_table;
I am trying to do :
SELECT *
FROM table
WHERE column IN (SELECT col FROM table2 WHERE col2 = value )
but I want to check if the second request doesn't return a null array.
How is that possible?
Thanks in advance
Simply add a NOT NULL check in the subquery to omit the null values returned.
SELECT * FROM table WHERE column IN
(SELECT col FROM table2 WHERE col2 = value AND col IS NOT NULL);