Accent sensitive matches in MySQL - mysql

MySQL table is recognizing cód and cod as same. How can i avoid this problem.
The table is COLLATE='utf8_general_ci'

You should look for the characters set, tablename, and column names of the fields you want to use
Make sure they are set to:
COLLATE='utf8_bin'
Make sure you don't put the _ci at the end as it stands for case insensitive
If you can't get to the table and db in the database, you can use it on your queries:
SELECT * FROM tablex WHERE LOWER(column) = 'cód' COLLATE utf8_bin

Related

Are WHERE clause in Mysql not case-sensitive?

is where clause not case-sensitive??
i tried using different versions of query for the table data:
username(TEXT) pass(TEXT)
admin admin
The above table has only one entry
SELECT pass FROM lbdb_user WHERE username = "Admin";
SELECT pass FROM lbdb_user WHERE username = "admiN";
SELECT pass FROM lbdb_user WHERE username = "admin ";
all the above queries that i ran produced the same result.
Mysql collate, searching case sensitive
Usually our mysql queries are not case sensitive. In order to query case sensitive, you can use the mysql COLLATE clause.
The collate clause lets you specify a collation, which basically is a set of rules for comparing characters in a given character set.
The suffixes ci, cs, bin of a collation stand for case insensitive, case sensitive and binary, respectively. A binary collation such as utf8_bin is case sensitive as well since it compares the characters based on their numeric values.
SELECT * FROM users WHERE name like 'cRaZy' COLLATE utf8_bin;

MySQL transform case-sensitive unique field into unique case-insensitive field

This is an interesting challenge question as there can be multiple ways to solve this problem :)
I have an ID column that is unique but case-sensitive, ex:
----- ID ------
0018000001K6dkh -> record 1
0018000001K6dkH -> record 2 (different from record 1)
As MySQL is case insensitive in utf8, it is considering the two ID values as identical:
SELECT COUNT(*) FROM table WHERE id='0018000001K6dkh' //returns 2 instead of 1
SELECT COUNT(*) FROM table WHERE id='0018000001K6dkH' //returns 2 instead of 1
I need to build a MySQL function that I can apply to this column to transform all IDs into a case-insensitive and unique IDs, ex:
function('0018000001K6dkh') = something
function('0018000001K6dkH') = something different
function('0018000000ttzlB') = something even different
function('0018000000tTzlB') = something even more different
Note: I don't want to use collations, because I would have to change all the WHEREs and JOINs in my application (Laravel PHP). I want to change the IDs permanently.
According the official MySQL documentation you can set the collation on a per column basis (*see data_type section for CHAR, VARCHAR, TEXT, ENUM, and SET types).
This should change the default manner in which they are compared to string literals and each other, but I am not familiar with the rules used when comparing fields of differing collation with each other.
This is from mysql http://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html
To cause a case-sensitive comparison of nonbinary strings to be case
insensitive, use COLLATE to name a case-insensitive collation. The
strings in the following example normally are case sensitive, but
COLLATE changes the comparison to be case insensitive:
SET #s1 = 'MySQL' COLLATE latin1_bin;
SET #s2 = 'mysql' COLLATE latin1_bin;
SELECT #s1 = #s2;
return false
using #s1 COLLATE latin1_swedish_ci = #s2;
SELECT #s1 COLLATE latin1_swedish_ci = #s2;
return true
or use BINARY
SELECT COUNT(*) FROM table WHERE BINARY id='0018000001K6dkh'
SELECT COUNT(*) FROM table WHERE BINARY id='0018000001K6dkH'

Special characters select issues with MySQL

Based on http://www.i18nqa.com/debug/utf8-debug.html I want to perform a search in my MySQL table to see if I have rows that have encoding problems.
If I run the following query :
select t.col1 from table t where t.col1 like '%Ú%'
it will bring all the t.col1 that have 'as' characters in them.
How can I change the query to make it fetch only the rows containing '%Ú%', and not all that contain '%as%'.
try this if you are using collation latin1_swedish_ci
select t.col1 from table t where t.col1 regexp '^[Ú]';
With MySQL's collations, case-folding and accent-stripping go together.
If you want neither, use the ..._bin collation for the character set you are using.
WHERE foo LIKE '%Ú%' COLLATE utf8_bin
Even faster would be to declare foo to be COLLATE utf8_bin instead of whatever you have. (Note: the default for utf8 is utf8_general_ci.)

What is the consequence of COLLATE in a database or table [duplicate]

This question already has answers here:
What does character set and collation mean exactly?
(4 answers)
Closed 8 years ago.
1/ Sometime you write
ALTER DATABASE [MyDb] COLLATE SQL_Latin1_General_CP1_CI_AS
after a moment, if you try
ALTER DATABASE [MyDb] COLLATE FRENCH_CI_AS
it may not succeed. why this?
2/ if table1 is SQL_Latin1_General_CP1_CI_AS and if table2 is FRENCH_CI_AS
one cannot write ...where table1.field1= table2.field2
What is the consequence of COLLATE in a database or table ?
you can compare your query with collate name
table1.field1 = table2.field2 COLLATE FRENCH_CI_AS
otherwise Alter Column.(If you want to change field COLLATE)
ALTER TABLE [dbo].[TableName]
ALTER COLUMN [FieldName] [DataType] COLLATE [Collate Name] NOT NULL
This answer is based on experience from working with Ms Sql Server. I have only a little experience from working with other databases, but I imagine much of this would apply for them as well.
From Msdn (here):
Collations let users sort and compare strings according to their own
conventions
Collations are used to let sql server know which characters are considered the same. If a case insensitive collation is used, X and x are considered the same. If a case sensitive collation is used, they are different. In an accent insensitive collation e and é might be considered the same, while in an accent sensitive collation they are different.
At least with Ms Sql Server, if you are comparing two strings that are stored in different collations, you must tell the server which collation to use in the comparison by using the COLLATE clause already mentioned in other answers. Not sure about how this is done in other databases.
If your table contains a row with a column that contains the text ÖBc and you select from this table like so:
select COL from TBL where COL = 'obc'
Then the row will be found using a case and accent insensitive collation like latin1_general_ci_ai. Accent insensitivity means that O and Ö are the same, and case insensitivity takes care of the case mismatch.
With the same select and collation latin1_general_cs_ai the row will not be found because of case sensitivity.
Similarly with collation latin1_general_ci_as, the row will not be found because of accent sensitivity.
Collation is also used to determine alphabetical sort order when sorting results. This determines if upper and lower case charachers are sorted differently and also where accented charaters are sorted.
You can compare item with different collations. For SQL Server you use :
table1.field1 = table2.field2 COLLATE FRENCH_CI_AS
or
table1.field1 = table2.field2 COLLATE SQL_Latin1_General_CP1_CI_AS
(or whatever the field1 collation is).
Note: You should try and leave the collation as the server default, unless you have a specific reason to do otherwise.

Special Characters and a simple select query

I have got a problem with a simple Select Query and special chars. I want to select the name Änne.
SELECT * FROM `names` WHERE `name` = 'Änne'
utf8_general_ci
Änne
Anne
okay, ...
utf8 general ci is a very simple collation. What it does it just
removes all accents then converts to upper case and uses the code of this sort of "base letter" result letter to compare.
http://forums.mysql.com/read.php?103,187048,188748
utf8_unicode_ci
Änne
Anne
why?
utf8_bin
Änne
utf8_bin seems to be the right choice at this point, but i have to do my search case insensitiv.
SELECT * FROM `names` WHERE `name` = 'änne'
utf8_bin
none
Is there no way to do so?
I could use php ucwords() to uppercase the first letters, but i would prefer to find a DB solution.
edit: ucwords('änne') = änne, so i cant use that too
SELECT * FROM `names` WHERE lower(`name`) = 'änne'
is working for me, because i don't have a difference between 'Änne' and 'änne' in my DB.
what about:
SELECT * FROM `names` WHERE upper(`name`) = upper("änne")
Quoting doc:
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
That means that case sensitive results are because you have set a binary collation. You can set collation column to utf8_general_ci and change it on searchs:
col_name COLLATE latin1_general_cs LIKE 'a%'
There is an error in your MySQL code:
SELECT * FROM names WHERE name = "Änne"
Remove the quotes around the table name and the field name.