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)
Related
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.
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'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.
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.
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,'') = ''