Column is null using Case When SQL - 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'))

Related

If value return the value. If a record not exists or when column is null, return 0 in Sql Server - different ways

I want to return 0 if there is no record or if the Column1 is null.
select #var = Column1
from myschema.mytable
where Id = #suppliedId;
select isnull(#var, 0);
The above code outputs 0 if if Column1 is null. Or if a row is not found
Whereas I tried to save some keystrokes but it resulted in,
select isnull(Column1, 0)
from myschema.mytable
where Id = #suppliedId;
The above code outputs null if Column1 is null or when there is no row
Any ideas what is wrong here ? Or is there any shorter way of writing the first code ?
You can do
SELECT #var = ISNULL(MAX(Column1), 0)
FROM myschema.mytable
WHERE Id = #suppliedId;
A scalar aggregate always returns a single row even if the underlying query returns zero rows.
Not really saving key strokes, but something like this could help :-)
SELECT TOP 1 tbl.field
FROM
(
SELECT 0 AS inx, 'no record' AS field
--if only one row is possible, than set '1' literally
UNION SELECT ROW_NUMBER() OVER(ORDER BY mytable.orderfield), ISNULL(mytable.Land,'is null')
FROM mytable
WHERE IDENTITY = #suppliedID
) AS tbl
ORDER BY tbl.inx DESC

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

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

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 =

Alternative option for case condtion in sql

How to check the column value zero or not
I want to insert 50 column values, each column i want to check whethere the value is 0 or not. If the value is 0 then it should be null
Query
insert into table1 values (Case when column1 = '0' then null else column1 end, .....
Case when column50 = '0' then null else column50 end)
The above query is working, but query length is too long because i am using the above query of 50 column's
there is any alternative option is there for check the column value is 0 or not.
like this if(column1, 0) then null
Need Query Help
Try using NULLIF:
insert into table1 values (NULLIF(column1, '0'), .....
NULLIF(column50, '0'))

Sql statement for mutually exclusive events

I am trying to run an sql statement on an iSeries that will output retuls based on a type parameter I pass in.
Just say mytable has a field called field1. field1 contains Y,N and NULL values.
A type of 'Y' should return just 'Y' values.
A type of 'N' should return not 'Y' values. (ie. Null, N and any other junk in the field)
I tried this...
select *
from mytable
where field1 in case when :type = 'Y' then 'Y'
else (select field1 from mytable where field1 <> 'Y') end
However, this does not work.
I believe the logic you are looking for is this:
SELECT *
FROM myTable
WHERE (:type = 'Y' AND field1 IS NOT null AND field1 = 'Y')
OR (:type <> 'Y' AND (field1 IS null OR field1 <> 'Y'))
(keep in mind the fact that short-circuit logic is not garuanteed with SQL...)
Remember that null doesn't really compare to anything, and it's best to call out the fact that you actually want it (in the second case).