Why like operator doesn't work - Sqlite - sql

I have this query that would return a search from base with the operation Like '%srch'
srch=search.getText().toString();
...
db.rawQuery("SELECT * FROM MyTable WHERE Name LIKE 'srch%' or Name LIKE '%srch' ",null);
but it doesn't work.
Can you explain why it doesn't work, and how I can fix it ?
thanks a lot

You can fix the query by changing it as below:
db.rawQuery("SELECT * FROM MyTable WHERE Name LIKE ?",
new String[] { "%" + srch + "%" });

why dont you use just one LIKE
db.rawQuery("SELECT * FROM MyTable WHERE Name LIKE '%'" + srch + "'%' ",null);

i find the solution i just do like this
db.rawQuery("SELECT * FROM MyTable WHERE name LIKE '%"+srch+"%'",null);
and its okay thanks all for help

Related

create user defined WHERE SQL phrase in r

I am using RODBC to connect to a database. I would love for a user to be able to define wildcard strings to lookup in the SQL as part of a function. I cannot use CONTAINS as the database is not full-text indexed.
The SQL I want to create is
"SELECT *
FROM mydataTable
WHERE (ItemNM LIKE '%CT%' OR ItemNM LIKE '%MRI%' OR ItemNM LIKE '%US%')"
The user should be able to define as many wildcards as they like, all from the ItemNM field and all separated by OR.
myLookup<-function(userdefined){
paste0("SELECT *
FROM mydataTable
WHERE ( LIKE '",userdefined,"')")
}
If I vectorise the userdefined (ie userdefined<-c("US","MRI")) then I end up with separate SQL strings which is no good. How can I get the output as above but for any length of user defined string where they are just defining the wildcard?
You could use :
myLookup <- function(userdefined) {
paste0('SELECT * FROM mydataTable WHERE (',
paste0('ITENM LIKE %', userdefined, '%', collapse = " OR "), ')')
}
userdefined<-c("US","MRI")
myLookup(userdefined)
#[1] "SELECT * FROM mydataTable WHERE (ITENM LIKE %US% OR ITENM LIKE %MRI%)"
We can use glue
library(glue)
mylookup <- function(userdefined){
as.character(glue('SELECT * FROM mydataTable WHERE (',
glue_collapse(glue("ItemNM LIKE '%{userdefined}%'"), sep=" OR "), ')'))
}
mylookup(userdefined)
#[1] "SELECT * FROM mydataTable WHERE (ItemNM LIKE '%US%' OR ItemNM LIKE '%MRI%')"

How to use SQL function CONCAT with LIKE

I confirmed that the columns in the SQL table contain the results I'm searching for (the results being NA), but when I use the query below, no results are returned.
Could I get some help with this please?
SELECT DISTINCT *
FROM [DB_NAME].[dbo].[TABLE_NAME]
WHERE BECDescription like '%Time Warner%' AND
(CONCAT(PhysicalAddress1, PhysicalCity, PhysicalStateProvince) like ('%NA% %NA% %NA%')
) AND
PhysicalCountry like '%NA%' AND
CarrierName like '%NA%' AND
CurrNetChargeAmt = 1326.00
I'm a little lost on why you are using CONCAT() here. Doesn't this do what you want?
WHERE BECDescription like '%Time Warner%' AND
PhysicalAddress1 like '%NA%' AND
PhysicalCity like '%NA%' AND
PhysicalStateProvince like '%NA%' AND
PhysicalCountry like '%NA%' AND
CarrierName like '%NA%' AND
CurrNetChargeAmt = 1326.00
It is certainly simpler to understand.
You concat PhysicalAddress1, PhysicalCity, PhysicalStateProvince but you add spaces between the '%NA'.
Try
SELECT DISTINCT *
FROM [db_name].[dbo].[table_name]
WHERE BECDescription like '%Time Warner%' AND (CONCAT(PhysicalAddress1, ' ', PhysicalCity, ' ', PhysicalStateProvince) like ('%NA% %NA% %NA%')) AND PhysicalCountry like '%NA%' AND CarrierName like '%NA%' AND CurrNetChargeAmt = 1326.00

Like statement across multiple columns in SQL

I'm trying to query a like statement across multiple columns. I have the following search terms:
'dog'
'cat'
'rabbit'
'gerbil'
'guinea pig'
'hamster'
and I need search for these terms or terms LIKE these in the 'animals' table which has about 40 different columns. I am aware I can do the like statement by doing
Select * from animals where [animalscolumn] like ('%dog%') or like ('%cat%') or like ('%gerbil%') or like ('%hamster%') or like ('%guinea pig%')
However the 'animalscolumn' isn't the only column I need to run the 'LIKE' statement across. I have to search for these terms in about 40 columns. Would anyone happen to know how? Thanks!
multiple like statements can not be used with or directly. You have to use column name for each like statement.
Use multiple like as mentioned below.
Select *
from animals
where
(
[animalscolumn] like ('%dog%') or
[animalscolumn] like ('%cat%') or
[animalscolumn] like ('%gerbil%') or
[animalscolumn] like ('%hamster%') or
[animalscolumn] like ('%guinea pig%')
)
If you want to find a set of number you can use IN
SELECT *
FROM tableName
WHERE columnId IN (154,156,133,157,119)
$sql = "SELECT * from like1 WHERE tutorial_author LIKE '$apply'
OR
tutorial_title LIKE '$apply'";
if($mode == 'search_contact'){
// $prefix='%';
$apply=$dataObj['search'];
$data = array();
// $sql = "SELECT * from add_contact WHERE tutorial_author OR tutorial_title LIKE '$apply'";
$sql = "SELECT * from add_contact WHERE
first_name LIKE '%$apply%'
OR
last_name LIKE '%$apply%'
OR
title LIKE '%$apply%'
OR
company LIKE '%$apply%'
OR
address LIKE '%$apply%'";
$result = $myConnection->query($sql);
if ($result->num_rows > 0) {
// print_r($result->fetch_assoc());
while($row = $result->fetch_assoc()) {
$row['user_image'] = site_url.upload_dir.$row['image'];
// print_r($row);
$data[]=$row;
}
$array = array('status'=>true, 'message'=> 'contacts fetched successfully', 'data'=> $data);
echo json_encode($array);
exit;
} else {
$array = array('status'=>false, 'message'=> "No contacts available" );
echo json_encode($array);
exit;
}
}
Select * from cscart_users
where status like '%a' and user_type like '%c' and firstname like '%az%';
Select * from cscart_users where status like '%a' and user_type like '%c'and firstname like'%az%';
syntax:
select * from tablename where columnname like'%b' and columnname2 like '%g';
Here is an example of a SQL Server stored procedure that takes a parameter.
CREATE PROCEDURE [dbo].sp_SearchMultiple #SearchTerm nvarchar(256)
AS
BEGIN
SET #SearchTerm = '%' + #SearchTerm + '%'
SELECT TOP 100
id, col1, col2, col3
FROM
asset_f
WHERE
col1 LIKE #SearchTerm OR col2 LIKE #SearchTerm OR col3 LIKE #SearchTerm
ORDER BY
id ASC
END

Is there an advanced like function where i can get results even if one word matches

hello I am trying to achieve something like this
should give results if
where 'Robert Doug' like '%Robert'
or
where 'Robert Doug' like '%Doug Robert'
if users enters first name or last name and if it matches in that whole string it should return the record.
Any help is appreciated.
Try like this
SELECT * FROM [YOUR TABLE_NAME] WHERE [COLUMN_NAME] LIKE ('%'+[USER PROMPT]+'%')
Or try this..
SELECT PATINDEX('%Robert%',NAME_COLUMN)
FROM [YOUR TABLE_NAME]
Or..
SELECT NAME_COLUMN
FROM [YOUR TABLE_NAME]
WHERE PATINDEX('%Robert%',NAME_COLUMN)<>'0'
Thank you all i tried something like this
WHERE
( d_searchJobTitle.term like '%'+case when CHARINDEX(' ','implementation analyst') >0 then SUBSTRING('implementation analyst',1,CHARINDEX(' ','implementation analyst')) else 'implementation analyst' end+'%'
and
d_searchJobTitle.term like '%'+ case when CHARINDEX(' ','implementation analyst') >0 then SUBSTRING('implementation analyst',CHARINDEX(' ','implementation analyst'),len('implementation analyst'))
else 'implementation analyst' end
+'%' )

Querying a condition where record from database is present in a string?

Basically, what I need is the inverse of this:
SELECT * FROM `table` WHERE `query` LIKE '%string%'
Something that looks like:
SELECT * FROM `table` WHERE '%string%' LIKE 'thestringmachine'
I looked up the internet an operation called CONTAINS() but it doesn't seem to work.
Can someone help me?
Edit:
$inc = "themachinethatthinks";
$query = mysql_query("SELECT * FROM `spell` WHERE CONTAINS('$inc',`word`)");
$num = mysql_numrows($query); $i=0;
while ($i < $num) {
$val = mysql_result($query,$i,"word");
echo $val."<br>";
$i++;
}
The database contains lot of words (only).
I am not sure I understand what you need, but I try:
SELECT * FROM your_table
WHERE LOCATE(your_col, 'thestringmachine') > 0
If you need query to be case insensitive:
SELECT * FROM your_table
WHERE LOCATE(LOWER(your_col), LOWER('thestringmachine')) > 0
I cannot try it, 'cause I don't have a SQL DB here at hand, but try this
SELECT * FROM `tablename` WHERE `thethinkingmachine' LIKE CONCAT('%', `colname`, '%');
If this would work, I doubt it will be very performant.