How to Make SQL Case Sensitive String Comparison on MySQL

How can I make SQL case sensitive string comparison on MySQL?

http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

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:

col_name COLLATE latin1_general_cs LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_general_cs
col_name COLLATE latin1_bin LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_bin

If you want a column always to be treated in case-sensitive fashion, declare it with a case sensitive or binary collation.

Case-sensitive string comparison in SQL (MariaDB, MySQL)

This should work:

WHERE BINARY column = 'data'

However, it will not work if you put column in single quotes. That would be a string as opposed to a column reference.

MySQL LIKE is case sensitive but I don't want it to be

Case sensitivity is based on the collation of the column you are searching, defined in your CREATE TABLE, or else the collation of the session, which determines the character set and collation of string literals.

Example:

CREATE TABLE `users_user` (
`username` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

insert into users_user set username='DEMO1-0048';

Here we see the default collation of utf8mb4_general_ci is case-insensitive:

mysql> select * from users_user where username like 'DeMO1-0048';
+------------+
| username |
+------------+
| DEMO1-0048 |
+------------+

But if I force the column to use a case-sensitive collation:

mysql> select * from users_user where username collate utf8mb4_bin like 'DeMO1-0048';
Empty set (0.00 sec)

Or if I force the string literal to use a case-insensitive collation:

mysql> select * from users_user where username like 'DeMO1-0048' collate utf8mb4_bin;
Empty set (0.00 sec)

Or if I define the table with a case-sensitive collation:

CREATE TABLE `users_user` (
`username` text COLLATE utf8mb4_bin
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

insert into users_user set username='DEMO1-0048';

mysql> select * from users_user where username like 'DeMO1-0048';
Empty set (0.00 sec)

So I would infer that your table is defined with a case-sensitive collation. You can check this:

mysql> select character_set_name, collation_name from information_schema.columns where table_name='users_user' and column_name='username';
+--------------------+----------------+
| character_set_name | collation_name |
+--------------------+----------------+
| utf8mb4 | utf8mb4_bin |
+--------------------+----------------+

You can force a string comparison to be case-insensitive, even if the default collation defined for the table/column is case-sensitive.

mysql> select * from users_user where username like 'DeMO1-0048' collate utf8mb4_general_ci;
+------------+
| username |
+------------+
| DEMO1-0048 |
+------------+

This works if you use the collate option on the column too:

mysql> select * from users_user where username collate utf8mb4_general_ci like 'DeMO1-0048';
+------------+
| username |
+------------+
| DEMO1-0048 |
+------------+

Case Sensitive String Comparison

SQL Server has case sensitivity at the server, database, and column level. This is part of the collation properties. So in your example, it's likely that one or more of these settings has been set to case-insensitive.

-- Check server collation
SELECT SERVERPROPERTY('COLLATION')

-- Check database collation.
SELECT DATABASEPROPERTYEX('AdventureWorks', 'Collation') SQLCollation;

-- Check column collation
select table_name, column_name, collation_name
from information_schema.columns
where table_name = @table_name

Something like this SQL might work for you:

If (@USERNAME='rajat' COLLATE Latin1_General_CS_AS)
.....

SQL ORDER BY a string value: What is it comparing? (Case sensitive?)

In MySQL, it depends on the effective collation. Collation is the set of rules that determine the position of characters in an ordered set and what characters are considered equal, and typically involve natural language rules. For example, Spanish used to have ch as an independent letter located betwen c and d and then switched to being just individual c and h; MySQL has collations for both.

You can see available collations with these commands:

SHOW COLLATION; -- Display all
SHOW COLLATION WHERE charset = 'utf8mb4'; -- Filter by encoding









































































































CollationCharsetIdDefaultCompiledSortlenPad_attribute
utf8mb4_0900_ai_ciutf8mb4255YesYes0NO PAD
utf8mb4_0900_as_ciutf8mb4305Yes0NO PAD
utf8mb4_0900_as_csutf8mb4278Yes0NO PAD
utf8mb4_0900_binutf8mb4309Yes1NO PAD
utf8mb4_binutf8mb446Yes1PAD SPACE
utf8mb4_croatian_ciutf8mb4245Yes8PAD SPACE
utf8mb4_cs_0900_ai_ciutf8mb4266Yes0NO PAD
utf8mb4_cs_0900_as_csutf8mb4289Yes0NO PAD
utf8mb4_czech_ciutf8mb4234Yes8PAD SPACE
utf8mb4_danish_ciutf8mb4235Yes8PAD SPACE

MySQL: is a SELECT statement case sensitive?

They are case insensitive, unless you do a binary comparison.

What is the fastest case sensitive string comparision in MySQL?

I've no possibility to run a benchmark, but have you tried:

SELECT * FROM a JOIN b ON (BINARY a.text = BINARY b.text)

EDIT

Just as a sidenote: When using the BINARY operator both columns a.text and b.text must use the same character set as comparison is done on the byte-level.

Case Sensitive String Occurrence

From the documentation of mysql's LOCATE function:

This function is multibyte safe, and is case-sensitive only if at
least one argument is a binary string.

That is, you need to cast/convert your arguments to perform a case-sensitive match.

For example: If your first record is Oil is a product Ingredients are... and your second record is Oil is a product ingredients are... then the following query:

SELECT 
LOCATE('ingredients', description) AS match_both_1,
LOCATE('Ingredients', description) AS match_both_2,
LOCATE(CAST('ingredients' AS BINARY), CAST(description AS BINARY)) AS match_second,
LOCATE(CAST('Ingredients' AS BINARY), CAST(description AS BINARY)) AS match_first
FROM product

will give you the expected results:

| match_both_1 | match_both_2 | match_second | match_first |
| 18 | 18 | 0 | 18 |
| 18 | 18 | 18 | 0 |

See DEMO.



Related Topics



Leave a reply



Submit