Using Case statement in Where clause in SQL developer - switch-statement

I am trying to write a "case" statement inside a "where clause" which has an "in" statement in the "then" part. Basically, the following code is what I want, but it's not the Oracle correct syntax. Does anybody have any idea what the correct syntax should be like?
create or replace PROCEDURE "Test"
(
Type in number
)
as
begin
select Id, Name, AccCode from myTable
where case Type
when 0 then AccCode in (130,131)
when 1 then AccCode in (230,231);
end;

I don't think you want a case statement. You probably just want
where (accCode in (130,131) and type = 1)
or (accCode in (230,231) and type = 0)
If you really want to use a case statement, you could say
where (case when accCode in (130,131) then 1
when accCode in (230,231) then 0
else null
end) = type
But that will not be as readable nor will it be as easy for the optimizer to find a good execution plan.

Related

SQL query to Excel formula conversion

How to convert this SQL query to an Excel formula?
CASE
WHEN UPPER(V_out) = 'GOOGLE' OR (UPPER(V_out) = 'APPLE' AND dis > 800)
THEN 2
ELSE
CASE
WHEN UPPER(V_out) IN ('APPLE', 'MOZILLA', 'SAMSUNG') OR UPPER(V_out) IS NULL
THEN 0
ELSE -2
END
END
So far I have tried this:
=IF(AND(CU2>800;OR(UPPER(LS2)="GOOGLE";UPPER(LS2)="APPLE"));2;IF(OR(MATCH(UPPER(LS2);{"APPLE";"MOZILLA";"SAMSUNG"};0);UPPER(LS2)=0);0)-2)
And it doesn't seem to work.
Any ideas where I made a mistake?
Thanks!
The MATCH needs a ISNUMBER(...) wrapper:
ISNUMBER(MATCH(UPPER(LS2);{"APPLE";"MOZILLA";"SAMSUNG"};0))
That way it returns a TRUE/FALSE and not a number or error.
and UPPER(LS2)=0 just needs to be LS2=""
and AND(CU2>800;OR(UPPER(LS2)="GOOGLE";UPPER(LS2)="APPLE")) does not quite match the case statement:
OR(UPPER(LS2)="GOOGLE";AND(UPPER(LS2)="APPLE";CU2>800))
is more like the case statement.
The -2 is in the wrong place.
=IF(OR(UPPER(LS2)="GOOGLE";AND(UPPER(LS2)="APPLE";CU2>800));2;IF(OR(ISNUMBER(MATCH(UPPER(LS2);{"APPLE";"MOZILLA";"SAMSUNG"};0));LS2="");0;-2))

Cognos Report Studio: CASE and IF Statements

I'm very new in using Cognos report studio and trying to filter some of the values and replace them into others.
I currently have values that are coming out as blanks and want to replace them as string "Property Claims"
what i'm trying to use in my main query is
CASE WHEN [Portfolio] is null
then 'Property Claims'
ELSE [Portfolio]
which is giving me an error. Also have a different filter i want to put in to replace windscreen flags to a string value rather than a number. For example if the flag is 1 i want to place it as 'Windscreen Claims'.
if [Claim Windscreen Flag] = 1
then ('Windscreen')
Else [Claim Windscreen Flag]
None of this works with the same error....can someone give me a hand?
Your first CASE statement is missing the END. The error message should be pretty clear. But there is a simpler way to do that:
coalesce([Portfolio], 'Property Claims')
The second problem is similar: Your IF...THEN...ELSE statement is missing a bunch of parentheses. But after correcting that you may have problems with incompatible data types. You may need to cast the numbers to strings:
case
when [Claim Windscreen Flag] = 1 then ('Windscreen')
else cast([Claim Windscreen Flag], varchar(50))
end
In future, please include the error messages.
it might be syntax
IS NULL (instead of = null)
NULL is not blank. You might also want = ' '
case might need an else and END at the bottom
referring to a data type as something else can cause errors. For example a numeric like [Sales] = 'Jane Doe'
For example (assuming the result is a string and data item 2 is also a string),
case
when([data item 1] IS NULL)Then('X')
when([data item 1] = ' ')Then('X')
else([data item 2])
end
Also, if you want to show a data item as a different type, you can use CAST

Trying to write a simple computed column expression (IF Statement)

I have a column in my table called "BanquetPrize" and another column called "PrizeWinner." If the "BanquetPrize" field is NOT NULL, I'd like the "PrizeWinner" field to auto-fill with "YES," and if it IS NULL, it should be "NO." (Persisted)
I'm really new to SQL and coming from DBF tables where the expression would have been:
IF(IsNotBlank(BanquetPrize), "YES", "NO")
But I can't figure out how to write it in SQL. What I've tried:
(IIF [BanquetPrize]isnotnull then 'YES' else 'NO' end)
-- and also --
(case when [BanquetPrize]isnotnull then 'YES' else 'NO' end)
Obviously the syntax was wrong with both of these, so I was hoping someone could educate me on the correct way to write it?
In SQL You want to use IS NULL as shown in the following code
CASE [BanquetPrize]
WHEN IS NULL THEN 'NO'
ELSE 'YES'
END
You can also do something like the following to give the return column a name, in this case I am giving it a name same as column which is BanquetPrize
CASE [BanquetPrize]
WHEN IS NULL THEN 'NO'
ELSE 'YES'
END AS 'BanquetPrize'
Additional Example when using CASE in ORDER BY Clause
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);
Read more about SQL CASE statement and using IS NULL here https://www.w3schools.com/sql/sql_case.asp
You can use CASE or IIF
CASE
SELECT BanquetPrize, CASE WHEN BanquetPrize IS NOT NULL THEN 'YES' ELSE 'NO' END as PrizeWinner
FROM t1;
IIF
SELECT BanquetPrize, IIF(BanquetPrize iS NOT NULL,'YES','NO') as PrizeWinner
FROM t1;
Demo

Create new bit column based on string evaluation

I was looking into the function Contains but it can only be used in the Where predicate, what I'm looking for is something of the sorts
Select doc.* , IsSync = StringContains('Sync', doc.Url) from vw_Doc as doc
IsSync now would contain 1/0 or true/false, depending if the word Sync exists in the document Url.
Is this at all possible ?
Thank you for your time
Edit: StringContains is meant as a pseudo-function, it's not valid syntax
I would use case and charindex().
select
doc.* ,
case
when charindex('Sync', doc.Url) > 0
then 1
else 0
end as IsSync
from
vw_Doc as doc
Contains is not limited to a where clause, it returns a boolean that can be used in various contexts, e.g.:
select *,
case when Contains( doc.url, 'Sync' ) then 1 else 0 end as IsSync
from vw_Doc;

select case with variable boolean operator

This is my first question on the forum, but reading previous questions has been enormously helpful in the project I'm working on, so already my thanks. I couldn't find an answer to this, but apologies if I overlooked something.
I am writing an excel macro in vba, and am trying to create a select case... statement in which the expression has a variable boolean and numeric component. For example, the macro can pull "> 3" or "< 3" from another worksheet.
My hope had been that I could assign to a string all of these parameters, i.e.:
test1 = "is " & BoolOperator1 & " " & NumericValue1
and then
Select case ValuetoCompare
Case test1
'Do something
Case test2
'...
Is there a way to do this? I suppose the alternative would be to nest the case with the numeric variable inside a select function that determines the operator, but I thought this would be more elegant.
Thanks in advance for your guidance--
Josh
Assuming that you'll get a string BoolOperator1 that is a valid operator, e.g. >=, =, and a numeric value NumericValue1, the easiest way to execute this comparison on another numeric value ValueToCompare is to use the Evaluate function. This will execute a string as VBA and return it's result.
In your case, you could simply use:
If Evaluate(ValueToCompare&BoolOperator1&NumericValue1) Then ...
If you want to use this in a Select Case statement, you'd either need to use a simple If ... ElseIf ... statement - or use this trick:
Select Case True
Case Evaluate(ValueToCompare&BoolOperator1&NumericValue1): ...
Case Evaluate(ValueToCompare&BoolOperator2&NumericValue2): ...
Case Else ...
End Select

Resources