sqlite query greater than or equal to with wildcards - sqlite

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.

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.

Select row if column value can be found in a string

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)

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.

SQLite strings filtering

for example my "select" query returns rows:
"asd/1"
"asd/2"
but for me rows "asd/1", "asd/2" represents the same value. Is any way to truncate strings to such result: (i want to truncate everything after '/' inclusive)
"asd"
??
Something like this should work
select distinct substr(column_name, 1, instr(column_name, '/') - 1) from table_name
This get back column_name up to the first '/' in the string (but not including the slash because of the -1) and then only give back the unique results (because of the distinct keyword)

How to concat two column with '-' seperator in PL/SQL

I just want to concat two columns with seperator '-'.
These are the two columns, want to concat.
I am using this query to concat them
select concat(amt,endamt)as amount from mstcatrule
and it is giving me result this
But I Want that data of 2 columns should be sepearted by '-'
RESULT I WANT IS :
AMOUNT
0-0
100-99999999999
100-500
Alternative:
select amt || '-' || endamt as amount from mstcatrule;
Do it with two concats:
select concat(concat(amt, '-'), endamt) as amount from mstcatrule;
concat(amt,'-') concatenates the amt with the dash and the resulting string is concatenated with endamt.
Another way is to use double pipe.
select amt || '-' || endamt as amount from mstcatrule;
You may have to convert amt and endamt to varchar
In oracle this works for me! :D
select amt||'-'||endamt as amount from mstcatrule
Alternative you can use under query
select concat(amt,'-',endamt) as amount from mstcatrule;
A generic format for the query
Select concat(column1,'-',column2) as concatedCols from table_Name
For Postgresql only

Resources