CASE expression - add one more condition to the WHEN part - sql

I have created a PL/SQL function where I have a case expression in a SQL query. This is working fine, but when I add another when condition it will not compile. Even if I use when ... and 2 > 1, this is also not compiling.
In the below code, the commented part is not working properly.
What I want is to add one more check in my when clause. Please advise.
create or replace function FUNCTION_NAME (date1 in varchar2,value1 in varchar2)
RETURN date
IS
date2 date;
BEGIN
SELECT D DATE2
INTO DATE2 FROM (SELECT CASE (SELECT TO_DATE(MAX(G.DATE3),'DD-MON-YYYY')
FROM TABLE1 G,
TABLE2 N
WHERE G.DATE3=N.DATE3)
WHEN LAST_DAY(TO_DATE(DATE1,'DD-MON-YYYY'))
/* AND MONTHS_BETWEEN (LAST_DAY(TO_DATE(SYSDATE)),
LAST_DAY(TO_DATE(TO_CHAR(DATE1),'DD-MON-YYYY'))) */
THEN LAST_DAY(TO_DATE(DATE1,'DD-MON-YYYY'))
ELSE
TO_DATE('31-DEC-99','DD-MON-YYYY')
END D
FROM DUAL);
RETURN DATE2;
END;

What you have is a case expression (not a case statement).
Case expressions are of two kinds: "simple" (case <expr> when val1 then ... when val2 then... etc.) and "searched" ( case when condition1 then ... when condition2 then ... etc.)
You wrote your case expression as a simple case expression. You can't, then, add conditions to the WHEN part. You must change the case expression to be "searched" all the way through.
case when (select ...) = last_day(...) AND <your commented condition> THEN .....
EDIT - copying part of a clarifying comment below my Answer.
Simple case expression:
case x when 1 then ....
Can also be written as searched case expression:
case when x = 1 then ....
These two are logically equivalent. However, if we want to add "AND 3 > 1" to the WHEN part, that works only in the searched form of the case expression.

There are two flavours of CASE.
Simple CASE:
select case dummy
when 'X' then 1
end as case_demo
from dual;
Searched CASE:
select case
when dummy = 'X' then 1
end as case_demo
from dual;
In your query you are mixing them like this, which won't work:
select case dummy
when 'X' and 1 = 1
then 1
end as case_demo
from dual;
If you switch to a "searched CASE", then you can add more when conditions:
select case
when dummy = 'X' and 1 = 1
then 1
end as case_demo
from dual;

Related

If-Then Statement In A SQL Query Insists On Trying To Convert To Wrong Type, Then Fails

I have a SQL query, linking table 1 to table 2 via an inner join, containing this part in the select part of the statement:
select
table1.field1,
table2.field1,
CASE (table2.field1)
WHEN -2 THEN ''
ELSE table2.field2
END as table2Field2,
table3.field4
from...
I want to be able to return table2Field2 when it has a relevant value, ie: when the object represented in table2 is not null, so that table2.field1 does not have a value of -2. In this case, the value of table2Field2 should be blank instead of a meaningless value.
However, this returns 0 instead of the blank text. If I change this line:
WHEN -2 THEN ''
to this:
WHEN -2 THEN 'someText'
then it complains at me that it's trying to convert an int to a string, which I'm not. table2field1 is an int, but table2Field2 is a string, which is what we're actually returning here.
How do I state (even more specifically) in this query that I'm returning the string field as a string, and not something else as a string that isn't (a) a string, and (b) the thing I specified I'm returning please?
All suggestions welcome, many thanks in advance for any help :)
In a CASE expression, all of the possible return values must be of the same data type. As written, the expression is trying to return one string and one integer.
If you want an empty string for your first output, you can CAST or CONVERT your second output to a character type value:
select
table1.field1,
table2.field1,
CASE (table2.field1)
WHEN -2 THEN ''
ELSE CAST(table2.field2 AS varchar(12)) --< 12 will cover any value of integer.
END as table2Field2,
table3.field4
from...
Is it possible you have your as table2field2 in the wrong location?
maybe try:
select
table1.field1,
table2.field1,
CASE (table2.field1)
WHEN -2 THEN ''
ELSE table2.field2
END as table2Field2,
table3.field4
from...
Because you do not want to answer me what is the database you use then I have to do it like this hehehe:
SQL Server: DEMO
select
t.col1,
CASE
WHEN convert(char,t.col1) = '-2' THEN 'aaa'
ELSE convert(char,(t.col2))
END test
from Tab1 t;
Oracle DEMO
select
t.col1,
CASE
WHEN to_char(t.col1) = '-2' THEN 'aaa'
ELSE to_char(t.col2)
END test
from tab1 t;

Is there boolean type for column in Oracle SQL?

I tried to:
select 1>2 from dual;
but got:
ORA-00923: FROM keyword not found where expected
Is there boolean type for column expression in Oracle SQL?
I able to do:
select case when 1>2 then 'T' else 'F' end from dual;
Originally I tried to compare date fields and the quickest way I found was getting difference and look to sign...
UPDATE I tried SIGN function, I don't know if it is vendor specific extension:
select SIGN(1-2) from dual;
select SIGN(DATE '2017-01-02' - DATE '2017-02-12') from dual;
but this trick doesn't work for strings...
No there is not, you can use 0 and 1 just as yes/no.
If you need to get the result 1 if something is true and 0 if it is false, you can use a case expression:
select case when (any_logical_condition_here) then 1 else 0 end as my_col
from ....
where ....
For example:
select case when 1 > 2 then 1 else 0 end as bool_result
from dual;
BOOL_RESULT
---------------------------------------
0
NOTE though - "Boolean" refers strictly to the TRUE/FALSE logic, it has no place for UNKNOWN. When you deal with null, as you must in SQL, you need three-valued logic. The case expression as written above returns 1 when the logical condition is true and 0 otherwise. Try it with 1 > null - the truth value is UNKNOWN, the case expression will return 0.

Case When Condition in Where Clause. Use filter condition if it matches the case when condition only

Is it possible to use case when condition in where clause to filter select statement.
For Eg:
Select * from table_name
where source ='UHC'
and
to_char(termdate,'YYYYMM') <= '201603';
But i want second filter condition to work only if policy number is '1'. For Eg:
case when policy_number = '1' then to_char(termdate,'YYYYMM') <= '201603';
if the policy number is not 1 then only 1st where clause should work but if policy number is 1 then both the where clause should work.
i hope i made my situation clear.
You don't need case at all:
Select * from table_name
where source ='UHC'
and ((policy_number = '1' and to_char(termdate,'YYYYMM') <= '201603')
or nvl(policy_number, '0') != '1');
With case condition will be like:
where source ='UHC' and case when policy_number = '1' then to_char(termdate,'YYYYMM') else '000000' end <= '201603');
in else you need something that is always less than '201603'. Another problem here is why you're comparing numbers as varchars? Is it really what you need?

Oracle: How can I get a value 'TRUE' or 'FALSE' comparing two NUMBERS in a query?

I want to compare two numbers. Let's take i.e. 1 and 2.
I've tried to write the following query but it simply doesn't work as expected (Toad says: ORA-00923: FROM keyword not found where expected):
SELECT 1 > 2 from dual
The DECODE is something like a Switch case, so how can I get the result of an expression evalutation (i.e. a number comparison) putting it in the select list?
I have found a solution using a functions instead of an expression in the SELECT LIST: i.e.
select DECODE(SIGN(actual - target)
, -1, 'NO Bonus for you'
, 0,'Just made it'
, 1, 'Congrats, you are a winner')
from some_table
Is there a more elegant way?
Also how do I compare two dates?
There is no boolean types in sql (at least in oracle).
you can use case:
SELECT CASE when 1 > 2 THEN 1 ELSE 0 END FROM dual
But your solution (decode) is also good, read here
The SIGN() function is indeed probably the best way of classifying (in)equality that may be of interest to you if you want to test a > b, a = b and a < b, and it will accept date-date or numeric-numeric as an argument.
I'd use a Case statement by preference, rather than a decode.
Select
case sign(actual-target)
when -1 then ...
when 0 then ...
when 1 then ...
end
SELECT (CASE
WHEN (SIGN(actual - target) > 0 ) THEN
'NO Bonus for you'
ELSE
'Just made it' END)
FROM dual
you can compare two dates with sql
METHOD (1):
SELECT TO_DATE('01/01/2012') - TO_DATE('01/01/2012')
FROM DUAL--gives zero
METHOD (2):
SELECT CASE
when MONTHS_BETWEEN('01/01/2012','01/01/2010') > 0
THEN 'FIRST IS GREATER'
ELSE 'SECOND IS GREATER OR EQUAL' END
FROM dual
sorry i cant format the code the formatting toolbar disappeared !
do any one know why?

Specify order of (T)SQL execution

I have seen similar questions asked elsewhere on this site, but more in the context of optimization.
I am having an issue with the order of execution of the conditions in a WHERE clause. I have a field which stores codes, most of which are numeric but some of which contain non-numeric characters. I need to do some operations on the numeric codes which will cause errors if attempted on non-numeric strings. I am trying to do something like
WHERE isnumeric(code) = 1
AND CAST(code AS integer) % 2 = 1
Is there any way to make sure that the isnumeric() executes first? If it doesn't, I get an error...
Thanks in advance!
The only place order of evaluation is guaranteed is CASE
WHERE
CASE WHEN isnumeric(code) = 1
THEN CAST(code AS integer) % 2
END = 1
Also just because it passes the isnumeric test doesn't guarantee that it will successfully cast to an integer.
SELECT ISNUMERIC('$') /*Returns 1*/
SELECT CAST('$' AS INTEGER) /*Fails*/
Depending upon your needs you may find these alternatives preferable.
Why not simply do it using LIKE?:
Where Code Not Like '%[^0-9]%'
Btw, either using my solution or using IsNumeric, there are some edge cases which might lead one to using a UDF such as 1,234,567 where IsNumeric will return 1 but Cast will throw an exception.
Why not use a CASE statement to say something like:
WHERE
CASE WHEN isnumeric(code) = 1
THEN CAST(code AS int) % 2 = 1
ELSE /* What ever else if not numeric */ END
You could do it in a case statement in the select clause, then limit by the value in an outer select
select * from (
select
case when isNum = 1 then CAST(code AS integer) % 2 else 0 end as castVal
from (
select
Case when isnumeric(code) = 1 then 1 else 0 end as isNum
from table) t
) t2
where castval = 1