SQL select rows case INsensitive not works my statement - sql

I have Android app that sends to server pattern to find user by name. I want to be able to find rows where VARCHAR column is like pattern (but case INsensitive) that I send to server. Lets say we have a nickname like 'Bigboss'. The SQL statement should be able to find this row either when is passed 'Bigboss' or 'bIGBOSS' or 'biGbOsS' or 'bi' etc. Currently it works weird.
Here is my SQL statement:
SELECT *
from users_details
where upper(name) like '" + pattern + "%'
or lower(name) like '" + pattern + "%'
Help me please.

SELECT *
from users_details
where name ilike '" + pattern + "%'
see the manual :
The key word ILIKE can be used instead of LIKE to make the match
case-insensitive according to the active locale

Can you try this.
SELECT * from users_details where upper(name) like '%PATTERN%'
If you are passing pattern dynamically then like pattern.touppercase()

Related

SQL Query to search by name(containing combination of two or more word)

When i search by using
....LIKE '%" + txtName.Text + "%'.....
Then it returns all the rows containing txtName. It is okay for name like
Bijay or
Bijay kumar.
And rows contains data like
Bijay,
Bijay kumar kush,
Bijay kush.
In this case it should return, Bijay kumar kush and Bijay Kush when search by using txtName like Bijay Kush. But not getting the output like this. It is empty when search by using first name and last name without supplying middle name.HeretxtNameis the Full Name text Box used for searching the name.I want to know where am I making the mistake.Any help would be appreciated. thanks in advance.I am using this type of query which doesn't return the result if searched without middle name as stated above.
...
if (!String.IsNullOrEmpty(txtName.Text) && !String.IsNullOrWhiteSpace(txtName.Text))
SearchSqlDataSource.SelectCommand += " AND (New_Employees.Full_Name LIKE '%" + txtName.Text + "%' OR New_Employees.Full_Name LIKE '%" + txtName.Text + "%')
I might suggest replacing all spaces with the wildcard character. You can do this in the application or the database. Assuming the underlying database is SQL Server, here is the database code:
WHERE New_Employees.Full_Name LIKE REPLACE('%" + txtName.Text + "%', ' ', '%')

How do I run an SQL update query using a like statement

I am trying to update a field in a table using an SQL update query where there is a like statement referencing a value in another table. They syntax unfortunately is not working. Below is my code. In short, I am trying to put a '1' in the field 'Query07ParolaChiave' in the table 'tblSearchEngine01' when the value located in table 'tblsearchengine07' is present in the field 'tblMasterListOfEventsNotes' located in the table 'tblSearchEngine01'. I think my code is almost complete but there is a syntax issue which i cant find.
st_sql = "UPDATE tblSearchEngine01, tblSearchEngine07 SET tblSearchEngine01.Query07ParolaChiaveSelect = '1' WHERE ((([tblSearchEngine01].[tblMasterListOfEventsNotes]) Like " * " & [tblsearchengine07].[ParolaChiave] & " * "))"
Application.DoCmd.RunSQL (st_sql)
I suggest you 2 solutions :
This one is using EXISTS functions, and will check for each row in tblSearchEngine01 if there is a matching value in tblsearchengine07
UPDATE
tblSearchEngine01
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
WHERE
EXISTS (SELECT 1
FROM tblsearchengine07
WHERE [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*')
This one is more performant because it uses JOIN
UPDATE
tblSearchEngine01
INNER JOIN tblsearchengine07
ON [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*'
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
I read something like in ADO/VBA, you have to use % instead of * as the wildcard.
You can have more information on wildcard and LIKE comparator here
UPDATE
Why the '1' after select in your first solution?
EXISTS (SELECT 1 ... is better for performance because it return only the number 1 instead of fields, anyway EXISTS just stop the excecution after 1 element found.
'Performant' means more consuming in regards to space and memory?
JOIN is more performant in term of time of execution, RDBMS are far better at joining tables than using subquery, in some rare case, it's more interesting to use the 1st solution.
Also, any initial thoughts as to why my original solution (coming straight from an Access Query which works) does not function?
I cannot really know but perhaps it's because of " * ", because you are saying SPACE + * + SPACE + VALUE + SPACE + * + SPACE. For ex : 'John' LIKE ' John '
May be with "*" instead of " * " could solve it...
I have no other track, I'm not Access sql developper, I usually play around Sql server/Oracle/mySql, hope it helped. ;)
Try to change your like this way:
... Like '*" & tblsearchengine07.parolachiave & "*'))"
The like statement go into the WHERE clause.
If you do want to use LIKE without you care about caps letters, then you can use it like this:
LIKE COLUMN_NAME = '%WhatYouLike%'
My suggestion is:
Use a table variable (#Table) with a unique/primary key coming from the table to be updated.
SELECT all the data to be updated (you can add the like statement here) and then INSERT that in the created table variable.
Construct the UPDATE statement with an INNER JOIN to the table variable matching with the unique/primary key.
I know this may take a lot of steps but believe me these are more efficient than using a black list approach.

Matching text string on first letter in SQL query

SAMPLE CODE:
Dim sql As String = "SELECT * FROM " + tblName + " WHERE needsTranslation = 'True' AND dataText LIKE " & "'" & alpha & "%" & "'" & " ORDER BY dataText;"
da = New SqlDataAdapter(sql, strConnection)
OP:
I would like to create a SQL query that returns all records when the first letter of a string matches my variable. I am coding this in an ASP.net code behind page in vb.net.
SELECT * FROM " + tblName + " WHERE textData = ' & alpha & "
In this exmample textData is a string of text and alpha is a single letter a through z or A through Z.
I don't need the criteria to be case sensitive, but I do need only the first letter of textData to match alpha.
I have tested the LIKE comparator and it does not return all records that begin with alpha.
What is the best way to do this? Any and all help will be appreciated.
thanks again,
The LIKE operator is what you'd want to use, but you have to use the % wildcard character like so:
SELECT * FROM MyTable WHERE textData LIKE 'a%'
SQL has sub-string operator SUBSTR() or SUBSTRING()
select * from tableName where substr( textData ) in ( 'A', 'B', 'C', ... );
I couldn't add to the comments on one of the other posts, but I'll strongly second the need to use a parameterized query for these reasons (you can include usage of the like operator with the wildcard % like the other answer correctly summarized to answer your question):
It will protect you from making mistakes with single quotes, especially if the user enters a search string that includes them
(they will cause your query to fail).
It protects you from SQL injection exploits. Example, a user were able to input the value of the variable "alpha" in the above
example they could enter something like:
'; DELETE FROM ;
If the user you were using had excessive database rights, they could
wreak all kinds of havoc (or they could potentially get access to
data they shouldn't have access to).

Collation Problem in asp.net connecting sql

Hi when I use this query I don't get any result.
In my table I have used collation Macedonian_BIN so the letters are in Macedonian Language.
What I need to do this code to work, it doesn't compare 2 values
For example
НОВ=НОВ
The ed is getting the value HOB I guess the problem is when is comparing the HOB value with the HOB in database
select * from Publisher where Motor like '%" + ed + "%'
You can explicitly add the COLLATE keyword to change the default collation as follows:
select * from Publisher
where Motor COLLATE Macedonian_BIN like '%" + ed + "%' ";
(substitute your desired collation)

Checking if a string is found in one of multiple columns in mySQL

I need to check if a string is found in one or more columns.
Basically, I have a program which lets you checks multiple fields (name, surname, etc...)
If both name and surname are checked and the user enters just the name, for example chris it would be easy to check it in mySQL with the LIKE parameter like this:
select * from tblClients WHERE name LIKE '%john%';
This obviously works. However, what I need to do is that if both name and surname are checked it would do something like this:
select * from tblClients WHERE (name or surname) LIKE '%john%';
I want that if this logic is made when there is a client named john doe, the second command will still find that client.
I tried this command and no syntax errors were found, however, 0 results where returned.
Any suggesstions please?
Can't you just use separate WHERE clauses, such as:
SELECT * FROM tblClients
WHERE (name LIKE '%john%')
OR (surname LIKE '%john%')
You're having to amend the SQL based on which columns the user selects anyway.
You'll need to do this:, as SQL will choke on the (name or surname) part
select * from tblClients WHERE name LIKE '%john%' or surname LIKE '%john%';
I'm not sure what language your using, but in psuedocode, you can make this a little easier with a simple function
query = [whatever you are searching for]
var columns = array('name', 'surname')
var where = array
foreach columns as column
where[] = column + ' like "%' + query + '%"'
sql = "select * from tblClients WHERE " + join(where, ' OR ');
//where join joins values of an array into a string
I recommend using UNIONs over ORs - ORs are notorious for poor performance, and risk maintenance issues if not properly understood:
SELECT c.* FROM tblClients c WHERE c.name LIKE '%john%'
UNION
SELECT c.* FROM tblClients c WHERE c.surname LIKE '%john%'
Keep in mind that UNION will return a distinct list - duplicates will be removed, but it will perform slower than using UNION ALL (which will not remove duplicates). Use what suits your purpose
Instead of having separate LIKE-clauses, you can concatenate the columns:
select *
from tblClients
WHERE name || surname LIKE '%john%'
This leaves some edge case (name = 'jo' and surname = 'hn' would be a match), if you're concerned about this you can add a separator between the columns
WHERE name || ' ' || surname LIKE '%john%'
I'm not sure about the performance impact of the two choices, but a LIKE with a percent at the start and the end will not use an index, so I don't think there will be problems.
This example column NAME and SURNAME one query, 1 space middle.
$Sql="SELECT * FROM customers WHERE CONCAT(NAME,' ',SURNAME) LIKE '%".$query."%' ";
//NAME = İSA
//SURNAME = ABC
//Query = İSA ABC
one query find.
I managed to program it.
Here is what I did:
$like = "";
foreach ($fields as $field)
{
if ($like == "")
{
$like = $field . " LIKE '%" . $query . "%' or ";
}
else
{
$like = $like . $field . " LIKE '%" . $query . "%' or ";
}
}
$like = substr_replace($like, "", -3);
This way, all ORs where insterted in a for loop and appended to a string. Then I programmed the SQL string like this:
$sql = "select * ";
$sql .= " from $table where " . $like
Hope this helps somebody else :)
Thanks for your posts.