Query gives #1305 - FUNCTION database-name.LEN does not exist; WHY?
MySQL Error 1305: 'FUNCTION database-name.LEN does not exist' - Understanding and Resolution

Encountering MySQL Error 1305 when using LEN() can be confusing. This article explains why this error occurs, particularly in the context of database-specific function calls, and provides solutions for proper string length calculation in MySQL.
The MySQL Error 1305, 'FUNCTION database-name.LEN does not exist', is a common pitfall for developers, especially those migrating from other SQL dialects or unfamiliar with MySQL's specific function names. This error indicates that the database server cannot find a function named LEN
within the specified database context. Unlike some other SQL databases (like SQL Server or Access), MySQL does not have a built-in LEN()
function. Instead, it uses LENGTH()
or CHAR_LENGTH()
for calculating string lengths.
Why LEN() Doesn't Work in MySQL
The primary reason for this error is a difference in SQL function naming conventions across various database systems. While LEN()
is a standard function in T-SQL (Microsoft SQL Server) and MS Access SQL for returning the length of a string, MySQL uses different function names to achieve the same result. When you execute a query containing LEN()
against a MySQL database, the server attempts to locate a function with that exact name. Since it doesn't exist in its internal function catalog, it throws the 1305 error.
flowchart TD A[SQL Query with LEN()] --> B{MySQL Server Receives Query} B --> C{Server Checks Function Catalog} C -- 'LEN()' Not Found --> D[Error 1305: FUNCTION ...LEN does not exist] C -- 'LENGTH()' or 'CHAR_LENGTH()' Found --> E[Function Executed Successfully]
Flowchart illustrating why MySQL throws Error 1305 for LEN()
The Correct MySQL Functions: LENGTH() and CHAR_LENGTH()
MySQL provides two main functions for determining the length of a string, each with a slightly different behavior regarding character encoding:
LENGTH(str)
: This function returns the length of the stringstr
in bytes. This is important for multi-byte character sets (like UTF-8), where a single character might occupy more than one byte.CHAR_LENGTH(str)
: This function returns the length of the stringstr
in characters. This is generally what users expect when they think of 'length', as it counts each character regardless of how many bytes it occupies. For single-byte character sets,LENGTH()
andCHAR_LENGTH()
will return the same value.
-- Incorrect query that would cause Error 1305
SELECT LEN('Hello World');
-- Correct query using LENGTH() (returns length in bytes)
SELECT LENGTH('Hello World');
-- Correct query using CHAR_LENGTH() (returns length in characters)
SELECT CHAR_LENGTH('Hello World');
-- Example with multi-byte characters (e.g., UTF-8)
SELECT LENGTH('你好世界'), CHAR_LENGTH('你好世界');
Demonstration of incorrect and correct string length functions in MySQL
CHAR_LENGTH()
when you need the logical character count of a string, especially if your database or application handles multi-byte character sets like UTF-8. LENGTH()
is useful when you need to know the actual byte size of the string data.Resolving the Error: Practical Steps
To fix the 'FUNCTION ...LEN does not exist' error, you simply need to replace LEN()
with the appropriate MySQL function (LENGTH()
or CHAR_LENGTH()
) in your SQL queries or application code. This often involves reviewing your SQL statements, stored procedures, triggers, or any ORM (Object-Relational Mapping) configurations that might be generating LEN()
calls.
1. Identify the problematic query
Locate the SQL query, stored procedure, or application code that is generating the LEN()
function call. This might be directly in your SQL files, within an ORM configuration (e.g., Hibernate, Entity Framework), or in a database migration script.
2. Choose the correct MySQL function
Decide whether you need LENGTH()
(byte count) or CHAR_LENGTH()
(character count). For most common use cases, CHAR_LENGTH()
is the desired function.
3. Replace LEN() with the chosen function
Modify the identified SQL statement or code to use LENGTH()
or CHAR_LENGTH()
instead of LEN()
. Ensure that the syntax is correct for MySQL.
4. Test your application
After making the change, thoroughly test your application or query to ensure that the string length calculations are correct and that the error no longer appears.