SQLite: execute case-sensitive LIKE query on a specific query - php

I want to run a SELECT ... LIKE query in SQLite that is case-sensitive. But I only want this one query to be case sensitive, and nothing else.
I know there is
PRAGMA case_sensitive_like = boolean;
But that seems to change all LIKE queries.
How can I enable case-sensitive LIKE on a single query?
Examples:
I want a query for "FuN" to match "blah FuN blah", but not "foo fun bar".
(This is running under PHP using PDO)
I might be able to toggle that on, then off after the query but I can concerned about the repercussions that may have (efficiency etc). Is there any harm?
I don't have write access to the database.
(This is under Windows Server 2008)
I also tried SELECT id, summary, status FROM Tickets WHERE summary COLLATE BINARY LIKE '%OPS%'; but that did not do a case-sensitive SELECT, it still returned results returns like laptops.

Why not go the simple way of using
PRAGMA case_sensitive_like = true/false;
before and after each query you want to be case sensitve? But beware- case sensitivity does only work for ASCII characters, not Unicode which makes SQlite not fully UC-compliant at this time.
Alternatively, SQlite allows applications to implement the REGEXP operator which might help according to www.sqlite.org/lang_expr.html.

Try:
SELECT id, summary, status FROM Tickets WHERE summary GLOB \"*OPS*\";
there is no space between *and OPS.

I think you may need to do a seperate check in your php code on returned value to see if they match your case-sensitive data.
$rs = mysql_query("Select * from tbl where myfield like '%$Value%'");
while($row == mysql_fetch_assoc($rs))
{
if (strpos($row['myfield'],$Value) !== false)
{
$Matches[] = $row;
}
}
print_R($Matches);

You can try something like this:
SELECT YOUR_COLUMN
FROM YOUR_TABLE
WHERE YOUR_COLUMN
COLLATE latin1_general_cs LIKE '%YOUR_VALUE%'
Not sure what your collation set is on the column. I picked latin as an example. Run the query and change 'cs' to 'ci' at the end. You should see different results.
UPDATE
Sorry. read the question too fast. The above collation is for mysql. For SQLLite, you should be able to use BINARY which should give you case sensitive search.
ref: http://www.sqlite.org/datatype3.html#collation

You can do that per column, not per query (which may be your case). For this, use sqlite collations.
CREATE TABLE user (name VARCHAR(255) COLLATE NOCASE);
All LIKE operations on this column then will be case insensitive.
You also can COLLATE on queries even though the column isn't declared with a specific collation:
SELECT * FROM list WHERE name LIKE '%php%' COLLATE NOCASE
Note that only ASCII chars are case insensitive by collation. "A" == "a", but "Æ" != "æ"
SQLite allows you to declare new collation types with sqlite_create_collation and them implement the collation logic on the PHP side, but PDO doesn't expose this.

SELECT * FROM table WHERe field LIKE '%search_term%'
In this form the SELECT is case insensitive.

Related

->where() clauses seem to be case sensitive

I have a query like so:
$profilesx->where('qZipCode', $location)->
orWhere('qCity', 'LIKE', '%'.$location.'%');
Where location is equal to belgrade, and the database column says Belgrade.
It seems to be case sensitive (using either = or LIKE) so if I search for Belgrade I get a result but if I search for belgrade I do not get any results.
How to make it case insensitive?
The default character set and collation are latin1 and
latin1_swedish_ci, so nonbinary string comparisons are case
insensitive by default. This means that if you search with col_name
LIKE 'a%', you get all column values that start with A or a. To make
this search case sensitive, make sure that one of the operands has a
case sensitive or binary collation. For example, if you are comparing
a column and a string that both have the latin1 character set, you can
use the COLLATE operator to cause either operand to have the
latin1_general_cs or latin1_bin collation:
source: http://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html
What's actually happened is that the case sensitivity has been switched off (which is not neccassarily a bad thing). The solution is given in the next section of that document. Try something like
orWhere('qCity', 'COLLATE latin1_general_cs LIKE', '%'.$location.'%');
If laravel doesn't like it you will have to use a raw query or change the collation setting for the column.
As a side note, try to avoid LIKE %something% queries if you can. Mysql cannot use an index for these sorts of queries and they generally tend to be slow on large tables because of it.
This is usually the collation settings for the database. You need to set it to a case insensitive collation type.
Read this link for more info:
http://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html

MYSQL Email Validation with Regex not working

i just want to fetch the invalid email addresses from my database, i tried with the following query, but its not working
$sql=mysql_query("SELECT * FROM mytable WHERE email!='' and email NOT REGEXP '^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$'");
And the invalid Email is a.bcdefg-3#abccom
It looks like the Richard answer is correct however, it may not works given the collation used.
Therefore, if you have a case sensitive collation, you may want to lowercase your field.
Try this query :
SELECT * FROM `mytable` WHERE `email` NOT REGEXP '^[[:alnum:]._%-\+]+#[[:alnum:].-]+[.][[:alnum:]]{2,4}$';
I have updated the regex to use character classes instead of character range to avoid lower (or upper) case transformation.
Moreover, in some IDE, you may have to escape "." with two backslashes, therefore I use
[.]
instead of escaped dot.
I updated again to allow subdomains.
Edited to allow +, thanks to #Charlie Brumbaugh comment.
Try this:
SELECT * FROM `mytable` WHERE `email` NOT REGEXP '^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$';
Works for my.
Furthermore, do not use the MySQL driver as it is deprecated in PHP.

Å vs å is not matching in mysql query - how to do case insensitive match

I have a danish website developed in PHP. I am using mysqli.
I have the words like Daugård and Århus in database field called tags.
I want this both values as result when I run a query like below.
Query : select * from table_name where tags like '%år%';
Expected result : Daugård and Århus both
Actual result : Daugård
Right now its performing case-sensitive match and returning only Daugård word.
I tried changing charset to utf8 dynamically by function set_charset('utf8'), but it didn't work.
Collation for the 'tags' field is 'utf8_general_ci', table collation is 'utf8_general_ci' and my database collation is 'latin1_swedish_ci'.
Help me how can I achieve this?
To avoid the collation issue, use a case conversion function on "tags" before comparison:
select * from table_name where lcase(tags) like '%år%';
I might be missing something as MySQL isn't that familiar to me. I know the function LOWER(tags) does the job in Oracle as long as the pattern searched for also is in lower case.

How to get all database having upper case character after some word using MYSQL

I want to list all database which has the common starting word and after it should have uppercase letters. (using MYSQL)
eg:
test_vino_JY
test_vino_JI
test_vino_ij
test_vino_klm
In the above example i want to list only test_vino_JY, test_vino_JI
May i know how to do that. I tried using the below query, its not working. Please help me on this.
SHOW DATABASES WHERE `Database` REGEXP '^test_vino_+[A-Z]';
show doesn't accept regexes, it only accepts a show foo like '%...%' wildcard-type matches. You'll have to select against the information_schema pseudo-db:
SELECT *
FROM information_schema.schemata
WHERE SCHEMA_NAME REGEXP '...';

how to write Regular Expression in MySQL select queries?

I tried this expression /\b(word\w*)\b/i to compare a word against the list of other words to find the duplicates. I used preg_math_all() and it worked fine. I wanted to do the same thing but this time check against the words retrieved from mysql database. This is what i wrote
SELECT * FROM table WHERE word = /\b(word\w*)\b/i
this didnt worked.
Your query should look like:
SELECT * FROM table WHERE column REGEXP '/\b(word\w*)\b/i'
MySQL regex syntax is a different from PHP's, you don't need the delimiters at the start and end.
SELECT * FROM table WHERE word REGEXP '\b(word\w*)\b'
You can't use the i flag for case insensitivity the same way as in PHP, as in MySQL whether the regex is case sensitive or not depends on whether the column collation is a case sensitive one or not.

Categories