I have the following condition:
if column is NULL, or have value like Unassessed or ABC then the result should be I.
I need help how to put three in one condition like below. Just confused with NULL value. And it is string in column.
JobAssessStage ='Unassessed','','ABC'
JobAssessStage in ('Unassessed','','ABC')
Use below condition with is null
JobAssessStage in ('Unassessed','ABC') or JobAssessStage is null
use case when for multiple condition
select case when JobAssessStage in ('Unassessed','ABC') or
JobAssessStage is null then 'I' else 'You' End
from yourtable
In some cases you can use also use COALESCE function (it returns the first non-null expression in a list) on JobAssessStage field:
COALESCE(JobAssessStage, 'X') in ('Unassessed','ABC','X')
If JobAssessStage is NULL with COALESCE the value you're testing becomes X.
I told you "in some cases" because you have to be sure that the value you have chosen (X) is not a value that your field can take.
You can formulate your like below:
SELECT *
FROM table
WHERE 1 = 1
AND (JobAssessStage IN ('Unassessed', 'ABC')
OR JobAssessStage IS NULL);
Related
I have a view xxabc_v (shown below), I need to update the "Code" column to Null wherever it is N/A when "Value" column sum (900+(-900)=0) becomes zero for the "field_name" values (Demand A+Demand B) for the "Date" 01-Apr-21.
How can I put the decode logic to code column in the above case?
Table structure and expected output:
You don't want decode() because a much simpler method works:
select nullif(code, 'N/A')
This returns NULL when code takes on the specified value.
If you actually want to change the data, then you want update:
update t
set code = NULL
where code = 'N/A';
EDIT:
I see, you have an extra condition. So, use case:
(case when code = 'N/A' and
sum(value) over (partition by id, date) = 0
then NULL
else code
end)
I assumed that you need date wise id wise sum when to sum(). Please check this out:
select date,id,(case when sum(value)over(partition by date,id)=0 and code='N/A' then NULL
else Code end)code, field_name,value
from tablename
I am using Postgres and have the following SQL statement:
SELECT *
FROM "osmlocal-dsd-de".t_osm_vehicle_image t
WHERE t.vehicle_config_id = 3
and image_type_id = 2
Which returns one row:
id vehicle_config_id cosy_url image_type_id
113 3 SomeValue 2
When I run the following:
SELECT * from "osmlocal-dsd-de".t_osm_vehicle_image t
WHERE t.vehicle_config_id = 3
and image_type_id = 2
and coalesce(t.cosy_url, '') = ''
Zero rows are returned.
I think my understanding of coalesce is wrong, because I would have expected one row still to be returned, because the cosy_url is not null.
Any advise on what I am doing wrong would be appreciated.
Your understanding of coalesce is wrong
It returns the first argument that is not null. If all arguments are null, the COALESCE function will return null
In your case t.cosy_url is not null it is equally SomeValue and your condition doesn't work because SomeValue is not equal ''
You seem to be misunderstanding coalesce(). It returns the first value that is not null.
In your case, you have:
coalesce(t.cosy_url, '')
Because t.cosy_url has a value ('SomeValue'), this evaluates to that value. The value is not '' so the expression returns false and the entire where clause returns false.
If you want non-NULL values, then use:
t.cosy_url is not null
When querying a boolean field that is null why does
Select * From MyTableName where [boolfieldX] <> 1
not return any rows with null in [boolfieldX]? 1 <> null I would have expected rows with null to be returned.
It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
You have to use the IS NULL and IS NOT NULL operators instead, or you have to use functions like ISNULL() and COALESCE()
Select * From MyTableName where [boolfieldX] <> 1 OR [boolfieldX] IS NULL
OR
Select * From MyTableName where ISNULL([boolfieldX],0) <> 1
Read more about null comparison in Stackoverflow Documentation
Read more about ISNULL() and COALESCE() Functions in Stackoverflow Documentation
I believe that it's because null is an unknown value. You can't query against an unknown 'value'. In my opinion, referring to null as a 'value' is an oxymoron because it represents an unknown. Using the operators "Is Null" and "Not Is Null" in conjunction with whatever selection criteria will return the desired results, or translating it by converting a null an alternate value will work like this: IsNull([boolfield], 'some compatible value')
Hi try to use this query:
select * from mytablename where [boolFieldX] is null And [boolFieldX] <> 1
I can't believe I'm having so much trouble with this.
Using this statement:
USE XXXX
SELECT
ID, DESCRIPTION, STATUS
FROM
PART
WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
AND PART.STATUS = 'O'
I get a table with 34 entries, each of them containing O in PART.STATUS.
What I actually want to say is, only show me the values which do NOT have a status of O. I know there are other ways around this. values that are not O should be null, but I'm annoyed that I can't figure out how the 'not equal' statement works. When I switch the last line to:
AND PART.STATUS <> 'O'
OR
AND PART.STATUS != 'O'
I get an empty table returned.
If I use the line
AND PART.STATUS IS NULL
I get the table I'm looking for.
What am I misunderstanding about the use of 'not equal statements'?
The problem is 3 valued predicate logic. When at least one side of predicate is NULL the result of predicate is UNKNOWN(no matter you use = or <> or > or <, ...), but WHERE clause only returns rows where predicate evaluates to TRUE. So our job is to make predicate to evaluate to TRUE when PART.STATUS IS NULL. This is done by adding additional check on NULL like:
USE XXXX
SELECT ID, DESCRIPTION,STATUS
FROM PART
WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
AND (PART.STATUS <> 'O' OR PART.STATUS IS NULL)
Here is a little example. Imagine this is your table and you are issuing your statement WHERE PART.STATUS <> 'O'
PART(STATUS)
'A'
'O'
NULL
It evaluates to:
WHERE 'A' <> 'O' --TRUE
WHERE 'O' <> 'O' --FALSE
WHERE NULL <> 'O'--UNKNOWN
Since WHERE clause returns only rows where result of predicate is TRUE, you will get only 'A' here.
the issue here is with the nullvalue since every logical comparation against it would return false for example
PART.STATUS = NULL-- Would be false
PART.STATUS <> NULL-- would also return false
so you should do your comparison like
AND (PART.STATUS <> 'O' OR PART.STATUS IS NULL)
The part you're missing is that NULL is not a value, but the absence of it. A NULL means that the value in that field is either inexistent or unknown. That's why you cant' directly compare a value to a NULL. As stated in other answers, you have to use PART.STATUS IS NULL. Another option would be to use the ISNULL function, wich will test a value for NULL and, if it is, will return whatever value you specify on the second parameter. E.g.
USE XXXX
SELECT ID, DESCRIPTION,STATUS
FROM PART
WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
AND ISNULL(PART.STATUS, '') <> 'O'
Check the documentation for NULL in Sql Server and the ISNULL function. Also, this question could be of use.
USE XXXX
SELECT ID, DESCRIPTION,STATUS
FROM PART
WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
OR PART.STATUS != 'O'
Maybe your misunderstanding is probably about the NULL concept. NULL is not an empty string neither different from 'O', it's just NULL.
That's why you have to use
AND PART.STATUS IS NULL
or
AND isnull(PART.STATUS, '') <> 'O'
It is important to remember how NULL is treated in a database. It isn't a value at all!
No operator (>,<,=.. etc) less (is) used will ever return rows with NULL.
writing in SQL "where column = NULL" is like saying "give me all rows where the value isn't a value
Null values can't be compared with equals(=) or not equals operators. Try the below:
USE XXXX
SELECT ID, DESCRIPTION,STATUS FROM PART WHERE
PART.ID LIKE 'PCH%'
AND PART.DESCRIPTION NOT LIKE '%OBSOLETE%'
AND ISNULL(PART.STATUS, '') <> 'O'
I have this:
**value**
S:581930640 | P:581930640
And I would like to get the value as in Oracle:
**valuaA ValueB**
581930640 581930640
select
case
when field like 's:%'
then substr(field,3,13)
else null
end as A,
case
when field like 's:%'
then substr(field,17)
else null
end as B
from table;
both the case condition are same, but the substring is different, this should do what you are trying to do.