Finding rows that start with a and b in SQLITE? - sqlite

Im trying to find all rows that start with J or M in sqlite. For example in Microsoft Access I know you can use :
SELECT *
FROM Customers
WHERE cust_name LIKE '[JM]%';
However Im not sure what the equivalent of [] is in sqlite. I have
tried:
SELECT *
FROM Customers
WHERE cust_name LIKE 'J%' OR 'M%'
ORDER BY cust_contact;
But this only returns rows that start with J :/ Any help would be appreciated.

Or use GLOB instead of LIKE, but it is case sensitive and uses a different syntax.
WHERE cust_name GLOB '[JjMm]*'
LIKE is case sensitive unless options have been set otherwise, like PRAGMA case_sensitive_like = boolean;. Although case sensitivity can be set for LIKE, GLOB is always case sensitive.

Try the following code
SELECT * FROM Customers
WHERE cust_name LIKE 'J%' OR
cust_name LIKE 'M%'
ORDER BY cust_contact;

Related

SQlite3 bound params and LIKE

Sqlite3 provides the sqlite3_bind_* functions which allow one to do parameter substitution into a SQL query. My question is: what is the right way to combine this with LIKE queries? For example, I might want to do:
SELECT * FROM thing WHERE name LIKE '%?'
but that doesn't work at all. Is the best way really just:
SELECT * FROM thing WHERE name LIKE ?
and then put the pattern characters into the actual string value to be substituted?
To concatenate strings, use the || operator:
SELECT * FROM thing WHERE name LIKE '%' || ?

Searching middle part of the string using Full Text Search in sql server

I am not able to search the middle part of string using fulltext search index for eg:there was a string "I like music" i was not able to search for like which is in the middle part of string..
Try LIKE operator.
SELECT
*
FROM
YourTABLE
WHERE
ColumnName LIKE '%like%'
Use Like keyword in query as follows:
select * from tablename where col like '%like%'
Here is the tutorial of like in sql:
http://www.w3schools.com/sql/sql_like.asp
Here is the MSDN:
http://msdn.microsoft.com/en-us/library/ms179859.aspx
Hope its helpful.
Well, in order to still use the fulltext index and to avoid a table scan, try this
select *
from yourTable
where contains (ColumnName,'like')
and (ColumnName not like 'like%' or ColumnName like '_%like%')
However, the query optimizer might decide that a full table scan is more effective.
Check if statistics of your table out of date, especially check the statistics relates to your fulltext index. Since you are using SQl Server 2008, so, you can query DMV to get statistics information of your index or using DBCC to see the statistics detail information
DBCC SHOW_STATISTICS ("[schema].[table]",indexname);
Check the first return result, [Updated], [rows], [rows sampled], if the statistics out of date, or [rows sampled] far less than [rows], which many cause SQL Engine decided to use table scan instead of using your index.

My sqlite3 database/queries are very slow. Switch or fix queries?

I have a large sqlite db. Its 185mb.
This query is taking about 5seconds and it returns 2 rows. I added an index to user.name than Content.user_id. It stills take many seconds. Can sqlite handle large files like this? Is there a simple fix for a private app like telling sqlite to put everything in ram on app startup? (Its C#.NET for dev use only).
select Content.*,name from user
join Content on Content.user_id=user.id
where user.name like 'some_name' order by some_col ASC;
Try the following:
Replace LIKE 'some_name' with = 'some_name'. LIKE queries with underscore or percent sign will not perform as well as equality checks. Depending on the position of the first percentage sign or underscore they might be unable to use your index on user.name.
Launch ANALYZE.
Followup to your answer:
I think you have case-insensitive LIKE pragma enabled. This means that:
You should recreate your database and use name COLLATE NOCASE in the definition of the name column of table users. Thus, all comparison tests on user names will be case insensitive, and so will your index be. This way, a case-insensitive LIKE can use the index also.
Perhaps you should check your indexes

sqlite full text wild card search

Can Sqlite FT3 or FT4 do something like
SELECT * FROM MyTable WHERE body MATCH '*qlite'
I know this:
SELECT * FROM MyTable WHERE body MATCH 'Sqlite*'
works but seems like '%like' like operation doesn't work in the full text.
From what I understand it's a limitation of FTS in general, across platforms, that suffix/postfix searches aren't possible.
The best workaround I've seen is to add a column to MyTable called ReverseBody and store the reverse of the Body column in there and add it to the FT index as well. Then you write queries like
select * from MyTable where reversebody match (REVERSE('qlite') + '*')
I work in SQL Server so we have a REVERSE built in. I don't think SQLite does, but you can add custom functions to do it as descrbed here

sqlite Query optimisation

The query
SELECT * FROM Table WHERE Path LIKE 'geo-Africa-Egypt-%'
can be optimized as:
SELECT * FROM Table WHERE Path >= 'geo-Africa-Egypt-' AND Path < 'geo-Africa-Egypt-zzz'
But how can be this done:
select * from foodDb where Food LIKE '%apples%";
how this can be optimized?
One option is redundant data. If you're querying a lot for some fixed set of strings occuring in the middle of some column, add another column that contains the information whether a particular string can be found in the other column.
Another option, for arbitrary but still tokenizable strings is to create a dictionary table where you have the tokens (e.g. apples) and foreign key references to the actual table where the token occurs.
In general, sqlite is by design not very good at full text searches.
It would surprise me if it was faster, but you could try GLOB instead of LIKE and compare;
SELECT * FROM foodDb WHERE Food GLOB '*apples*';

Resources