Common SQL Server Problems: Invalid Length


 

This is another part in my series designed to offer guidance around common issues in SQL Server. Today, let’s talk about the all-too-common error: invalid length.

What Is the Invalid Length Error in SQL Server? Msg 537, Level 16, State 3 Invalid length parameter passed to the LEFT or SUBSTRING function.

The invalid length error, as shown above, occurs when you have incorrect or unexpected input to a string function. For example:

DECLARE @FirstName nvarchar(32) = N’frank’; SELECT LEFT(@FirstName, -1);

While it’s unlikely you’d intentionally pass a negative value here, the desired substring often comes from some other calculation. For