LEN Function - SQL Server to MySQL Migration

In SQL Server, the LEN function returns the length of the string in characters excluding trailing spaces.

In MySQL, you can use the CHAR_LENGTH function that includes the traling blanks, so you can also apply the RTRIM function if you want to exclude them.

SQL Server:

  -- Get the string length
  SELECT LEN('Hello');
  # 5
 
  -- Trailing blanks are not included (!)
  SELECT LEN('Hello  ');
  # 5

MySQL:

  -- Get the string length
  SELECT CHAR_LENGTH('Hello');
  # 5
 
  -- Trailing blanks are included (!)
  SELECT CHAR_LENGTH('Hello  ');
  # 7
 
  -- Excluding the trailing blanks
  SELECT CHAR_LENGTH(RTRIM('Hello  '));
  # 5

For more information, see SQL Server to MySQL Migration.