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

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

Related

Using Case statement in Where clause in SQL developer

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.

Netezza "not exists" within a CASE statement

I have a multi-layered CASE statement, and one of the conditions needs to reference a table via a "not exists". I keep getting the error about 'correlated subqueries not allowed". How can I reference a table along with a condition inside a CASE statement? Below is a portion of my code:
WHEN ...... previous condition
WHEN ( CCOB_CLIENT_LOB_ID = 2 AND OI_CARRIER_LOB_ID IN (1,2,12,13) )
and not exists ( select S.STATE
FROM CCOB_PACIFICSOURCE.V_SELFPAY_COB_STATES S
WHERE S.STATE = SELFPAY_COB_STATE ) then 'NONE'
WHEN .... subsequent condition
The short answer is: you cann’t.
The longer answer is that you have to rewrite the query to outer join the table you give the alias S.
Then it’s quite possible to test for NULL.
Watch out for dublicates on the S.state column though :)

Combining two separate case statements in NetSuite Case Statements

I currently have two separate saved search columns for these two statements, and I'm hoping to combine them and list the data in just one column. I've tried OR AND, but the results then error out.
First Statement
Case when {item.custitem54} is null then {quantity} else null end
Second Statement
Case when {item.custitem54} = 'Yes' and {shipdate} between to_date('05/25/2020', 'MM/DD/YYYY') and to_date('12/25/2020', 'MM/DD/YYYY') then {quantity} else null end
You can have multiple WHEN in a case statement. The first matched WHEN determines the result unless it gets to the ELSE.
Case when {item.custitem54} = 'Yes'
and {shipdate} between date '2020-05-25' and date '2020-12-25' then {quantity}
when {item.custitem54} is null then {quantity}
else null
end

Using a case when result for comparison in another case when in NetSuite

I am trying to run an equation on a saved search in NetSuite that uses a formula, and a field.
I can run this and I get the correct value:
CASE WHEN {systemnotes.context}='UI' AND {systemnotes.field}='Date Closed' THEN {systemnotes.newvalue} END
And, this returns the correct value:
{startdate}
But, I can not figure out how to say that if the first case does not return a null value, and the second one is not null then give me the difference between the two. I was trying something like this (in many different variations:
CASE WHEN [(CASE WHEN {systemnotes.context}='UI' AND {systemnotes.field}='Date Closed' THEN {systemnotes.newvalue} END) IS NOT NULL AND {startdate} IS NOT NULL] THEN
{CASE WHEN {systemnotes.context}='UI' AND {systemnotes.field}='Date Closed' THEN {systemnotes.newvalue} END)-{startdate}
END
Every time I run it I get invalid expression. I've Googled around adn con not seem to find anything that discusses this type of operation, though.
The formula below worked for me. I couldn't tell what recordtype you were running against so I just added a custom date field to the case record called Test Date Field. Just replace the field name with yours and it should work.
The key is that {systemnotes.newvalue} returns text so you need to wrap that result in TO_DATE() to convert it. Then you can subtract {startdate} from it.
Your column type should be Formula(numeric).
case when
(CASE WHEN {systemnotes.context}='UI' AND {systemnotes.field}='Test Date Field' THEN {systemnotes.newvalue} END is not null and {startdate} is not null)
then (TO_DATE(CASE WHEN {systemnotes.context}='UI' AND {systemnotes.field}='Test Date Field' THEN {systemnotes.newvalue} END) - {startdate}) END

IBM Cognos prompt value when not entered

I have a problem in report studio. please help me to figure this out..
i have a optional prompt, i want to check whether the value is selected or not..
Please help how to check..
if (?parameter? is null ) then ('1') else ('2')
or
if (ParamDisplayValue('parameter') is null ) then ('1') else ('2')
Both the above are not working..
Suppose if i select any value in the prompt then the else part works and i get the result as 2, if i wont select anything then i'm not getting the result as 1
My guess, without doing extensive testing, is that an empty optional prompt doesn't exist at all and can't be compared to null. That said, I believe there's an easy fix.
Since you know that when you select an item '?parameter? is null' will return false, '?parameter? is not null' should return true and you can reverse the logic:
if (?parameter? is not null) then ('2') else ('1')
Try to put a conditional block. Set a block variable of type boolean with this expression:
ParamDisplayValue('myParam') is null
Then go to your conditional block again switch property "current block" to yes/no.
When yes (meaning that our block variable is true so the parameter is null) add a text item and just write "All".
When no add a text item with source type as report expression and write ParamDisplayValue('myParam')
P.S: there is also a way to count how many values the user selected (so as not to display all of them 1 by 1 but just show "62 values selected") but it needs some javascript and a hidden prompt field.
Use ParamCount
More details here:
http://joesanswers.blogspot.com.au/2008/09/checking-for-empty-parameters-in-cognos.html

Resources