This is weird. As per MySQL developer website -
Returns the length of the string str, measured in characters. A multibyte character counts as a single character. This means that for a string containing five 2-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
It clearly means that the output of CHAR_LENGTH(str) is independent of character set.
Now as given in -
Character String Literal Character Set and Collation
I can use introducer to SET CHARACTER SET of a String, as in -
SELECT 'abc';
SELECT _latin1'abc';
SELECT _binary'abc';
SELECT _utf8mb4'abc' COLLATE utf8mb4_danish_ci;
In my case, I take the String literal "Hello", use introducer to set it's CHARACTER SET, and use it as argument to the MySQL CHAR_LENGTH(str) function. But weirdly enough, it sometimes produces different outputs when different CHARACTER SETS are used. Example -
SELECT CHAR_LENGTH(_utf8mb4"Hello") AS character_length;
+------------------+
| character_length |
+------------------+
| 5 |
+------------------+
SELECT CHAR_LENGTH(_latin1"Hello") AS character_length;
+------------------+
| character_length |
+------------------+
| 5 |
+------------------+
SELECT CHAR_LENGTH(_ucs2"Hello") AS character_length;
+------------------+
| character_length |
+------------------+
| 3 |
+------------------+
Here, "Hello" has 5 characters. The CHARACTER SETS _utf8mb4 and _latin1 displays the number of characters correctly. But the CHARACTER SET _ucs2 oddly displays the number 0f characters to be 3.
How is this happening?