Check if values in an array exist in another array using sql - sql

I want to perform some operations based on, if we have some values in one array existing in another array. Following is part of my code where I am trying to handle the arrays. I have 2 arrays CL_NM[ ] and CL_NM_FIN[ ]. I would want to perform an update if a value in CL_NM[ ] doesn't exist in CL_NM_FIN[ ]. Please help me on how should I modify my code.
Upon trying to do this, I am getting that the column already exists error because the for loop is not going through all elements in CL_NM_FIN[ ] before going into the else condition.

I was able to solve this using the following:
DO
BEGIN
DECLARE arr1 TINYINT ARRAY = ARRAY(1,2,3,4);
DECLARE arr2 INTEGER ARRAY = ARRAY(1,2);
--convert array in table rst1 with column name col1
rst1 = UNNEST(:arr1) as (COL1);
--convert array in table rst2 with column name col1
rst2 = UNNEST(:arr2) as (COL1);
--query with minus
select col1 from :rst1
minus
select col1 from :rst2;
END;

Related

SQL / DB2 Array in Where declared in with

anyone does know a workaround for querying with parameters/variables for usage in where [...] in functions on db2 v11?
what i tried:
DECLARE #list varchar(23) = '1,2,3,4'
SELECT ...FROM tbl WHERE col IN (#list)
WITH test(val) AS (VALUES(ARRAY['5','9']))
SELECT ... FROM table, test WHERE col ANY(val)
both do not work, first one isn't db2 compatible, the second ones does not work cause he cant split the values.
any ideas or examples?
Try this:
SELECT t.*
FROM tbl t
WHERE EXISTS
(
select 1
from xmltable
(
'for $id in tokenize($s, ",") return <i>{string($id)}</i>'
passing '1,2,3,4' as "s"
columns
tok int path '.'
) v
where v.tok = t.col
);
You may use a parameter marker instead of the string constant 1,2,3,4 as for usual string paramter, if you want to provide such a list of integers as a comma separated string at runtime.
Declare local temporary table and insert your value list in that ?

Using IF statement in PostgreSQL to update a column that can have only 2 values

I have a table in PostgreSQL which has a column names "value" that can accept two string values (lets say "one" and "two")
I would like to create a query that updates a row and if value column is currently "one" it updates it to be "two" and vice-versa
I've tried to the the following query:
IF EXISTS(SELECT * FROM table_name WHERE filename='file1' and value='one')
THEN UPDATE table_name SET value='two' WHERE filename='file1'
ELSE
UPDATE table_name SET value='one' WHERE filename='file1'
end if;
But I keep getting "syntax error at or near "IF""
Any help please?
You wouldn't use if for this. You would use case:
update table_name
set value = (case when value = 'one' then 'two' else 'one' end);
Or, if you choose to represent the binary value as a boolean, this is simpler with not:
update table_name
set value = not value;

Comparing 2 strings and find out mismatches

I need to create a query in MariaDB 10.2. I am trying to create a query with string functions as now
Below is the scenario
One string has placeholders and another string has values of those placeholders. I want to get a tabular output in which one column have placeholders and another column have placeholder's values
value1 -->
jdbc:postgresql://$PGHOST_1$:$PGPORT_INSTANCE_1$/eventstore
value2 -->
jdbc:postgresql://1.2.3.4:5432/eventstore
now i want to get below data
Thanks in advance.
This needs a looping mechanism. Therefore, use a stored procedure or client code to do the looping:
SELECT placeholder, `value` FROM mylist;
`col` starts out looking like `value1`
foreach row in the list of replacements:
UPDATE mytable SET col =
REPLACE(col, placeholder, `value`);
Now `col` looks like your `value2`
or, if you don't want to change col:
SELECT placeholder, `value` FROM mylist;
SELECT #col := col FROM mytable;
foreach row in the list of replacements:
SET #col =
REPLACE(#col, placeholder, `value`);
then use #col in subsequent work
(value is a keyword, hence the backtics.)
From there, build the "mismatches".

Replace table column value using part of same column

I am updating a column in my table, replacing a column with part of its existing value:
Update Table_Name Set VarName '12345' where VarName = 'AA-BB-12345';
Update Table_Name Set VarName '99999' where VarName = 'XX-AR-99999';
...
How to do this in a loop so it makes changes through entire Table_Name table pulling out the last element of VarName and using that to update? There are 500 rows to be updated in the table, too many to do single update lines, so I need a loop to accomplish this.
I think this is close:
Update Table_Name
set VarName = ( select regexp_substr(VarName, '[^-]+,1,3 ) from Table_Name );
You aren't that far off, but your subquery isn't correlated - there is no link between the value(s) the subquery finds and the row being updated, so you're probably getting an ORA-01427 error. But you don't need the subquery since you're updating the same row:
update table_name set varname = regexp_substr(varname, '[^-]+', 1, 3);
You can do it without a regular expression too:
update table_name set varname = substr(varname, instr(varname, '-', -1) + 1);
which also has the advantage of not nulling any rows which aren't the expected pattern. Even so, you could reduce unnecessary work by only updating values with dashes:
update table_name set varname = substr(varname, instr(varname, '-', -1) + 1)
where instr(varname, '-', -1) > 0;
or
update table_name set varname = regexp_substr(varname, '[^-]+', 1, 3)
where regexp_substr(varname, '[^-]+', 1, 3) is not null;
which will only update rows where the old value has (at least) three elements.
The two updates will behave differently if there are two elements - the first one with substr/instr will always take the last part after a dash if there are any; The second one with rexexp_substr will not update one with a single dash. You've only shown one fixed pattern so none of that may be relevant to you. At least, until you run the same update again against that data...

Assigning Value along with Data Retrieval

Is there a way to combine assigning a value to a variable and Selecting a column in sql. I need to compute and select a column in a table based on the variable. The variable's value changes based on another column in the table.
var #BeginValue
Columns in table : ReducedBy
My initial begin value is stored in #BeginValue. The table has reducedBy which is a factor by which my begin value should be reduced. So when i select, beginvalue for the first recored would be #BeginValue and the #EndValue should be #BeginValue = #BeginValue - reducedBy. It continues like this, as many times as the number of records in my table.
Result set must be like this:
#Begin = 10
Begin End ReducedBy
10 8 2
8 6 2
6 5 1
Is there a way with which i can achieve this without using a cursor or with multiple update statements.
You can't assign in a query that returns a result set. The closest you can get is to store the result in a table variable. Then you can both do computations against that table, and return it as a result set:
-- Store results in table variable
declare #tbl table (id int, col1 int, ...)
insert #tbl
(id, col1, ...)
select id
, col1
, ...
from ... your query here ...
-- Assign variable
select #YourVariable = ... your computation here ...
from #tbl
-- Return result set
select *
from #tbl
If your question is
Can I do..
SELECT #a = field, field2 from table
and get a resultset and set the value of #a?
Then the answer is no, not in a single statement.