How can I write this?
A delete statement to remove all records from the TableA table where the LastName starts with ba and they have a phone number in the 3 area code.
DELETE
FROM TableA
WHERE LastName = 'ba' AND PhoneNumber = '3';
Assuming the phone number column be text, then we can use LIKE comparisons as follows:
DELETE
FROM TableA
WHERE LastName LIKE 'Ba%' OR LastName LIKE 'ba%' OR PhoneNumber LIKE '3%';
MySQL, SQL Server, PostgreSQL, SQLite are all completely different database products. In all of them though, matching a pattern is done using LIKE, not =. To match a value that starts with a certain prefix you use LIKE 'something%'
% matches any character. It's the equivalent of * in a file search or .* in a regular expression.
DELETE FROM TableA
WHERE LastName LIKE 'ba%' AND PhoneNumber LIKE '3%'
Different databases have different case-sensitivity (collation) rules though.
In SQL Server and MySQL, case-insensitive sorting and searching is most common, so LIKE 'ba%' will match both Ba and ba.
In PostgreSQL, the norm is case-sensitive matching. You may have to use [Bb]a% in that case, to match either B or b
I looked around some and didn't find what I was after so here goes.
SELECT * FROM trees WHERE trees.`title` LIKE '%elm%'
This works fine, but not if the tree is named Elm or ELM etc...
How do I make SQL case insensitive for this wild-card search?
I'm using MySQL 5 and Apache.
I've always solved this using lower:
SELECT * FROM trees WHERE LOWER( trees.title ) LIKE '%elm%'
SELECT *
FROM trees
WHERE trees.`title` COLLATE UTF8_GENERAL_CI LIKE '%elm%'
Actually, if you add COLLATE UTF8_GENERAL_CI to your column's definition, you can just omit all these tricks: it will work automatically.
ALTER TABLE trees
MODIFY COLUMN title VARCHAR(…) CHARACTER
SET UTF8 COLLATE UTF8_GENERAL_CI.
This will also rebuild any indexes on this column so that they could be used for the queries without leading '%'
The case sensitivity is defined in the columns / tables / database collation settings. You can do the query under a specific collation in the following way:
SELECT *
FROM trees
WHERE trees.`title` LIKE '%elm%' COLLATE utf8_general_ci
for instance.
(Replace utf8_general_ci with whatever collation you find useful). The _ci stands for case insensitive.
This is the example of a simple LIKE query:
SELECT * FROM <table> WHERE <key> LIKE '%<searchpattern>%'
Now, case-insensitive using LOWER() func:
SELECT * FROM <table> WHERE LOWER(<key>) LIKE LOWER('%<searchpattern>%')
Simply use :
"SELECT * FROM `trees` WHERE LOWER(trees.`title`) LIKE '%elm%'";
Or Use
"SELECT * FROM `trees` WHERE LCASE(trees.`title`) LIKE '%elm%'";
Both functions works same
I'm doing something like that.
Getting the values in lowercase and MySQL does the rest
$string = $_GET['string'];
mysqli_query($con,"SELECT *
FROM table_name
WHERE LOWER(column_name)
LIKE LOWER('%$string%')");
And For MySQL PDO Alternative:
$string = $_GET['string'];
$q = "SELECT *
FROM table_name
WHERE LOWER(column_name)
LIKE LOWER(?);";
$query = $dbConnection->prepare($q);
$query->bindValue(1, "%$string%", PDO::PARAM_STR);
$query->execute();
use ILIKE
SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
it worked for me !!
Non-binary string comparisons (including LIKE) are case insensitive by default in MySql:
https://dev.mysql.com/doc/refman/en/case-sensitivity.html
I think this query will do a case insensitive search:
SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
You don't need to ALTER any table. Just use the following queries, prior to the actual SELECT query that you want to use the wildcard:
set names `utf8`;
SET COLLATION_CONNECTION=utf8_general_ci;
SET CHARACTER_SET_CLIENT=utf8;
SET CHARACTER_SET_RESULTS=utf8;
well in mysql 5.5 , like operator is insensitive...so if your vale is elm or ELM or Elm or eLM or any other , and you use like '%elm%' , it will list all the matching values.
I cant say about earlier versions of mysql.
If you go in Oracle , like work as case-sensitive , so if you type like '%elm%' , it will go only for this and ignore uppercases..
Strange , but this is how it is :)
SELECT name
FROM gallery
WHERE CONVERT(name USING utf8) LIKE _utf8 '%$q%'
GROUP BY name COLLATE utf8_general_ci LIMIT 5
You must set up proper encoding and collation for your tables.
Table encoding must reflect the actual data encoding. What is your data encoding?
To see table encoding, you can run a query SHOW CREATE TABLE tablename
When I want to develop insensitive case searchs, I always convert every string to lower case before do comparasion
I've always solved like this:
SELECT * FROM trees WHERE LOWER( trees.title ) LIKE LOWER('%elm%');
For example if you want to search name like Raja not raja, Royal not royal etc, add BINARY before column name in WHERE clause.
SELECT name FROM person_tbl
WHERE BINARY name LIKE "R%";
Hello i have a table with colums called NAME, in this colum i can have this type of name : First name Uppercase and other lower (Jack), all name uppercase (JACK), and name with space (Jack ) or (JACK ).
How can show all name than have jack in all type ?
i need to search multiple name with IN statement
i use ORACLE
Assuming SQL Server:
SELECT Name FROM Table WHERE Name LIKE '%jack%'
SELECT Name
FROM Table
WHERE UPPER(Name) LIKE '%JACK%'
will work in all SQL. In SQL Server I believe LIKE is case insensitive but it might depend on configuration settings and collation.
or maybe you need
SELECT Name
FROM Table
WHERE UPPER(TRIM(Name)) IN ('JACK','HOGAN','VICTOR')
(replace TRIM(Name) with RTRIM(LTRIM(Name)) if not using SQL Server.)
If you have a table with the list of names you can do it like this:
SELECT Name
FROM Table
JOIN NameTable ON UPPER(RTRIM(LTRIM(Table.Name))) = NameTable.Name
If I try to create a select with special (nordic) characters like:
Select * from users where name like '%æ%'
It just selects all users instead of those containing the letter 'æ'.
Do I need to install some special drivers to the database, or is there something else I have missed?
Update:
I am using a SQL Server 2008 database, the collation is set to 'SQL_Latin1_General_CP1_CI_AS' and the datafield is a nullable nVarChar datatype.
Most likely some collation or datatype issue
Example, gives 97 and 230
SELECT ASCII('æ' COLLATE Albanian_CI_AI), ASCII('æ' COLLATE Latin1_General_CI_AS)
We'll need more info basically.
Edit: Question about Danish/Norwegian å (although unresolved)
Edit 2: change the code to this if name is nvarchar so the literal becomes unicode too.
Select * from users where name like N'%æ%'
Make sure you're using an extended character set (UTF-8) and you should be fine.
it should be work correctly ..
but try this Select * from users where name like '%[æ]%'
I have a field 'Description' which can have product descriptions with any unicode characters.
If I search for a description which contains an international character, with a LIKE condition (word searched with does not have the international character) I get the following results:
Ex: GEWÜRZTRAMINER is one of the descriptions.
When I do:
Select * from table where Description LIKE '%GEWURZTRAMINER%', it retrieves the entry.
When I do:
Select * from table where Description LIKE '%GEWURZ%', the entry is not retrieved.
(Note: the search condition does not include the Ü but has a U)
Is there a way around this so that I can retrieve with '%GEWURZ%' as well?
For bog standard varchar, you'd have to coerce to a accent insensitive collation
Select 1 where 'GEWÜRZTRAMINER' COLLATE LATIN1_GENERAL_CI_AI LIKE '%GEWURZTRAMINER%'
There should be no difference between the calls though for the SQL you provided.
It will depend on the collation order for the column. It should work if you use e.g. SQL_Latin1_General_CP1_CI_AI