Select row if column value can be found in a string - sqlite

I have a string that contains ID numbers, something like this: „1;3;5;6;7;“
I want to select all rows in an SQLite table that have an ID which is contained in that string.
One select statement that gives me the rows 1,3,5,6 and 7.
Any idea how to do this?

You can do it with LIKE operator:
select * from tablename
where ';' || '1;3;5;6;7' || ';' like '%;' || id || ';%'

You can use the SQLite instr core function
SELECT * FROM your_table WHERE instr(';'||'1;3;5;6;7;',';'||id||';');
preceding the list with ; and wrapping the id in ;'s ensures that only the specific values are extracted (e.g. so that 1 doesn't get 11 111 etc)

Related

How to concatenate a Select query inside a INSTR() in SQLite?

I was trying to order a result set by the order of the values in an IN() clause.
SELECT * FROM CrossReference WHERE cross_reference_id IN (SELECT Id FROM FilteredIds)
So I tried to find a function such as MySql FIELD(). Then I found these answers (answer1, answer2) which explain how to do the exact thing on SQLite using the INSTR().
SELECT *, INSTR(',GDBR10,GDBR5,GDBR30,', ',' || ticker || ',') POS
FROM tbl
WHERE POS>0
ORDER BY POS;
So it's working as expected, but I want to populate the ids dynamically using a select query. I tried many approaches, but nothing seemed to work. Here is the last one I tried. It gave me just one result row (a result related to the first filterId).
SELECT *, INSTR (','||(SELECT id FROM FilteredIds)||',', ',' || cross_reference_id || ',') POS FROM CrossReference WHERE POS>0 ORDER BY POS;
So I guess I'm making some kind of mistake when concatenating the SELECT query with the rest of the code. Because when I manually enter the filtered Ids it works and returns results according to the entered filter ids.

sqlite query greater than or equal to with wildcards

I'm using this code:
SELECT *
FROM TableName
WHERE (ColumnName >= '' AND ColumnName < '豈')
OR (ColumnName >= '󰀀' AND ColumnName < '󿿾')
OR (ColumnName >= '􀀀' AND ColumnName < '􏿾');
from this answer - but it only returns entries that begin with anything in those ranges.
I need to be able to find entries that don't begin with characters in those ranges but contain characters that exist in the above ranges.
I have tried changing
'' AND ColumnName < '豈'
to
'%""%' AND CHS < '%"豈"%'
hoping that would work - but it evidently doesn't work like that.
How can I get this to work?
For single characters, you could use LIKE, but character ranges require GLOB:
SELECT ...
FROM MyTable
WHERE ColumnName GLOB '*[-豈]*'
OR ColumnName GLOB '*[󰀀-󿿾]*'
OR ColumnName GLOB '*[􀀀-􏿾]*';
The only way to do this with standard SQL would be to include an OR clause for each and every character within your ranges:
SELECT * FROM Table WHERE
ColName LIKE '%X%' OR
ColName LIKE '%Y%' OR
ColName LIKE '%Z%' . . .
which is tedious and probably not practical depending on how many characters are in your ranges.
Two other options you can look at are regular expressions, represented in SQLite with the REGEXP operator:
SELECT * FROM Table WHERE ColName REGEXP 'regular_expression'
or else full text search, documented here: http://www.sqlite.org/fts3.html.

how to search for a particular string in the given string in oracle

I have a string with value as '12A,12B,12C,13,14'.
I want to check whether '2A' is available in the above string.
while trying my value '2A' checks in 12A and returns as matched.
Please give me a solution for this.
You can do something like this:
select * from table where ',' || col || ',' like '%,2A,%';
Commas are concatenated to the column to cover the cases where the element is present at the start or end of the string.

Using column values from one table within a select clause from another table

I have an Oracle table called: col_mapping where a column in this table has the column values of columns from another table.
Example data of table: col_mapping
ID DESCR COL_VALS
------------------------------
1 LABEL COL_1
2 NAME_ADDR COL_2:COL_3
3 SALARY COL4
Based on the above table, I now would like to go through each record in col_mapping and use the COL_VALS as part of my
select COL_VALS from other_tab
obviously, where there is more than one value like COL_2:COL_3, need to replace the ':' with a ','
SELECT COL_1 from other_tab;
SELECT COL_2,COL_3 from other_tab;
SELECT COL_4 from other_tab;
You can generate and execute insert statements based on the col_vals value using dynamic SQL, inside a cursor loop over the col_mappings rows:
begin
for r in (select replace(col_vals, ':', ',') as cols from col_mapping) loop
dbms_output.put_line('insert into staging_tab(' || r.cols || ')'
|| ' select ' || r.cols || ' from other_tab');
execute immediate 'insert into staging_tab(' || r.cols || ')'
|| ' select ' || r.cols || ' from other_tab';
end loop;
end;
/
The cursor just gets the cols_val value, replacing colon with comma. The generated insert uses that modified cols_val as both the insert column list and the select list - since you said the staging table will match the other_tab structure.
The dbms_output line is just so you can see the generated statements - it ins't necessary for the code to run, and you'll need to set serveroutput on or equivalent to see it anyway. With that enabled, running that block using your example mapping data shows:
PL/SQL procedure successfully completed.
insert into staging_tab(COL_1) select COL_1 from other_tab
insert into staging_tab(COL_2,COL_3) select COL_2,COL_3 from other_tab
insert into staging_tab(COL4) select COL4 from other_tab
What you'll end up with is the data from each row in other_tab spread across multiple rows in staging_tab, with lots of null values. Hopefully that's what you intended.

select the union of several tables together in a single step

I am very new to Oracle 11g and am trying to generate a large string by appending text for each column in a select statement and using a cursor to store the results. However I want the last statement to not have a union all included. The final result I want to build large string of each row generated or simply execute the result if possible.
Note: column1 has a list of schemas that I am interested in.
select 'select * from ' || column1 || '.' || column2 || ' union all ' from mytable
This is where column1 is the schema, column2 is the table name.
What is the simplest way to generate the final string without using rtrim to remove the last string. And is there a simple way to append all these rows together in the string automatically?
The final goal is to actually just execute the union into a resulting cursor.
If you're querying in a loop anyway I wouldn't try to construct the string as part of the select at all; I'd do it all within the loop. Something like (untested):
declare
str varchar2(32768);
begin
for rec in (select column1, column2, rownum as rn from mytable)
loop
if rec.rn > 1 then
str := str || ' union all ';
end if;
str := str || 'select * from "' || rec.column[ || '"."' || rec.column2 ||'"';
end loop;
-- do something with str e.g. display to verify the syntax
-- before using in a cursor
dbms_output.put_line(str);
end;
Rather than adding union all to the end of every row except the last one,the rn check means it's added to the start of every row except the first one, which is easier to detect.
I've also wrapped the schema and table names in double quotes, just in case you have to deal with any quoted identifiers. But if your stored values don't match the case of the owners and table names in all_tables this will cause a problem rather than solve it.

Resources