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.
Related
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 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
I am trying to Migrate client's data and this error happened during the migration procedure. I have searched around and the possible solution is to trim or use DBMS_LOB.SUBSTR, but i do not know where to put them in. It could be this part (TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval) but i am still new to SQL and not sure what could be the cause of the problem. Can anyone guide me on how to solve this problem? Thank you!
Update
AIMSCMDDL_AT.AI_OPENNET_SVC_RPT
SET
CUST_AGREEMENT_SIGNATURE =
CASE
WHEN TO_CHAR(CUST_AGREEMENT_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, CUST_DECLARATION_AUTH_ID =
CASE
WHEN CUST_DECLARATION_AUTH_ID = null
THEN null
ELSE
CASE WHEN LENGTH(CUST_DECLARATION_AUTH_ID)<>9
THEN TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
ELSE
CASE WHEN LENGTH(CUST_DECLARATION_AUTH_ID)=9
and REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,1,1),'[A-Za-z]')
and REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,-1,1), '[A-Za-z]')
and AIMSCMDDL_AT.is_number(substr(CUST_DECLARATION_AUTH_ID,2,7)) = 1
THEN substr(CUST_DECLARATION_AUTH_ID,1,1)||LPAD(TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval),7,'0')||substr(CUST_DECLARATION_AUTH_ID,-1,1)
ELSE TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
END
END
END
, CUST_DECLARATION_AUTH_NM =
CASE
WHEN CUST_DECLARATION_AUTH_NM = null
THEN null
ELSE REGEXP_REPLACE(CUST_DECLARATION_AUTH_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, CUSTOMER_SIGNATURE =
CASE
WHEN TO_CHAR(CUSTOMER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, INSTALLER_NM =
CASE
WHEN INSTALLER_NM = null
THEN null
ELSE REGEXP_REPLACE(INSTALLER_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, INSTALLER_SIGNATURE =
CASE
WHEN TO_CHAR(INSTALLER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, REJ_CUSTOMER_ID =
CASE
WHEN REJ_CUSTOMER_ID = null
THEN null
ELSE
CASE WHEN LENGTH(REJ_CUSTOMER_ID)<>9
THEN TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
ELSE
CASE WHEN LENGTH(REJ_CUSTOMER_ID)=9
and REGEXP_LIKE(substr(REJ_CUSTOMER_ID,1,1),'[A-Za-z]')
and REGEXP_LIKE(substr(REJ_CUSTOMER_ID,-1,1), '[A-Za-z]')
and AIMSCMDDL_AT.is_number(substr(REJ_CUSTOMER_ID,2,7)) = 1
THEN substr(REJ_CUSTOMER_ID,1,1)||LPAD(TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval),7,'0')||substr(REJ_CUSTOMER_ID,-1,1)
ELSE TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
END
END
END
, REJ_CUSTOMER_NM =
CASE
WHEN REJ_CUSTOMER_NM = null
THEN null
ELSE REGEXP_REPLACE(REJ_CUSTOMER_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, REJ_CUSTOMER_SIGNATURE =
CASE
WHEN TO_CHAR(REJ_CUSTOMER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, REJ_INSTALLER_NM =
CASE
WHEN REJ_INSTALLER_NM = null
THEN null
ELSE REGEXP_REPLACE(REJ_INSTALLER_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, REJ_INSTALLER_SIGNATURE =
CASE
WHEN TO_CHAR(REJ_INSTALLER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
;
Most likely it comes from one of these expressions:
CASE
WHEN TO_CHAR(INSTALLER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
First, they don't work. ... = NULL never yields TRUE. Is IS NULL instead.
There is no need to use TO_CHAR(), IS NULL works for any data type.
And you can write it shorter as
INSTALLER_SIGNATURE = NVL2(INSTALLER_SIGNATURE, 'NULL', NULL)
NB,
REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,1,1),'[A-Za-z]')
and REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,-1,1), '[A-Za-z]')
can be written as
REGEXP_LIKE(CUST_DECLARATION_AUTH_ID,'^[A-Za-z].*[A-Za-z]$')
I have a table named VWDRSSTA and it has the following fields
SYSTEM,
EREIGNIS;
DATUM_ZEIT,
ANTRAGSNUMMER,VORGANGSNUMMER,VERS_NR_INT,
DOK_ART,
DUNKEL
I am looking to create a view of this table with a filter in two fields DOK_ART and DUNKEL using CASE statement. Here is what I tried doing
CREATE VIEW VWDRSSTA_VIEW As
SELECT SYSTEM, EREIGNIS, DATUM_ZEIT, ANTRAGSNUMMER, VORGANGSNUMMER, VERS_NR_INT,
CASE
WHEN EREIGNIS = 'EIN-ES' AND DOK_ART = 'EN' Then 'EN'
ELSE ''
END
CASE
WHEN EREIGNIS = 'POL_AN' AND DUNKEL = 1 Then 1
ELSE ''
END
FROM VWDRSSTA;
Which is going wrong. How can I achieve the above?
Try this:
CREATE OR REPLACE FROCE VIEW VWDRSSTA_VIEW As
SELECT SYSTEM, EREIGNIS, DATUM_ZEIT, ANTRAGSNUMMER, VORGANGSNUMMER, VERS_NR_INT,
CASE
WHEN EREIGNIS = 'EIN-ES' AND DOK_ART = 'EN'
THEN 'EN'
ELSE ''
END AS DOK_ART,
CASE
WHEN EREIGNIS = 'POL_AN' AND DUNKEL = 1
THEN 1
ELSE ''
END AS DUNKEL
FROM
VWDRSSTA;
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.