I am using Oracle and am trying to build out some sql for the following scenario:
On EMPLOYEES table, if employee has ADDRESS3 not equal to ' ', populate this field with ADDRESS2 else, populate this field with ADDRESS1.
...
, ADDRESS_LINE2 = NVL((
SELECT (CASE t2.ADDRESS3 != ' ' THEN t2.ADDRESS2 ELSE t2.ADDRESS1 END)
FROM EMPLOYEES t2
WHERE t2.EMPLID = PS_Z_EXS_EMP_TBL_T.EMPLID
), t2.ADDRESS1)
...
but it keeps giving me an error message about missing the right parenthesis. When I comment this bit out though it runs fine. DOes anyone know what I'm doing wrong?
CASE has two variants - both needs WHEN clauses.
One variant can have complete and complex boolean expression in each WHEN clause:
CASE
WHEN t2.ADDRESS3 != ' ' THEN t2.ADDRESS2
ELSE t2.ADDRESS1
END
In the other variant each WHEN clause contain values to be tested for the CASE expression:
CASE t2.ADDRESS3
WHEN ' ' THEN t2.ADDRESS1
ELSE t2.ADDRESS2
END
The last one cannot do != so therefore "reversed" logic ;-)
You need the END keyword at the end of a case expression:
CASE t2.ADDRESS3 != ' ' THEN t2.ADDRESS2 ELSE t2.ADDRESS1 END
Related
Say I have a string looking like this ",LI,PA,LK";
I want to remove the first char, so it looks like "LI,PA,LK";
In Java my code to handle this, will look like this:
public String returnSubs(String val) {
int index = val.indexOf(",");
String res = val.substring(index+1, val.length());
return res;
}
I want to achieve the exact same thing in SQL, having this query
select patientID, case when liver is not null then 'LI' else '' end
|| case when kidney_r is not null then ',KR' else '' end
|| case when kidney_l is not null then ',KL' else ''end
|| case when heart is not null then ',HE' else '' end
|| case when liver_domino is not null then ',LI-Dom' else '' end
|| case when lung_r is not null then ',LungR' else '' end
|| case when pancreas is not null then ',PA' else '' end
|| case when liver_split is not null then ',Lsplit' else '' end
|| case when lung_l is not null then ',LungL' else '' end
|| case when intestine is not null then ',Intestine' else '' end
into organType
from offers
where patientID > 1
;
Also, the string I get from the query above, could look like LI, PA, KL, (notice the comma is at the end, and not the begining)
I see that I can use the SUBSTRING and/or INSTR of SQL. But I'm not really sure how. I am creating a procedure where this will be handled
Thanks for any help
Oracle has a function trim() that does exactly what you want:
trim(leading ',' from col)
You can use this in either an update or select.
Note: You appear to be storing multiple values in a comma-delimited list. That is a very bad way to model data. You do not want to overload what strings are by storing multiple values. Oracle has many better alternatives -- association tables, nested tables, JSON, and XML come to mind.
You could also use LTRIM here:
SELECT
LTRIM(organTypes, ',') AS col_out
FROM offers;
Some databases, such as MySQL, offer functions like CONCAT_WS which concatenate with a separator while ensuring that no dangling separators are added to the resulting output. Oracle does not have this, but LTRIM should be sufficient here.
even this will work:
substr(',LI,PA,LK',2)
In SQL SERVER:
SUBSTRING(VAL,2,LEN(VAL))
VAL--> COLUMN NAME
2--> IT SKIPS 1ST VALUE
LEN-->LENGTH OF THE COLUMN
Hi I'm trying to put on a validation on my where clause. But this validation needs only to exist when a certain condition is met else i dont need this validation.
I tried but i am not getting my expected output. Maybe I missed something? Please help
WHERE (CASE
WHEN 1 = (SELECT DECODE (PARAMETER_VALUE, 'FALSE', 1, 2)
FROM TABLE_A
WHERE PARAMETER_NAME = 'STUDENT_STATUS')
THEN
INVALID_STAT
ELSE
' '
END) = 'Y'
What about replacing the last ' ' to 'Y'. So when the query returns a result it converts to
WHERE INVALID_STAT = 'Y'
And when the query doesn't return any result it converts to -
WHERE 'Y' = 'Y'
Thus ignoring the condition.
This is my problem: I added a column Cause to my table. This column contains different conditions (up to here, everything is fine). But since I have a lot of lines for each product, it can have 3 conditions at the same time.
What I'm trying to do is that once it finds a condition, it does not go to the one after (and it is by this order of priority).
I do not know if I was clear, but if you want more explanation do not hesitate to ask me questions
Cause = (CASE
WHEN Four IS NOT NULL THEN 'Retards'
WHEN (MAX(DateP BETWEEN '2018-10-24' AND '2018-10-14') THEN 'stock'
WHEN Reference = 0 THEN 'respecté'
WHEN Produit = 2 THEN 'non respecté'
ELSE 'Erreur'
END)
This is an example of what I want to do:
The CASE expression stops just after the first WHEN..THEN is found. If you want to concatenate labels and check all conditions, you can use multiple CASE expression.
(case when Four IS NOT NULL THEN 'Retards' ELSE '' END +
case when (MAX(DateP) between '2018-10-24' AND '2018-10-14') THEN 'stock' ELSE '' END +
case when Reference = 0 THEN 'respecté' ELSE '' END +
case when Produit = 2 THEN 'non respecté' ELSE '' END
) AS Cause
I am sorry if this duplicate question. Please let me know where I can find right question. I have a stored procedure where I can just make few minor modifications and I cannot change/update data in my Student table. We have this problem and I need to fix it.
In below statement, sometimes student.FullName will have [NEXTLINE] in it then I need to replace it with '' ( empty string) else nothing return as it is. I tried various ways but getting error when I replace +student.FullName in THEN clause. Please let me know how can I do this.
CASE
WHEN student.ID IS NULL THEN
CASE
WHEN student.Status = 0 THEN '<BOLDSTART>rejected<BOLDEND>.'+'[LINEBREAK]'+ student.FullName
WHEN student.Status = 1 THEN ' <BOLDSTART>accepted<BOLDEND>.'
END
END
I want to add similar logic like below in above +student.FullName
IF (student.FullName LIKE '%[NEXTLINE]%')
BEGIN
SELECT REPLACE (student.FullName,'[NEXTLINE]','')
END
ELSE
SELECT student.FullName
Thanks in advance
UPDATE
Thanks to D Stanley. I solved my problem like below
CASE
WHEN student.ID IS NULL THEN
WHEN student.Status = 0 THEN '<BOLDSTART>rejected<BOLDEND>.' + '[LINEBREAK]' + REPLACE (student.FullName,'[NEXTLINE]','')
WHEN student.Status = 1 THEN ' <BOLDSTART>accepted<BOLDEND>.'
END
You can just wrap your entire logic in a replace() call:
REPLACE((CASE WHEN student.ID IS NOT NULL THEN NULL
WHEN student.Status = 0
THEN '<BOLDSTART>rejected<BOLDEND>.' + '[LINEBREAK]' + student.FullName
WHEN student.Status = 1
THEN ' <BOLDSTART>accepted<BOLDEND>.'
END), '[NEXTLINE]', ''
)
Notice that I also simplified your logic. The nested case statements are not necessary. case is evaluated sequentially, so you can just check for the conditions you want.
Is this what you want?
replace
+ student.FullName
for
+ CASE
WHEN student.FullName LIKE '%[NEXTLINE]%' THEN REPLACE (student.FullName,'[NEXTLINE]','')
ELSE student.FullName
END
You don't need to see if it contains the string. If it does not, REPLACE will just return the original string:
CASE
WHEN student.ID IS NULL THEN
CASE
WHEN student.Status = 0 THEN
'<BOLDSTART>rejected<BOLDEND>.'+'[LINEBREAK]'+ REPLACE(student.FullName,'[NEXTLINE]','')
WHEN student.Status = 1 THEN
' <BOLDSTART>accepted<BOLDEND>.'
END
END
SQL Syntax is still something I am learning. I am getting the error noted below the this snippet of code.
SELECT
CASE WHEN LTRIM(RTRIM(cLehmanNo)) =' ' THEN NULL
WHEN cLehmanNo IS NOT NULL THEN REPLACE ( cLehmanNo,SUBSTRING (cLehmanNo,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cLehmanNo),1), ' ' )
END asLOAN_NUMBER
,CASE WHEN LTRIM(RTRIM(cMERS)) =' ' THEN NULL
WHEN cMERS IS NOT NULL THEN REPLACE ( cMERS,SUBSTRING (cMERS,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cMERS),1), ' ' )
END asMERS_ID
and 100+ more of same.
Msg 8133, Level 16, State 1, Line 1
None of the result expressions in a CASE specification can be NULL.
What am I doing wrong? How do I keep the gist of the statement and not get this crazy error?
This happens when it can't infer the type.
e.g.
SELECT CASE WHEN 1 = 2 THEN NULL ELSE NULL END
But this works
SELECT CASE WHEN 1 = 2 THEN NULL ELSE replace(NULL,'','') END
so I doubt the error is from the code you have shown us (You are using string functions and the following quick test shows that it will assume that to be varchar(8000))
SELECT CASE WHEN 1 = 2 THEN NULL ELSE REPLACE(NULL,'','') END a
INTO t /*Creates column of datatype varchar(8000)*/
You need to convert NULL to a correct type matching the overall values, e.g. CONVERT(VARCHAR(10), NULL), otherwise the server can't deduce which type to make the resulting value.
The error message actually means that all results in one of your case expressions are null. You have an expression like:
case when something then null when something then null end
At least one of the results has to be something other than null. You could circumvent this, but most likely there is a mistake in the query, as a case exression that always returns the same result is pointless.
The error message has been changed to:
At least one of the result expressions
in a CASE specification must be an
expression other than the NULL
constant.
SELECT
CASE WHEN LTRIM(RTRIM(cLehmanNo)) =' ' THEN NULL
WHEN cLehmanNo IS NOT NULL THEN REPLACE ( cLehmanNo,SUBSTRING (cLehmanNo,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cLehmanNo),1), ' ' )
ELSE ''
END asLOAN_NUMBER
,CASE WHEN LTRIM(RTRIM(cMERS)) =' ' THEN NULL
WHEN cMERS IS NOT NULL THEN REPLACE ( cMERS,SUBSTRING (cMERS,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cMERS),1), ' ' )
ELSE ''
END asMERS_ID