Character start with query in room persistent android - android-room

I have tried the following query but it does not give any result.
SELECT * FROM details WHERE name LIKE :name
I have used AutoCompleteTextview for searching purpose. is there any query to find with a substring?

Like MoinKhan suggested, adding the wildcard to the string before sending to Room worked for me.
query = "%"+query+"%";

I recently ran into this issue and found the nicest solution is to use Sql string concatenation || in combination with % which represents zero, one, or multiple numbers or characters.
#Query("SELECT * FROM details WHERE name LIKE '%' || :name || '%'")
fun getDetails(name: String): details
This will get the details where the name contains the one we are looking for
Similarly the below will find matches that start with the name
LIKE :name || '%'

Example:
#Query("SELECT * FROM user WHERE first_name LIKE :firstName ")
User findByName(String firstName);
I think u must use different name for "name".

Related

Using multiple keywords with LIKE in Postgresql through Spring Data JPA

I need to define a JPA query that translates as:
SELECT
*
from
items_table
where
lower(CONCAT(item_id,' ', color,' ',details,' ')) like all ('{"%blue%", "%plastic%"}') and
store='Main' and
status='In Stock' and
item_type='Container';
the parameter list in all will be an input along with the store, status and item_type from the front end.
I understand it's the first WHERE condition you're having a problem with. If a String[]-typed parameter doesn't cut it, try using this approach, hopefully the LIKE ALL will pass through to the generated query - sth like:
#Query("SELECT i FROM Item i WHERE LOWER(CONCAT(...)) LIKE ALL :terms AND ...")
List<Item> findByTerms(TypedParameterValue terms, ...)
itemRepository.findByTerms(new TypedParameterValue(StringArrayType.INSTANCE, terms), ...)
If it turns out JPA doesn't like LIKE ALL, you'll have to keep using the native query.

How to properly perform like queries with Quickbase

I am working with quicktable queries and everything seems to be fine.
Now I want to perform queries using like operators. For instance in PHP I can do something like:
$data ='content to search';
$stmt = $db->prepare('SELECT * FROM members where name like :name OR email like :email limit 20');
$stmt->execute(array(
':name' => '%'.$data.'%',
':email' => '%'.$data.'%',
));
Now in quick table, I have tried using CT, EX or HAS parameter etc with OR Operators. Only CT gives nearby result but not exact as per code below.
//Email = 7
//name =8
{
"from": "tableId",
"where": "{7.CT.'nancy#gmail.com'}OR{8.CT.'nancy'}"
}
Is there any way I can obtain a better search with like operators with Quickbase. The documentation here does not cover that.
CT is the closest string comparison operator in Quick Base to LIKE in SQL, but since you can't use wildcards in Quick Base queries you might need to group multiple query strings to achieve the same result. The is also a SW operator that can sometimes come in helpful for comparing parts of a strings.

Android Room Query cannot use same variable more than once

I am trying to convert this raw query to Query.
Raw Query:
SELECT * from numbers WHERE number=$numberToSearch OR number LIKE '%$numberToSearch%' OR number LIKE '%$numberToSearch' OR number LIKE '$numberToSearch%'
I started with simple one and created this Query
#Query("SELECT * from numbers WHERE number=:numberToSearch OR number LIKE :numberToSearch")
However, room things that my query needs to variables and creates exception. I can of course provide :numberToSearch few times as variable but that doe not sound right.
Is there any way writing a Query that works?
You should enclose your like query with % before passing as a parameter.
So correct representation of what you original query is goes as follows
val numberToSearch = 123
val numberToSearchLike = `%$numberToSearch%`
// You have to call with two parameters as parameters are not same
searchNumber(numberToSearch, numberToSearchLike)
And your DAO should look like this
#Query("SELECT * from numbers WHERE number=:numberToSearch OR number LIKE :numberToSearchLike")
fun searchNumber(numberToSearch:Int, numberToSearchLike: String)
Thats actually the way your do LIKE operations in Room
P.S.
In fact you don't have to have WHERE number=:numberToSearch as your LIKE operator will return the same thing.
So your query is same as saying
#Query("SELECT * from numbers WHERE number LIKE :numberToSearchLike")

Ecto where like query acts like where ==

I'm trying to get an ecto like query working like this:
def find(searchterm) do
query = from c in Contact,
#where: fragment("? % ?", c.company_name, ^searchterm),
where: like(c.company_name, ^searchterm),
contacts = Repo.all(query)
{:ok, contacts}
end
In my table, I have a company_name "Asymptote". Using where: like/2 my query looks like this:
SELECT c0."id", c0."company_id", c0."company_name" FROM "contacts" AS c0 WHERE (c0."company_name" LIKE $1) ["Asym"] (1.0ms)
when the pg_trm search uncommented, it looks like this:
SELECT c0."id", c0."company_id", c0."company_name" FROM "contacts" AS c0 WHERE (c0."company_name" % $1) ["Asym"] (1.0ms)
As far as I can see, the queries look good, but there are no results. Since I added the index after adding "Asymptote" to the database, I expect that is why it isn't found in the pg_trm index, but why won't like/2 or ilike/2 work? When entering in the full name "Asymptote", I am able to find the record.
I faced some similar problem. Unfortunately I had no pg_trgm available. I used the LIKE as in:
from candidate in query,
where: like(candidate.first_name, ^("%#{text}%"))
This matched the text in any place of the candidate.first_name.
With the help of Mitchell Henke of Rokkincat, here's a way to get pg_trgm working with custom match percentages:
def find(searchterm) do
limit = 0.1
query = from c in Contact,
where: fragment("similarity(?, ?) > ?", c.company_name, ^searchterm, ^limit),
#where: like(c.company_name, ^searchterm),
contacts = Repo.all(query)
{:ok, contacts}
end
But I still can't get like/2 working. Also, I don't see where in Ecto source this function exists.

Finding Last Name Using LINQ

I am trying to get last name using linq in visual studio. In my database, i have the Field name like "FullName".
In this Field I have value like "Subbu Cargos"
I Wanna Display the "Cargos" in my textbox.
How can i make a simple linq query?
Would it be over simple to say:
return FullName.Split(' ').Last()
?
I would suggest breaking it up into different fields - Firstname, Middlename, lastname, Title - and rebuilding the name on the fly when you come to display it.
If you're still determined to use one field, then consider a query like:
string s = "Subba Cargos";
var lastnames = from name in s.Split(new Char[] { ' ' }).Last<string>()
select name;
I would suggest not trying to parse out the last name. Like you say, first and last names could be switched around, someone might have a second name, or a last name that consists of multiple words (“van Dijk”), or may not have entered a last name at all.
Check out this article: Falsehoods Programmers Believe About Names
If you still want to do this however, try something like this:
customers.Select(c => c.FullName.Split(' ').Last());
You might not be able to this on the server side. In that case:
customers
.Select(c => c.FullName)
.ToList()
.Select(n => n.Split(' ').Last());
Untested, but this should give a rough idea.
You could also do it like this:
customers
.Select (b => b.FullName.Substring ((b.FullName.IndexOf(' ') + 1)));

Resources