how can I fill null value with S.T in PostgreSQL? - sql

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;

Related

Oracle sql case when exclude cells containg string

I have this sql query which works well:
SELECT distinct
A,
CASE
WHEN Parameter = 'ID' and regexp_like (VALUE, 'FOO')
THEN VALUE
ELSE 'NA'
END AS test FROM my_table;
my_table
A
parameter
value
x
ID
FOO1223
y
ID
FOO5462
z
ID
empy
p
ID
BAR5432
result:
A
value
x
FOO1223
y
FOO5462
z
NA
p
NA
Now I would like to exclude VALUE that starts with 'BAR'. How can I add this to the CASE...WHEN statement?
The output should look this:
A
value
x
FOO1223
y
FOO5462
z
NA
Do NOT LIKE in the WHERE clause to skip the rows where value start with BAR.
SELECT distinct
A,
CASE
WHEN Parameter = 'ID' and regexp_like (VALUE, 'FOO')
THEN VALUE
ELSE 'NA'
END AS test FROM my_table
WHERE value NOT LIKE 'BAR%'

Compare nulll value with non null value SQL

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.

Excluding a value when null is present in the column

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.

NVL function in oracle can be filter with the other condition?

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;

Sum of null columns in SQL

I have a table where A, B and C allow null values.
SELECT
A, B, C,
A + B + C AS 'SUM'
FROM Table
If my A, B and C values are 10, NULL and NULL, my SUM column shows nothing in it.
Is there any way to fix this, and display SUM as 10? OTHER THAN converting NULL s to Zeros?
You could use SQL COALESCE which uses the value in the column, or an alternative value if the column is null
So
SUM ( COALESCE(A,0) + COALESCE(B,0) + COALESCE(C,0))
In this case you need to use IsNull(Column, 0) to ensure it is always 0 at minimum.
SELECT
A, B, C,
IsNull(A,0) + IsNull(B,0) + IsNull(C,0) AS 'SUM'
FROM Table
ISNULL() determines what to do when you have a null value. if column returns a null value so you specified a 0 to be returned instead.