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

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.

Related

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)

REGEXP_SUBSTR return all matches (mariaDB)

I need to get all the matches of a regular expression in a text field in a MariaDB table. As far as I know REGEXP_SUBSTR is the way to go to get the value of the match of a regular expression in a text field, but it always returns after the first match and I would like to get all matches.
Is there any way to do this in MariaDB?
An example of the content of the text field would be:
#Generation {
// 1
True =>
`CP?:24658` <= `CPV?:24658=57186`;
//`CP?23432:24658` <= `CPV?:24658=57186`
// 2
`CP?:24658` <> `CPV?:24658=57178` =>
`CP?:24656` <> `CPV?:24656=57169`;
And the select expression that I'm using right now is:
select REGEXP_SUBSTR(textfield,'CP\\?(?:\\d*:)*24658') as my_match
from table
where id = 1243;
Which at the moment returns just the first match:
CP?:24658
And I would like it to return all matches:
CP?:24658
CP?23432:24658
CP?:24658
Use just REGEXP to find the interesting rows. Put those into a temp table
Repeatedly process the temp table -- but remove the SUBSTR as you use it.
What will you be doing with each substr? Maybe that will help us devise a better approach.

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.

Remove a part of string containing another field in SQLite

I need to remove a part of string in one column corresponding to another column.
I know I can use the REPLACE function for that, but not sure how to use it.
So, in my case, I want to remove the first part of the "name" column that is the same as the winery column.
Example:
Name: Family Wines Vintage Special
Winery: Family Wines
I want to obtain:
Name: Vintage Special
Winery: Family Wines
Possible problems:
the function needs to do nothing if Name and Winery are the same.
don't keep the space at begin of Name field after removing
if possible, clean the Name string if starts with coma (') or semicolon (;) after the removing OR use another query for that
Something like that:
UPDATE usr_wines SET name=REPLACE(name, winery, '') WHERE name LIKE '%' || winery;
Thanks,
To remove a character from the beginning of a text value, use the substr() function:
UPDATE usr_wines
SET Name = substr(Name, 2)
WHERE Name LIKE ',%';
The same can be done with a dynamic string:
UPDATE usr_wines
SET Name = substr(Name, length(Winery) + 1)
WHERE Name LIKE Winery || '%'
AND Name != Winery;

SQLite SELECT statement where column equals zero

I'm preety new to SQLite.
I have a preety basic question.. Why can't I select rows where specific column equals zero?
The is_unwanted column is type TINYINT (which I see in SQLite basically means INTEGER)
So, I have only one record in the database (for testing).
When I try
SELECT is_unwanted FROM 'urls'
I get a result of "0" (zero), which is fine because that column contains the actual number 0.
I tried =>
SELECT * FROM 'urls' WHERE is_unwanted = 0
And got NO result, but
SELECT * FROM 'urls' WHERE is_unwanted <> 0
gives me result.
What am I doing wrong??
Try running
select '{' || is_unwanted || '}' from urls
to see if the value in the database is really a string containing spaces.
SQLite is a dynamically typed database; when you specify TINYINT is is a hint (SQLite uses the term "affinity") for the column. You can use
select is_unwanted, typeof(is_unwanted) from urls
to see the values with their types.
You could try:
SELECT * FROM urls WHERE coalesce(is_unwanted,'') = ''

Resources