Using prompts in Select Case statements - sql

I want to use select case for my prompts. Condition is that when the prompt :4 = 'I' then prompt :5 equals all of its values, and I use the code below but i receive the following error.
Error in running query because of SQL Error, Code=936, Message=ORA-00936: missing expression (50,380)
2 = 2 AND (CASE :5
WHEN :4 = 'I' AND :5 = ' '
THEN :5 = '%'
END)
Anything wrong with my case statemnt?

There are two different syntaxes for CASE:
CASE value1
WHEN value2 THEN expression1
WHEN value2 THEN expression2
ELSE expression3
END
and:
CASE
WHEN boolean_expression THEN expression1
WHEN boolean_expression THEN expression2
ELSE expression3
END
Note: The first statement can be converted to the second as
CASE
WHEN value1 = value2 THEN expression1
WHEN value1 = value3 THEN expression2
ELSE expression3
END
You are mixing these two syntaxes and it is invalid SQL.
You appear to want:
:5 = CASE WHEN :4 = 'I' AND :5 = ' ' THEN '%' END
However, even that will not work as bind variables are set once and are not re-evaluated so your logic would be:
( ( :4 = 'I' AND :5 = ' ' ) AND :5 = '%' )
OR ( NOT ( :4 = 'I' AND :5 = ' ' ) AND :5 = NULL )
Since :5 cannot ever be both ' ' and '%' then that branch of the logic can never be true and anything (including NULL) is never equal to NULL so the second branch of the logic is also never true; therefore your expression will never match anything.

Just create an expression like this
decode(:4,'I',:5,SOMEVALUEORFIELD) = :5

This is your case expression:
(CASE :5 WHEN :4 = 'I' AND :5 = ' ' THEN :5 = '%'
END)
I cannot figure out what you really want. Both the CASE :5 and the :5 = '%' are improper. Perhaps:
(CASE WHEN :4 = 'I' AND :5 = ' ' THEN '%'
END) as :5
However, you don't normally use parameters for column aliases.

Related

How do I use concat and case switch together in PostgresSQL?

I am trying to use concat as function and inside the same function use case switch statements in PostgresSQL. The end goal is populating a table with a bunch of statements that change according to the case.
Does anybody know how to solve this?
I tried to use '+' and ',' but I always get the same Syntax error
(SELECT concat(cast(idpersonale as varchar(5)),' = '
,case when dstitolo is not null then dstitolo else '' end + ' '
,case when dsnome is not null then dsnome else '' end + ' '
,case when dscognome is not null then dscognome else '' end
dscognome)
FROM global.glb_personale
WHERE cch.glb_personale.idpersonale =inter.interventoeseguitoda) as InterventoEseguitoDa
FROM cch.pats_cch_interventi inter
WHERE (idtiporecord is null or not idtiporecord in('uti','int')) and
NOT idintervento IN (SELECT idint
FROM cch.glo_error_data_2
WHERE (iddb = 'cch') AND (cnerror = 1))
order by idintervento;
Concatenation works with || operator or concat(text1, text2, text3, ...) function. Every text literal can be also an expression which gives a type text, like a CASE clause:
demo:db<>fiddle
SELECT
'you' || 'can' || 'concat' || 'like' || 'that' AS concat1,
concat('or', 'like', 'that') AS concat2,
concat('Mix' || 'both', 'up') AS concat3,
concat(
'And',
CASE WHEN false THEN 'now' ELSE 'doint' END || 'this',
CASE
WHEN true THEN 'with' || 'some'
WHEN false THEN 'CASE'
ELSE 'clauses'
END
) AS concat4
That means in your case: Change concat(text1 + text2 + text3) to concat(text1, text2, text3) (because the texts are function arguments) or to text1 || text2 || text3
since you are using concat(), replace + to , instead.
(SELECT concat(cast(idpersonale as varchar(5)),' = '
,case when dstitolo is not null then dstitolo else '' end, ' '
,case when dsnome is not null then dsnome else '' end, ' '
,case when dscognome is not null then dscognome else '' end
dscognome)
FROM global.glb_personale
WHERE cch.glb_personale.idpersonale =inter.interventoeseguitoda) as InterventoEseguitoDa
FROM cch.pats_cch_interventi inter
WHERE (idtiporecord is null or not idtiporecord in('uti','int')) and
NOT idintervento IN (SELECT idint
FROM cch.glo_error_data_2
WHERE (iddb = 'cch') AND (cnerror = 1))
While the concatenation operator (||) and the function (concat) combine strings there is a big difference between them: how they handle nulls.
If any operand is null then the concatenation operator result is NULL.
If any operand is null then the concatenation function moves to the next operand, leaving previous and subsequent results as they would had the null operand not been present. Example:
with dscognome (idpersonale,dstitolo ,dsnome ,dscognome) as
( values (1,'a','b','c')
, (2,'d','e',null)
, (3,'f',null,'g')
, (4,null, 'h', 'i')
, (5,null, null, null)
)
select idpersonale
, dstitolo || dsnome || dscognome "using || opreator"
, concat(dstitolo,dsnome,dscognome) "using concat function"
from dscognome;
Since the entire purpose of the case structure is to handle nulls, using the concatenation function entirely negates it's requirement and the query can be written:
with dscognome (idpersonale,dstitolo ,dsnome ,dscognome) as
( values (1,'a','b','c')
, (2,'d','e',null)
, (3,'f',null,'g')
, (4,null, 'h', 'i')
, (5,null, null, null)
)
SELECT concat(cast(idpersonale as varchar),' = '
, dstitolo , ' '
, dsnome, ' '
, dscognome )
from dscognome;

I want to concatenate these case statements

I'm getting multiple results from these statements and want to concatenate these. Please help with syntax and structure
(SELECT NVL (
CASE WHEN (csi.hello = DT.S)
THEN 'Task IS ALREADY DONE'
WHEN VV.Mo LIKE NULL
THEN 'The model is not available'
WHEN (B.tsk = '7')
THEN 'The task status has been Cancelled'
WHEN (B.tski = '14')
THEN 'The task has been Assigned'
WHEN (B.tsko = '11001')
THEN 'The task has been Return Part STATUS'
END, '')RMA_CRITERIA,
Use string concatenation:
SELECT (CASE WHEN csi.hello = DT.S THEN 'Task IS ALREADY DONE;' ELSE '' END ||
CASE WHEN VV.Mo IS NULL THEN 'The model is not available;' ELSE '' END ||
CASE WHEN B.tsk = '7' THEN 'The task status has been Cancelled;' ELSE '' END ||
CASE WHEN B.tski = '14' THEN 'The task has been Assigned;' ELSE '' END ||
CASE WHEN B.tsko = '11001' THEN 'The task has been Return Part STATUS;' ELSE '' END
) as RMA_CRITERIA,
Note that this adds a separator (;), although you do not specify one. Also LIKE NULL always returns the equivalent of false; I assume you intend IS NULL. And the ELSE '' is redundant in Oracle, but it makes the intention clear.

MSSQL Case denoted by an integer returns a string

I have an issue with my SELECT statement in SSRS.
I want to use an integer to return strings values.
I tried it with this SELECT clause:
SELECT CASE #param = '1' THEN value like '__%' ELSE value like ' '
But it doesn't work, so I tried to use this one instead:
WHERE
((#param = '1' AND value like '__%') OR (#param = '0' AND value = '%'))
The expected result is: When the case is "1" SELECT should return only values which are not ' '
When the case is "0" SELECT should return all values = ' ' + '__%'
Thank you for your help
Your case condition is wrong formatted. You missed WHEN and END .It should be like that:
SELECT CASE WHEN #param = '1' THEN value like '__%' ELSE value like ' ' END
EDIT : As much as I understand you want to see Value if the #param='1' and if not the result will be '__%'. If yes this following CASE usage should be correct:
SELECT CASE WHEN #param = '1' THEN value ELSE value= '__%' END
I fix it my self condition is
((#column = '1' AND column like '__%') OR (#column = '0'AND column like '_%'))
WHERE
((#param = '1' AND value != '') OR (#param = '0'))
just try this

Nested CASE statement in UPDATE SET clause

I am attempting to switch a value based on a set of conditions and I have noticed that where I have a nested CASE statement within the SET clause of my UPDATE expression, the columns are not updating.
When a simple CASE expression existis the columns appear to be updating. However, for the OVERRIDDEN_CHECK_NUMBER and OVERRIDDEN_AMOUNT in this example, the columns are not updating.
The OVERRIDDEN_DATE, OVERRIDDEN_USER_ID, CHECK_NO and AMOUNT columns in the UPDATE are updating without issue.
Can anyone tell me why the OVERRIDDEN_CHECK_NUMBER and OVERRIDDEN_AMOUNT will not update in this UPDATE statement?
Are nested CASE statements not tallowed in the SET clause of the UPDATE expression?
SQL EXAMPLE
UPDATE WAREHOUSE.BANK_STATEMENT_ACTIVITY
SET OVERRIDDEN_CHECK_NO = --(CASE :btn
(CASE WHEN (:btn = '1') THEN CASE WHEN '141973' = '141973' THEN NULL
WHEN '141973' != '999999' THEN LPAD(TRIM(141973), 12, '0')
END
WHEN (:btn = '2') THEN '1740 - Previously Paid Warrant'
ELSE NULL
END),
OVERRIDDEN_AMOUNT = --(CASE :btn
(CASE WHEN (:btn = '1') THEN CASE WHEN 253.20 = 253.20 THEN NULL
WHEN 253.20 != 999.99 THEN LPAD(TRIM(253.20), 12, '0')
END
WHEN (:btn = '2') THEN NULL
ELSE NULL
END),
OVERRIDDEN_DATE = SYSDATE,
OVERRIDDEN_USER_ID = 1009,
CHECK_NO = (CASE :btn
WHEN '1' THEN LPAD(TRIM(999999), 12, '0')
WHEN '2' THEN '142775'
ELSE NULL
END),
AMOUNT = (CASE :btn
WHEN '1' THEN TRIM(78.60)
WHEN '2' THEN TRIM(253.20)
ELSE NULL
END)
WHERE TO_NUMBER(CHECK_NO) = (CASE :btn
WHEN '1' THEN '141973'
WHEN '2' THEN '142775'
ELSE NULL
END)
AND AMOUNT = (CASE :btn
WHEN '1' THEN 78.60
WHEN '2' THEN 253.20
ELSE NULL
END)
AND TRUNC(LOAD_DATE) = (CASE :btn
WHEN '1' THEN TRUNC(LOAD_DATE)
WHEN '2' THEN (SELECT (MAX(LOAD_DATE)) FROM WAREHOUSE.BANK_STATEMENT_ACTIVITY)
ELSE NULL
END)
AND BANKACCTNO = (SELECT LONGDESC
FROM TCMS.COMPLEMENTARY_VALIDATIONS
WHERE TEXTCODE = 'BANKACCT'
AND CODE = 80);
EDIT
After fixing the logival issue that #mathguy pointed out the update query via the editor worked. However, when executing this via the PL/SQL pacakge the table update fails.
Here is the before update:
Before - result
Her is the after result:
After - result
They are identical.
This is the actual package procedure:
PROCEDURE UpdateBankStatementActivity( btn IN VARCHAR2)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE( 'UpdateBankStatementActivity - btn: ' || btn );
DBMS_OUTPUT.PUT_LINE( 'UpdateBankStatementActivity - bsa_rec.OVERRIDDEN_CHECK_NO: ' || bsa_rec.OVERRIDDEN_CHECK_NO || CHR(10) ||
'bsa_rec.OVERRIDDEN_AMOUNT: ' || bsa_rec.OVERRIDDEN_AMOUNT || CHR(10) ||
'bsa_rec.CHECK_NO: ' || bsa_rec.CHECK_NO || CHR(10) ||
'bsa_rec.AMOUNT: ' || bsa_rec.AMOUNT || CHR(10) ||
'bsa_rec.LOAD_DATE:' || bsa_rec.LOAD_DATE );
UPDATE WAREHOUSE.BANK_STATEMENT_ACTIVITY
SET OVERRIDDEN_CHECK_NO = --(CASE :btn
(CASE WHEN (btn = '1') THEN CASE WHEN bsa_rec.OVERRIDDEN_CHECK_NO = bsa_rec.CHECK_NO THEN NULL
WHEN bsa_rec.OVERRIDDEN_CHECK_NO != bsa_rec.CHECK_NO THEN LPAD(TRIM(bsa_rec.OVERRIDDEN_CHECK_NO), 12, '0')
END
WHEN (btn = '2') THEN '1740 - Previously Paid Warrant'
ELSE NULL
END),
OVERRIDDEN_AMOUNT = --(CASE :btn
(CASE WHEN (btn = '1') THEN CASE WHEN bsa_rec.OVERRIDDEN_AMOUNT = bsa_rec.AMOUNT THEN NULL
WHEN bsa_rec.OVERRIDDEN_AMOUNT != bsa_rec.AMOUNT THEN LPAD(TRIM(bsa_rec.OVERRIDDEN_AMOUNT), 12, '0')
END
WHEN (btn = '2') THEN NULL
ELSE NULL
END),
OVERRIDDEN_DATE = SYSDATE,
OVERRIDDEN_USER_ID = bsa_rec.OVERRIDDEN_USER_ID,
CHECK_NO = (CASE btn
WHEN '1' THEN LPAD(TRIM(bsa_rec.CHECK_NO), 12, '0')
WHEN '2' THEN LPAD(TRIM(bsa_rec.CHECK_NO), 12, '0')
ELSE NULL
END),
AMOUNT = (CASE btn
WHEN '1' THEN TRIM(bsa_rec.OVERRIDDEN_AMOUNT)
WHEN '2' THEN TRIM(bsa_rec.AMOUNT)
ELSE NULL
END)
WHERE TO_NUMBER(CHECK_NO) = (CASE btn
WHEN '1' THEN bsa_rec.OVERRIDDEN_CHECK_NO
WHEN '2' THEN bsa_rec.CHECK_NO
ELSE NULL
END)
AND AMOUNT = (CASE btn
WHEN '1' THEN bsa_rec.OVERRIDDEN_AMOUNT
WHEN '2' THEN bsa_rec.AMOUNT
ELSE NULL
END)
AND TRUNC(LOAD_DATE) = (CASE btn
WHEN '1' THEN TRUNC(bsa_rec.LOAD_DATE)
WHEN '2' THEN (SELECT MAX(LOAD_DATE) FROM WAREHOUSE.BANK_STATEMENT_ACTIVITY)
ELSE NULL
END)
AND BANKACCTNO = (SELECT LONGDESC
FROM TCMS.COMPLEMENTARY_VALIDATIONS
WHERE TEXTCODE = 'BANKACCT'
AND CODE = 80);
COMMIT;
END UpdateBankStatementActivity;
The parameter values in the first SQL CODE snippet are consistent with the parameters in the PL/SQL block.
What do you mean by "not updating"?
There is a clear logical flaw in your nested case expressions, when :btn = '1'. Namely, the first branch of the "inner" case expression always evaluates to TRUE, so the "update value" will be null every time you pass in :btn = '1'. Is that the problem you are noticing? Then: For overridden_amount the result will always be null because of this; for overridden_check_no it will not be null if and only if :btn = '2'.
Note that you could write the "outer" case expressions with the same syntax you use for the simple expressions: case :btn when '1' then case .... end else ... end.
EDIT: Answering your modified question (perhaps). I am not sure I can follow the logic in its entirety; but what is happening in the example (assuming you called the procedure with btn = '1' is this:
Both the overridden_check_no and the overridden_amount are null in the original table. In the nested (inner) case expression for both columns you check two columns, with either = or !=. Neither of the tests is true when one of the terms is null! So the case evaluations falls through to the else clause, or when else is not included, to the default, which is null. The update actually did work, it just updated the values to null because none of the "actual branches" in the inner case statements evaluated to true.
You say, though, that this works OK if you just run it as a stand-alone SQL update, directly from your editor. I wonder how that is possible, unless - again - the code in the procedure is different from what you have in your editor.

Syntax Errors With Case Statement in SQL

If the When conditions are correct, then I want it to be labeled as 'Suspended for Audit....' if they are not correct, then have it be either blank or filled in by the t.fstrTaskSource + 'TYP' + t.fstrType statement (this part works already)
SELECT t.flngKey AS flngTaskKey,
t.fstrAccountType,
t.fstrTaskSource,
CASE t.fstrCategory
WHEN '' THEN ''
ELSE t.fstrTaskSource + '_CAT_' + t.fstrCategory
END AS fstrCategory,
CASE t.fstrType
WHEN '' THEN ''
WHEN (wd.fstrWorkType = 'SUSIN1' -- I am getting a syntax error here on the = sign --
AND wd.fstrOwner = ' '
AND wd.flngworkkey = wr.flngworkkey
AND wr.fstrAccountType <> '007'
AND wr.fblnOpen = 1
AND EXISTS
(SELECT 1
FROM tblIndicator i
WHERE i.fstrIndicator = 'EIWTCH'
AND i.flngVer = 0
AND i.flngAccountKey = wd.flngAccountKey)) -- I am also getting an error here on the ) sign --
THEN 'Suspended for Audit Indicator - EIC Watch For'
ELSE t.fstrTaskSource + '_TYP_' + t.fstrType
END AS fstrType
Your second Case Expression is a mix of Simple Case and Searched Case.
I.e.
CASE t.fstrType
WHEN '' THEN ''
WHEN (wd.fstrWorkType = 'SUSIN1'
Change it to a Searched Case expression as:
CASE WHEN t.fstrType = '' THEN ''
WHEN (wd.fstrWorkType = 'SUSIN1' ...
Two formats of Case Expression are:
--Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
--Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Your are trying to use the two syntax of CASE in the same time
the first:
case expression
when "val1" then ..
when "val2" then ..
end
the second:
case
when column = "val1" then ..
when column2 = "val2" then ..
end
So, use this in your second CASE:
CASE
WHEN t.fstrType = '' THEN ''
WHEN (wd.fstrWorkType = 'SUSIN1' ...