How to Find If a Value Exists Within a Varray

How to find if a value exists within a VARRAY

You need to expose the nested table in the FROM clause using the table() function. You can then reference attributes of the collection:

SQL> select g.title
2 from game_table g
3 , table(g.gametheme) gt
4 where gt.theme = 'Action';

TITLE
--------------------------------------------------
Star Wars

SQL>

"what if I then needed to retrieve rows with multiple Themes i.e Action, FPS?"

Apologies for the clunky solution but I need to go to work now. I may post a more elegant solution later.

SQL> select * from game_table
2 /

TITLE
--------------------------------------------------
GAMETHEME(THEME)
--------------------------------------------------------------------------------
Star Wars
THEME_TYPE(THEME_GAME('Action'), THEME_GAME('FPS'))

Uncharted 3
THEME_TYPE(THEME_GAME('Action'), THEME_GAME('Puzzle'))

Commander Cody
THEME_TYPE(THEME_GAME('Fun'), THEME_GAME('Puzzle'))

SQL> select g.title
2 from game_table g
3 , table(g.gametheme) gt
4 , table(g.gametheme) gt1
5 where gt.theme = 'Action'
6 and gt1.theme = 'FPS' ;

TITLE
--------------------------------------------------
Star Wars

SQL>

This alternative approach won't work with your current type because VARRAY does not support member of. But it would work if the collection was a Nested Table.

 select g.title
from game_table g
where 'Action' member of g.gametheme
and 'FPS' member of g.gametheme

Oracle: Fastest Way in PL/SQL to See if Value Exists: List, VARRAY, or Temp Table

Is myGroup a varray? If it is a string try something like:

select 1
from dual
where 'abc,NONE,def' like '%,NONE,%'

It is hard to follow the constraints you're working under... If at all possible, do everything inside of sql and it will be faster.

Update:

So if you're already in a plsql unit and wanted to stay in a plsql unit then the logic above would go something like this:

declare
gp varchar2(200) := 'abc,def,NONE,higlmn,op';
begin
if ','||gp||',' like '%,NONE,%' then
dbms_output.put_line('y');
else
dbms_output.put_line('n');
end if;
end;

if this itself is in a loop then, make the list once as:

declare
gp varchar2(200) := 'abc,def,NONE,higlmn,op';
gp2 varchar2(200) := ',' || gp || ',';
begin
if g2 like '%,NONE,%' then
dbms_output.put_line('y');
else
dbms_output.put_line('n');
end if;
end;

Also try instr which is probably faster than like:

  declare
gp varchar2(200) := ',abc,def,NONE,hig,';
begin
if instr(gp, ',NONE,') > 0 then
dbms_output.put_line('y');
else
dbms_output.put_line('n');
end if;
end;

I have no idea if this faster than the other solutions mentioned (it stands a good chance), it is something else to try.

How to check if a value exists in array with .includes?

Just check before push:

function checkId(e) {
if (e.target.value !== ""
&& !favs.includes(e.target.value))
{
favs.push(e.target.value);
// other code here
}
}

Check if value exists in nested array

You need to check occupation as well with Array#includes.

const occupationExists = value => users.some(user =>
user.occupation.includes(value)
);

PHP get next day name in array if current day name is not exists

One solution would be to have a second, reference array with all the days, where you could look up next days until you'll find one that is available also in $repeatdays.

One not perfect implementation would be:

<?php

function get_next_day($current)
{
$reference = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
$current_key = array_search($current, $reference);
$next_key = ($current_key + 1) % count($reference);
return $reference[$next_key];
}

function search_for_available_next_day($current, $repeatdays)
{
$next = $current;
do {
$next = get_next_day($next);
}
while (!in_array($next, $repeatdays));
return $next;
}

var_dump(search_for_available_next_day('wednesday', ['sunday', 'friday']));
var_dump(search_for_available_next_day('friday', ['sunday', 'friday']));
var_dump(search_for_available_next_day('thursday', ['sunday', 'friday']));
var_dump(search_for_available_next_day('saturday', ['sunday', 'friday']));



Related Topics



Leave a reply



Submit