I have a table with an url column with urls like so:
https://shop.domain.com/product/12345/this-is-great-product
I need to extract the last part from the last slash to the end:
this-is-great-product
I was planning in using something REGEXP_SUBSTR but its an extension that I don want to install.
How can i do this in SQLite version 3.39.4 ?
You can use JSON: convert your delimited string (url) into a valid JSON array, then take the last element:
CREATE TABLE IF NOT EXISTS urls(url);
DELETE FROM urls;
INSERT INTO urls(url) VALUES ('https://shop.domain.com/product/12345/this-is-great-product');
SELECT json_extract('["' || replace(url, '/', '", "') || '"]', '$[#-1]') AS suffix FROM urls;
See the fiddle.
Related
I have a master table containing URLs:
CREATE TABLE IF NOT EXISTS MasterTable (url, masterId, PRIMARY KEY(url), UNIQUE(masterId));
An url string looks like this: file:///Users/user1/Pictures/rubus_and_apple.jpeg.
Now I want to lookup on the url column but only on the filename rubus_and_apple and not on all url string. (Meaning only on the last component of the url w/o extension).
For example I want to look the keyword rubus and get the url:file:///Users/user1/Pictures/rubus_and_apple.jpeg.
I need my query to be like:
SELECT masterId
FROM MasterTable
WHERE <url last component w/o extension> LIKE '%/rubus%';
How can I do so?
You could use LIKE:
SELECT masterId
FROM MasterTable
WHERE url LIKE '%/rubus.%';
Please note that this expression is not-SARGable so it won't use index.
EDIT:
WITH MasterTable(MasterId, url) AS(
VALUES(1, 'file:///Users/user1/Pictures/rubus_and_apple.jpeg')
)
SELECT *
FROM MasterTable
WHERE REPLACE(url,RTRIM(url,REPLACE(url,'/','')),'') LIKE '%' || 'rubus' || '%';
-- part of string after last /
db<>fiddle demo
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.
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.
while inserting i can insert danish character in proper format in sqlite Db but while retrieving my query returns no result
String searchQuery= "SELECT * FROM article,product where article.ItemNo=product.ItemNo ";
if(searchText.length()>0)
{
searchQuery += " AND (article.itemNo like '"+ searchText +"%' OR product.Description like '"+ searchText +"%')";
}
in debug mode query is
`SELECT * FROM article,product where article.ItemNo=product.ItemNo AND (article.itemNo like '%ø%' OR product.Description like '%ø%')..`
No result returns
Proper query will be
SELECT * FROM article,product where article.ItemNo=product.ItemNo AND (article.itemNo like '%Ø%' OR product.Description like '%Ø%');
the desired description field value in Db is MØNTPUNG.
I am wondering is there any issue of case sensitivty?I am using UTF8 encoding for my raw file that will insert data to DB.
The documentation says:
SQLite only understands upper/lower case for ASCII characters by default. The LIKE operator is case sensitive by default for unicode characters that are beyond the ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ' LIKE 'Æ' is FALSE.
To handle non-ASCII characters correctly, store an uppercase version of your string(s) in a separate column, and search in that with an uppercase search pattern.
Given the following tables:
create table index(name text, docid int);
create virtual table docs using fts4();
The following query works as intended when querying for a single token (for example: march, or bad):
select name from index where docid in (select docid from docs where docs match ?)
But how can I query for more than one token (say, bad bed)? Binding the string bad bed directly does not work (always selects nothing), neither surrounding the placeholder or the string with double quotes, nor using AND to MATCH each token separetly (this last one throws an error).
Using intersect does work, but it's clunky and innefficient when searching for many tokens:
select name from index where docid in (
select docid from docs where docs match ?
intersect
select docid from docs where docs match ?
intersect
...
)
Each ? is paired with a single token.
You can use the concatenation operator || in sqlite. '' would be the empty string
SELECT * FROM table WHERE docs MATCH '' || ? || ' ' || ? || ' ' || ? || ''
Make sure there is a space between every token or an ' AND '.
Update
Actually it doesn't work. It seems there are tokenator issues with this approach. Its better to concatenate all the tokens with the space and bind the resulting string with a single '?'
There are operators within the match syntax in FTS so you can use AND, OR and NOT.
See here for documentation
e.g.
-- Return the docid values associated with all documents that contain the
-- two terms "sqlite" and "database", and/or contain the term "library".
SELECT docid FROM docs WHERE docs MATCH 'sqlite AND database OR library';