Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a string is inside a list of predefined strings?

Tags:

oracle

plsql

I defined a list of strings, which contains different country codes (like USA,CHINA,HK,JPN, etc.).

How can I check, if an input variable is the country code in the list?

I use the following code to test, but it fails.

declare
 country_list  CONSTANT VARCHAR2(200) := USA,CHINA,HK,JPN;
 input VARCHAR2(200);
begin
 input  := 'JPN';
 IF input   IN  (country_list)
         DBMS_OUTPUT.PUT_LINE('It is Inside');
    else       
         DBMS_OUTPUT.PUT_LINE('It is not  Inside');
 END IF;
end;
like image 959
drupalspring Avatar asked Apr 30 '10 03:04

drupalspring


People also ask

How to check if a string is present in a list of strings?

Python Find String in List using count() We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1.

How do you check if a column has alphabets in Oracle?

Answer: To test a string for alphabetic characters, you could use a combination of the LENGTH function, TRIM function, and TRANSLATE function built into Oracle. This function will return a null value if string1 is alphabetic. It will return a value "greater than 0" if string1 contains any non-alphabetic characters.


1 Answers

If you can guarantee that the input will not include the delimiter, you can do this:

country_list := 'USA,CHINA,HK,JPN';

input := 'JPN'; -- will be found
IF INSTR(',' || country_list || ','
        ,',' || input || ',') > 0 THEN
   --found
ELSE
   --not found
END IF;

input := 'HINA'; --will not be found
IF INSTR(',' || country_list || ','
        ,',' || input || ',') > 0 THEN
   --found
ELSE
   --not found
END IF;
like image 82
Jeffrey Kemp Avatar answered Sep 21 '22 12:09

Jeffrey Kemp