22011
PostgreSQLERRORNotableData ExceptionHIGH confidence
substring error
What this means
SQLSTATE 22011 is raised when arguments to the SUBSTRING function are invalid — for example, a negative or zero length argument in contexts where the standard requires validation.
Why it happens
- 1Invalid from/for arguments to SUBSTRING that violate the SQL standard length requirements
How to reproduce
SUBSTRING with an invalid length argument.
trigger — this will error
trigger — this will error
SELECT SUBSTRING('hello' FROM 2 FOR -1);expected output
ERROR: negative substring length not allowed
Fix
Ensure SUBSTRING arguments are non-negative
WHEN When using SUBSTRING with computed arguments.
Ensure SUBSTRING arguments are non-negative
SELECT SUBSTRING('hello' FROM 2 FOR GREATEST(len_val, 0));Why this works
GREATEST clamps the length to 0 or above. A FOR 0 returns an empty string, which is usually safer than an error.
Sources
Official documentation ↗
Class 22 — Data Exception
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev