I am trying to use two case statement to create a calculated column but it's not working. Do you have a solution ? Thanks!
Case
when ${selector}='china' then
Case
when [design]="XXX" then [design]
when [design]="YYY" then [design]
end
when ${selector}='japan' then
case
when [design]="AA" then [design]
when [design]="BB" then [design]
end
end
SOLUTION
replace ${selector}
bye insert it as a value with " DocumentProperty("Selector")='japan' "
Related
In Teradata I need a condition to select only records:
starting in numbers between 0 and 4
followed by string ABCD
followed by anything
I can use substring and it works. But this is not a nice piece of code.
SELECT
'4ABCDXXX' AS T
, CASE WHEN
Cast (Substring (T, 1,1) AS SMALLINT) BETWEEN 0 AND 4
AND Substring (T, 2,4) = 'ABCD'
THEN 'OK' ELSE 'NOK' END
I tried
LIKE '[0-4]ABCD%'
But this does not seem to be working...
How can this be elegantly achieved?
Thanks.
I don't think that Teradata supports the enhanced LIKE syntax which are you attempting. But, in lieu of this, we can use REGEXP_SIMILAR:
SELECT
'4ABCDXXX' AS T,
CASE WHEN REGEXP_SIMILAR('4ABCDXXX', '^[0-4]ABCD.*$', 'c')
THEN 'OK' ELSE 'NOK' END AS label
FROM yourTable;
I've never been able to make negative lookaheads work in Teradata, so I would use two tests:
select
'4ABCD123' as t,
case when
regexp_similar(t,'^[0-4]ABCD') = 1 -- starts with 0-4 followed by ABCD
and t like '%ABCD' -- does not end with ABCD
then 'nok' else 'ok' end,
I am trying this but sure I am missing a lot
declare
my_id table.ISR_ID%type;
begin
select NVL(MAX(table.ISR_ID)+1,1) into isr_id
from table;
select my_pkg.getFunction(InputToFunction=> isr_id); -- from ?
end;
If you declared MY_ID variable, you should have selected into it, not into ISR_ID (which is never declared).
Also, you should return function's result into something (probably another variable?). I've declared it as FUN_RES - see the comment within the PL/SQL anonymous block.
Saying that you are missing a lot doesn't help much; you should specify which errors you get. Anyway: try such a code, say whether it works or not and - if not - say why not (possible errors, etc.).
declare
my_id table.ISR_ID%type;
fun_res number; --> function result should be returned into this variable.
-- I don't know what it returns, so I set it to be a NUMBER.
-- Change it, if necessary.
begin
select NVL(MAX(table.ISR_ID) + 1, 1)
into my_id
from table;
fun_res := my_pkg.getFunction(my_id);
end;
[EDIT]
If you have to select function's value for every ISR_ID in a table, then you don't need PL/SQL but
select isr_id,
my_pkg.getfunction(isr_id) fun_res
from table;
If you want PL/SQL, then do it in a loop, for example:
begin
for cur_r in (select isr_id from table) loop
dbms_output.put_line(cur_r.isr_id ||', result = ' || my_pkg.getfunction(cur_r.isr_id));
end loop;
end;
/
I am aware that this has been asked so many times, but my problem doesn't seem to go away. I've already put the delimiter in the correct places but i still keep on getting error 'Error(9,1): Encountered the symbol "/" ' at line 9. If I'm not mistaken, the delimiter that causes the error shold be there.
CREATE OR REPLACE PACKAGE FOR_CLASS_NOV2 AS
PROCEDURE PRINT_SNAME(S_NO S.SNO%TYPE);
FUNCTION FIND_MAX_QTY
RETURN NUMBER;
END;
/
CREATE OR REPLACE PACKAGE BODY FOR_CLASS_NOV2 AS
PROCEDURE PRINT_SNAME(S_NO S.SNO%TYPE) IS
S_SNAME S.SNAME%TYPE;
BEGIN
SELECT SNAME
INTO S_SNAME
FROM S
WHERE SNO = S_NO;
DBMS_OUT.PUT_LINE('SUPPLIER NAME IS: ' || S_NAME);
END PRINT_SNAME;
FUNCTION FIND_MAX_QTY()
RETURN NUMBER IS
M_QTY NUMBER;
BEGIN
SELECT AX(STY)
INTO M_QTY
FROM SP;
RETURN M_QTY;
END FIND_MAX_QTY;
END;
/
I think you should not use parenthesis in the function when there's no parameters.
Try replacing this:
FUNCTION FIND_MAX_QTY()
RETURN NUMBER IS
With this:
FUNCTION FIND_MAX_QTY
RETURN NUMBER IS
Also, you have a variable named S_SNAME but you're printing S_NAME.
I am trying to use REGEXP_REPLACE in PL/SQL to replace some text with the same text in lower case. Actually, the rule is that I want all text between "()" that has only one char to be in lower case.
Here is an example :
SELECT REGEXP_REPLACE(
'i want what what is between <> in lower case : I am a test(E) (A) (HELLO)'
, '(\(\D\))', '<\1>'
) FROM DUAL
Result :
I want what is between <> in lower case : I am a test(e) (a) (HELLO)
or this because I am a little confuse about my exercice:
I want what is between <> in lower case : I am a test<(e)> <(a)> (HELLO)
How can I get my text in lower case ? I tried in several ways but I can't get out with it. I don't know hot to tell REGEXP_REPLACE to put "\1" content in lower case.
Thanks you for your help.
Best regards.
MS
(with Oracle11g) Here is how to replace the first occurrence:
Use REGEXP_instr and REGEXP_substr to be able to apply lower to the matched pattern
SELECT substr(it, 1 , REGEXP_instr( it, '(\(\D\))')-1)
||lower( REGEXP_substr(it, '(\(\D\))') )
||substr(it, REGEXP_instr( it, '(\(\D\))')+3, length(it))
FROM (SELECT 'i want what what is between <> in lower case : I am a test(E) (A) (HELLO)' it from dual) ;
and if you want the weird <> around it:
SELECT substr(it, 1 , REGEXP_instr( it, '(\(\D\))')-1)
|| '<'
||lower( REGEXP_substr(it, '(\(\D\))') )
|| '>'
||substr(it, REGEXP_instr( it, '(\(\D\))')+3, length(it))
FROM (SELECT 'i want what what is between <> in lower case : I am a test(E) (A) (HELLO)' it from dual) ;
I think you cannot have recursive regexp in Oracle. So if you want to be able to replace 2 occurrences:
SELECT substr(rit, 1 , REGEXP_instr( rit, '(\([[:upper:]]{1}\))')-1)
||lower( REGEXP_substr(rit, '(\([[:upper:]]{1}\))') )
||substr(rit, REGEXP_instr( rit, '(\([[:upper:]]{1}\))')+3, length(rit))
from (
(SELECT substr(it, 1 , REGEXP_instr( it, '(\([[:upper:]]{1}\))')-1)
||lower( REGEXP_substr(it, '(\([[:upper:]]{1}\))') )
||substr(it, REGEXP_instr( it, '(\([[:upper:]]{1}\))')+3, length(it)) rit
FROM (SELECT 'i want what what is between <> in lower case : I am a test(E) (A) (HELLO)' it from dual))
) ;
(+ I replace the \D by [[:upper:]]{1} which is more accurate)
To bad this has to be such a difficult problem! Seems like it would be easy.
To handle a variable number of occurrences of the pattern, you need to loop through the string looking for them. Maybe someone will come up with a slick solution using CONNECT BY or something but in the meantime and since you are using PL/SQL why don't you go old-school and create a function that does it? It will arguably be easier to maintain and will be wrapped up in a reusable unit available for all to use too. Pass it a string and have it return the cleaned up version.
SQL> select lower_single_letters('I want what is between in lower case : I am a test(E) (A) (HELLO)') text
from dual;
TEXT
--------------------------------------------------------------------------------
I want what is between in lower case : I am a test(e) (a) (HELLO)
SQL>
Here's some sample code since I wanted an example for use in my utility package:
CREATE OR REPLACE function lower_single_letters(string_in varchar2) return varchar2 is
tmp_string varchar2(1000) := string_in; -- Holds the string
regex_pattern constant varchar2(20) := '\([[:upper:]]\)'; -- Pattern to look for '(A)'
letter_offset integer; -- Offset of the pattern
letter varchar2(1); -- The letter to lower()
BEGIN
-- Loop while the pattern is found in the string passed in
while regexp_like(tmp_string, regex_pattern)
loop
-- Get the offset in the string
letter_offset := regexp_instr(tmp_string, regex_pattern)+1;
-- Get the letter
letter := substr(tmp_string, letter_offset, 1);
-- Replace it in the string
tmp_string := regexp_replace(tmp_string, '.', lower(letter), 1, letter_offset);
end loop;
-- Return it when the pattern is no longer found
return(tmp_string);
END lower_single_letters;
I have to execute a statement like ( i need and keyword along with when ).
select
'Is Allowed'= case A.Column
when
A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else
'No' end from TableName
I am getting syntax error,how to rewrite it without affecting the condition.
Try:
select case when A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else 'No' end as 'Is Allowed'
from TableName
SELECT
CASE A.Column
WHEN 'Is Allowed THEN 'First'
WHEN 2 THEN 'Second'
WHEN 3 THEN 'Third'
ELSE 'Other'
END
Is the general way of making a CASE (which is your question). However, your query/logic looks a bit convoluted. More detailed answer / query is possible, but would perhaps use more statements/ nested CASE.
Have a closer look at CASE (Transact-SQL)
SELECT CASE
WHEN some boolean expression
THEN value
ELSE default value
END
or
SELECT CASE value to check
WHEN vlue to check agains
THEN value
ELSE default value
END