Assume that I have 1234. I want a query to select all columns that match:
1234
01234
001234
0001234
...
How can I do that in SQLite?
You could try to convert the string into a number:
SELECT ... WHERE CAST(Column AS NUMBER) = 1234
However, this also allows leading spaces, or .0 floating-point numbers.
you can use 'like' query
SELECT * FROM table WHERE field like '%1234%'
Related
I've got a database with several columns and a column name and I'm doing the following sqlite statement through OrmLite:
SELECT `name`,`id`,`ita_name`,`setCode`
FROM `MyTable`
WHERE (`name` LIKE ?)
ORDER BY `name` LIMIT 30
everything works fine but if the value of the name column is "Michael*,* Basketball player" and I submit "Michael Basketball player" the query return no results.
I've already tried to use %myQueryText% but with no success.
What can i do to return values from my column ignoring "," ?
SQL LIKE statement is more specific, than you think. The % wildcard matches zero or more characters at the position of the wildcard, so %myQueryText% matches anythingmyQueryText OR myQueryTextanything but not myQuery(comma)text. You need place wildcard at position of comma, see SQL LIKE wildcards.
So you probably want something like
... WHERE (`name` LIKE "Michael%Basketball player") ...
Edit/ Prepare your search pattern in Java, possibly like
String searchPattern = "Michael Basket player";
String preparedPattern = searchPattern.replace(' ', '%');
// outputs pattern "Michael%Basket%player"
Which could match even Michael the best Basketbal and other sports player and if you want to overcome such troubles, you can use REGEXP and prepare search pattern:
String searchPattern = "Michael Basket player";
String preparedPattern = searchPattern.replace(' ', '[,\s]+');
// outputs pattern "Michael[,\s]+Basket[,\s]+player"
and then in SQL query change WHERE statement to
WHERE `name` REGEXP "Michael[,\s]+Basket[,\s]+player"
the [,\s]+ matches either comma or white space and the occurance is one or multiple times, so it matches one space or space and comma or comma and more spaces...
I have the following problem:
I try to query from the database the names that contain characters with diacritics.
The selection is:
String like = "ë";
String selection = "LOWER(name) LIKE \"%" + like + "%\"";
The problem is even if i have rows that contain "Ë" the query is empty. Any ideas?
And I do not want to build a separate column for normalized names.
From SQLite DOC :-
(A bug: 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.)
So, you should replace diacritics before using with like. Remove diacritics from string in Java is good SO to do that. Or to use lower and either cases separate.
try this
String selection = "LOWER(name) LIKE \"'%" + like + "%'\"";
I'm seeing some weird behaviour on my FTS enabled SQLite database. I have a table named fingerprints that contains a column named scan. Entries of scan are long strings that look like this:
00:13:10:d5:69:88_-58;0c:85:25:68:b4:30_-75;0c:85:25:68:b4:34_-76;0c:85:25:68:b4:33_-76;0c:85:25:68:b4:31_-76;0c:85:25:68:b4:35_-76;00:23:eb:ad:f6:00_-87; etc
It represent MAC addresses and signal strengths. Now I want to do string matching on the table and try to match for instance a MAC address:
SELECT _id FROM fingerprints WHERE scan MATCH "00:13:10:d5:69:88";
This returns a lot of rows that do not have the specified string in it for some reason. Second thing I will try to match is
SELECT _id FROM fingerprints WHERE scan MATCH "00:13:10:d5:69:88_-58";
This returns the same rows has before and is completely wrong.
Does SQLite treats the : _ - characters in any special way?
Thanks
What you're seeing is the effect of the FTS tokenizing your data.
The full text search doesn't work on un-processed long strings, it splits your data (and your search terms) into words and indexes them individually. The default tokenizer uses all alphanumeric characters and all characters with a code point >128 for words, and uses the rest of the characters (for example, as you're seeing : _ -) as word boundaries.
In other words, your search for 00:13:10:d5:69:88 will search for rows containing the words 00 and 13 and 10 and d5 and 69 and 88 in any order.
You can verify this behavior;
sqlite> CREATE VIRTUAL TABLE simple USING fts3(tokenize=simple);
sqlite> INSERT INTO simple VALUES('00:13:10:d5:69:88');
sqlite> SELECT * FROM simple WHERE simple MATCH '69:10';
-> 00:13:10:d5:69:88
EDIT: Apparently SQLite is smarter than I originally gave it credit for, you can use phrase queries (scroll down about a page from the link destination) to look for word sequences, which would solve your problem. Phrase queries are specified by enclosing a space (or other word separator) separated sequence of terms in double quotes (").
sqlite> SELECT * FROM simple WHERE simple MATCH '"69:10"';
-> No match
sqlite> SELECT * FROM simple WHERE simple MATCH '"69 88"';
-> 00:13:10:d5:69:88
sqlite> SELECT * FROM simple WHERE simple MATCH '"69:88"';
-> 00:13:10:d5:69:88
I did a bit of research about sql escape characters and count statements and didnt find a solution to my question. Even though I used stuff like:
SELECT * FROM table WHERE path LIKE '%/_%' ESCAPE '/';
I got a table where in a column there is paths so I want to select the items where I have certain number of slashes:
ID DIRECTORY
1 root/A
2 root/B
3 root/A/1/2
4 root/B/1/2
5 root/A/1
6 root/B/2
so, how do I select for example the elements that have only 2 slashes??
Edit 1: This is to be done in Android SQL-Lite Database
You can use a regular expression:
SELECT * FROM table WHERE path REGEXP '^([^/]*)/([^/]+)/([^/]*)$';
The above expression looks specifically for an optional group of characters not containing /, followed by /, followed by another group without /, followed by /, and optionally another set of characters before the end of the string.
So:
/Bxx92/2 -- match
5 root/A/1 -- match
6 root/Bxx92/2 -- match
6 root/Bxx92/2 -- match
7 root/Bxx92/ -- match
6 root/2 -- NO match
If there MUST be something before the first and after the last /, change the expression to '^([^/]+)/([^/]+)/([^/]+)$'
You can use this trick to count occurrences of a character in a string:
SELECT LENGTH('path') - LENGTH(REPLACE('path', '/', '')) AS `occurrences`
So you can achieve the goal with
SELECT id, path FROM
(SELECT id, path, LENGTH('path') - LENGTH(REPLACE('path', '/', '')) AS `occurrences`
FROM table) temp
WHERE occurrences = 2
However, I expect performance will be terrible. If you are going to query like that, consider adding a column with the path depth so that you can query directly with
SELECT id, path FROM table WHERE depth = 2
I've a column in which contains numbers or strings. The type of the column is varchar.
Usually when we sort it using the string field, then all the numbers come first and then strings start. But I want all the strings first and then numbers.
TIA !
You'll have to write it in two separate queries. One for selecting numbers, the other for strings. Preferably I would create a second column (one for numbers, one for strings), making it easier and faster to have those two queries run.
This worked for me...
Select * from Table order by stringfield+0;
edit: http://www.sqlite.org/datatypes.html (Point 4.0)
UPDATE: Try this....
select * from Table where LENGTH(trim(stringfield,"0123456789 ") )=0 union select * from table order by stringfield;
How about the following (two queries as suggested above):
select * from Table where LENGTH(trim(stringfield,"0123456789 ")) > 0; select * from table where LENGTH(trim(stringfield,"0123456789 ")) = 0;
The first select should return only values that are not numeric, whilst the second should return only values that are numeric.
For a table that contains a mixture of numeric and string data, this outputs the strings first, then the numbers.
Have you considered creating a custom collation-function? I have never used this myself, but it sounds like exactly what you need.