Can you change the value of a column that is NULL using a CASE? - sql

I know you can use COALESCE and ISNULL but I was jut wondering if you could do it with a SELECT case.
I had this
SELECT (CASE Table.Column WHEN ' ' THEN '1/1/2001' Else Table.Column End),Column2
FROM Table
That didn't do anything so I tried:
SELECT (CASE Table.Column WHEN NULL THEN '1/1/2001' Else Table.Column End),Column2
FROM Table
Nothing.
Just Curious. Thanks!

You can certainly use the is [not] null predicate like so:
select
case
when t.Column1 is null then '1/1/2001'
else t.Column1
end
,t.Column2
from Table1 as t
However, there are functions built specifically for dealing with null:
1. isnull(check_expression , replacement_value)
Replaces NULL with the specified replacement value.
select
isnull(t.Column1, '1/1/2001')
,t.Column2
from Table1 as t
2. coalesce(expression [ ,...n ])
Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.
select
coalesce(t.Column1, '1/1/2001')
,t.Column2
from Table1 as t

Use IS with NULL
SELECT CASE WHEN Column IS NULL
THEN '1/1/2001'
ELSE Column
END,
Column2
FROM Table

nulls require IS keyword, not WHEN or =

Related

How to output 'is null' in a where clause in a case when statement?

The query checks the value of a parameter foo which is passed by a dropdown inside a program. If that paramater contains a certain value, an attribute should only contain null values. Can I manage that without pl/SQL?
select * from table t
where case when $foo$ = 'yes' then t.something is null end
Do you mean this logic?
select something
from table t
where ($foo$ = 'yes' and t.something is null) or ($foo != 'yes')
Just use nvl function :
select *
from mytable t
where nvl($foo$,'yes') = 'yes';

Column is null using Case When SQL

I'm using SQL Developer
I need to accept a user input as either Y or N.
If N is entered, I need to convert this to null. If Y is entered it should remain Y. The script then needs to search for a column with either a null value or a value of Y depending on what is entered by the user.
I have the code below with a decent idea for how to tackle it
accept WITHDRAWN char prompt 'Please enter Y or N)'
SELECT *
FROM table1
WHERE column1 = case when '&WITHDRAWN' = 'N' then null else '&WITHDRAWN' end;
As you can probably tell, it doesn't work, as for the column to be null, the '=' sign would need to be an 'is'. Likewise for the column to be 'Y' then the sign would need to be '='.
I really can't figure out a way to substitute N for the value of null and allow column1 to match up to this. Is it even possible?
I'd coalesce the column1 value so when it's Null, N is substituted and compared to the user input.
SELECT *
FROM table1
WHERE coalsece(column1,'N') = &WITHDRAWN;
Assumption being in table1 all column1's that are null are infact 'N' so the coalesce simply allows us to do a character to character comparison.
Also be aware that indexes on column1 will be ignored since we had to use a function to get the 'N' value returned.
You don't need case at all:
SELECT *
FROM Table1
WHERE
(
'&WITHDRAWN' = 'N'
AND Column1 IS NULL
)
OR Column1 = 'Y'
select * from table1
where (
(column1 = '&WITHDRAWN' and '&WITHDRAWN' != 'N')
or
(column1 is null and '&WITHDRAWN' = 'N'))
Hope this help!
SELECT * FROM table1
WHERE (
(column1 = '&&WITHDRAWN' AND '&&WITHDRAWN' = 'Y')
OR
(column1 IS NULL AND '&&WITHDRAWN' = 'N'))

Case when statement in SQL

I am using the following query. In this query I want to apply the where clause based on passed parameter. But the issue is that where clause is like 'value = if parameterVal = 'I' than NULL else NOT NULL'
I've build a query like this
SELECT * FROM MASTER
WHERE
Column1 IS (CASE WHEN :Filter = 'I' THEN 'NULL' ELSE 'NOT NULL' END)
but it's not working. Help me solve this.
UPDATE
Updating question to elaborate question more clearly.
I've one table MASTER. Now I am passing one parameter in query that is Filter (indicated by :Filter in query).
Now when the Filter parameter's value is 'I' than it should return the following result.
SELECT * FROM MASTER WHERE Column1 IS NULL
but if the passed argument is not equal to 'I' than,
SELECT * FROM MASTER WHERE Column1 IS NOT NULL
SELECT * FROM MASTER
WHERE (Filter = 'I' AND Column1 IS NULL)
OR
(Filter <> 'I' AND Column1 IS NOT NULL)
If you really insist on using a CASE the SELECT could be rewritten as:
SELECT *
FROM MASTER
WHERE CASE
WHEN COLUMN1 IS NULL AND FILTER = 'I' THEN 1
WHEN COLUMN1 IS NOT NULL AND FILTER <> 'I' THEN 1
ELSE 0
END = 1
SQLFiddle here
Frankly, though, I think that this is very difficult to interpret, and I suggest that #MAli's version is better.
Your case has assignment not equality check

Oracle conditional select

Im sure this is an easy one.
How would I do a conditional value select on a column.
Basically if column1 ='Y' then display as "FOO" else if 'n' display "foobar"
select column1 from table1;
With a simple case expression:
select case column1 when 'Y' then 'FOO' else 'foorbar' end
from table1;
That assume a simple if/else. Your question specifies two values, and you can check both:
select case column1 when 'Y' then 'FOO' when 'n' then 'foorbar' end
from table1;
If you have any column1 values other than Y and n you'd get null; you can still specify a different value with an else even if you're testing for more than one explicit value:
select case column1 when 'Y' then 'FOO' when 'n' then 'foorbar' else 'bar' end
from table1;
An alternative syntax in Oracle is decode, I like this because it's nice and concise, but basically does the same as the case statement suggested above:
select decode(column1,'Y','FOO','foobar') from table1;
The syntax is as follows: decode( expression , search , result [, search , result]... [, default] ).
Further examples are in Oracles docs: http://www.techonthenet.com/oracle/functions/decode.php

<> operator in SQL

I have a table like this
ID Name IsDeleted
1 Yogesh Null
2 Goldy 1
Now when I run this query
select *
from tableName
where IsDeleted <> 1
I should get ID 1 record, But I am not getting it,
But when I run this
select *
from tableName
where IsDeleted is null
I get ID 1 record,
Why am I facing this behavior ??
Isn't NULL <> 1 is a true statement in SQL ??
IsDeleted is a bit type field with Allow Null true
select * from table
where COALESCE(IsDeleted, 0) <> 1
-- or ISNULL instead of COALESCE.
--ISNULL seems to be better in subqueries, but it's not ANSI SQL.
or
select * from table
where IsDeleted <> 1 or IsDeleted IS NULL
Comparing something with null will always result in unknown. That is why you need to use the is operator to compare null or use functions like COALESCE or isnull to replace null
select *
from tableName
where isnull(IsDeleted,0) <> 1
you compare different types. in this case its an other type (unknown) and not comparable
use the or statement to compare each type seperate
WHERE IsDeleted <> 1 OR IsDeleted is null
Learn about NULL - a comparison with NULL (in standard SQL) yields UNKNOWN, which is not true nor false (and the reason why your expectation is not met).
Try this:
PRINT CASE
WHEN 1 = NULL THEN '1 = NULL'
WHEN 1 <> NULL THEN '1 <> NULL'
ELSE '1 is neither = NULL nor <> NULL'
END
You can either first make sure that you don't have a NULL value (for instance by using the ISNULL or COALESCE functions), or use a condition with the operator IS NULL or IS NOT NULL.
Normal practice would dictate that if you had a column that essentially was a true false, yes no type of field then you should use a bit field with the default value set to 0.
So in your case above you could just run this:
select *
from tableName
where IsDeleted = 0
But in answer to your above question, if the Null is a true NULL value in the table then this will work for you:
select *
from tableName
where IsDeleted is null
or
select *
from tableName
where isnull(IsDeleted,0) = 0
to get record 1 and
select *
from tableName
where IsDeleted is not null
to get record 2
Good luck
Paul.