0

So I've been searching but I can't find the help I need anywhere.

I have this query to search what's missing in a specific list of values, but besides finding out if there's actually something missing, I need to know WHICH value is missing:

SELECT t1.*, 
FROM tabl1 t1
WHERE col1 IN ('31' , '32') 
AND NOT (valList LIKE '%;13;%' AND valList LIKE '%;19;%')
OR NOT (valList LIKE '%;15;%');

So, if the list contains ;13;15; I need to know that the value missing is 19.

Thanks in advance! RMGz

RMGz
  • 3
  • 3

2 Answers2

1

You have a very poor data format. You should not be storing lists of things (especially numbers) as strings.

But given a bad data format, you can use case to give the list:

SELECT t1.*,
       ((CASE WHEN valList NOT LIKE '%;13;%' THEN '13;' ELSE '' END) ||
        (CASE WHEN valList NOT LIKE '%;19;%' THEN '19;' ELSE '' END) ||
        (CASE WHEN valList NOT LIKE '%;15;%' THEN '15;' ELSE '' END)
       ) as MissingIds
FROM tabl1 t1
WHERE col1 IN ('31' , '32') AND
      NOT (valList LIKE '%;13;%' AND valList LIKE '%;19;%') OR
      NOT (valList LIKE '%;15;%');

You could even use a subquery so you don't have to repeat logic:

SELECT t1.*
FROM (SELECT t1.*,
             ((CASE WHEN valList NOT LIKE '%;13;%' THEN '13;' ELSE '' END) ||
              (CASE WHEN valList NOT LIKE '%;19;%' THEN '19;' ELSE '' END) ||
              (CASE WHEN valList NOT LIKE '%;15;%' THEN '15;' ELSE '' END)
             ) as MissingIds
      FROM tabl1 t1
      WHERE col1 IN ('31' , '32')
     ) t1
WHERE MIssingIds IS NOT NOT NULL
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Yes, I know the data format is terrible - but I'm working on an existing project and I'm stuck to it as is. Thanks for the help! I'll let you know if works out ;) RMGz – RMGz Jun 07 '16 at 10:59
  • As mentioned on the selected answer, I have situations in which I would have to use too many "case" statements... Thanks for the support! RMGz – RMGz Jun 07 '16 at 12:31
0

You can make use of Oracle's Collections:

Oracle Setup:

A collection to store many strings:

CREATE TYPE VARCHAR2_TABLE AS TABLE OF VARCHAR2(4000);
/

A helper function to split the list into a collection of strings:

CREATE OR REPLACE FUNCTION split_String(
  i_str    IN  VARCHAR2,
  i_delim  IN  VARCHAR2 DEFAULT ','
) RETURN VARCHAR2_TABLE DETERMINISTIC
AS
  p_result       VARCHAR2_TABLE := VARCHAR2_TABLE();
  p_start        NUMBER(5) := 1;
  p_end          NUMBER(5);
  c_len CONSTANT NUMBER(5) := LENGTH( i_str );
  c_ld  CONSTANT NUMBER(5) := LENGTH( i_delim );
BEGIN
  IF c_len > 0 THEN
    p_end := INSTR( i_str, i_delim, p_start );
    WHILE p_end > 0 LOOP
      p_result.EXTEND;
      p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, p_end - p_start );
      p_start := p_end + c_ld;
      p_end := INSTR( i_str, i_delim, p_start );
    END LOOP;
    IF p_start <= c_len + 1 THEN
      p_result.EXTEND;
      p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, c_len - p_start + 1 );
    END IF;
  END IF;
  RETURN p_result;
END;
/

Query:

SELECT *
FROM   (
  SELECT t.*,
         varchar2_table( '15', '13', '19' )
           MULTISET EXCEPT split_string( TRIM( BOTH ';' FROM valList ), ';' )
           AS missing_values
  FROM   tabl1 t
)
WHERE  missing_values IS NOT EMPTY;

You can then modify the query by passing as many (or few) values into the collection (and can even pass a collection as a bind value) and you do not have to add many CASE statements.

Community
  • 1
  • 1
MT0
  • 143,790
  • 11
  • 59
  • 117