0

Consider the following sql command run in MySQL command prompt-

select 'aBc'='Abc';

O/P

+-------------+
| 'aBc'='Abc' |
+-------------+
|           1 |
+-------------+

I expected the result to show '0' in place of '1'.

How can I differentiate between two strings if they are not in the same case?

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57
  • 1
    Check this question: " How can I make SQL case sensitive string comparison on MySQL? " http://stackoverflow.com/questions/5629111/how-can-i-make-sql-case-sensitive-string-comparison-on-mysql – Yousf Oct 13 '13 at 14:34

3 Answers3

1

You can use a binary collation. For example:

select 'aBc'='Abc' collate utf8_bin;

Or you can transform one of the strings into a binary type:

select binary('aBc')=binary('Abc');

For the differences between these two, see The _bin and binary Collations in the MySQL documentation.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • showing error- `ERROR 1253 (42000): COLLATION 'utf8_bin' is not valid for CHARACTER SET 'latin1'` for the 1st command. 2nd one runs fine. – Rajesh Paul Oct 13 '13 at 14:45
  • If your client charset is set to `latin1` you can use `latin1_bin`, which is the binary collation that corresponds to the `latin1` charset. – Joni Oct 13 '13 at 15:11
  • thanx for navigating me with proper concept. – Rajesh Paul Oct 13 '13 at 15:27
1

By default, MySQL is case-insensitive. Either change column collation, or use COLLATE keyword, like:

SELECT 'abc' COLLATE 'utf8_bin' = 'abc' COLLATE 'utf8_bin'
Guillaume Poussel
  • 9,572
  • 2
  • 33
  • 42
  • It's a character set dependent answer. In my system its throwing an error- `ERROR 1253 (42000): COLLATION 'utf8_bin' is not valid for CHARACTER SET 'latin1'` – Rajesh Paul Oct 13 '13 at 14:47
0

Command1

select BINARY 'aBc'='Abc';

O/P

+---------------------+
| BINARY 'aBc'= 'Abc' |
+---------------------+
|                   0 |
+---------------------+

Command2

select BINARY 'aBc'= 'aBc';

O/P

+---------------------+
| BINARY 'aBc'= 'aBc' |
+---------------------+
|                   1 |
+---------------------+
Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57