LENGTH Function - Oracle to MySQL Migration

In Oracle the LENGTH function returns the string length in characters.

MySQL also provides LENGTH function, but it returns the string length in bytes, so you have to convert Oracle LENGTH to MySQL CHAR_LENGTH function that returns the string length in characters.

Oracle:

  -- Regular ANSI string
  SELECT LENGTH('abc') FROM dual;
  # 3
 
  -- Length of € sign in Unicode that takes 2 bytes of storage
  SELECT LENGTH(u'\20AC') FROM dual;
  # 1

MySQL:

  -- Regular ANSI string
  SELECT CHAR_LENGTH('abc') FROM dual;
  # 3
 
  -- Length of € sign in Unicode that takes 2 bytes of storage
  SELECT CHAR_LENGTH(_utf16 x'20AC');
  # 1

Compare if you use MySQL LENGTH function for Unicode string:

MySQL:

  -- Length of € sign in Unicode in bytes
  SELECT LENGTH(_utf16 x'20AC');
  # 2

For more information, see Oracle to MySQL Migration.