SQL string length limits
Every engine caps strings somewhere, and the caps apply at surprising places: literals, concatenations and aggregate results, not just columns.
- Catégorie
- Strings
- Returns
string- In this category
- 70 of 172
Syntaxe
VARCHAR2(32767)
nvarchar(max)
TEXT
Each block is a separate form. The support table and the examples show which engine takes which.
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Oui | Supported under this name. |
| PostgreSQL | Oui | Supported under this name. |
| MySQL | Oui | Supported under this name. |
| SQLite | Oui | Supported under this name. |
| Oracle | Oui | Supported under this name. |
Exemples
SQL Server
SELECT LEN(REPLICATE('x', 9000)) AS cut; -- 8000
SQL Server
SELECT LEN(REPLICATE(CAST('x' AS varchar(max)), 9000)) AS full_; -- 9000
Oracle
SELECT LENGTH(LPAD('y', 9000, 'y')) FROM dual; -- 4000, cut silently
SELECT LPAD('y', 3000, 'y') || LPAD('y', 3000, 'y') FROM dual; -- ORA-01489
Bon à savoir
- SQL Server: non-max string math caps at 8000 bytes; REPLICATE('x', 9000) yields 8000 characters silently. Cast an operand to varchar(max)/nvarchar(max) to lift it.
- Oracle: VARCHAR2 in SQL caps at 4000 bytes (32767 with MAX_STRING_SIZE=EXTENDED). || past the cap raises ORA-01489, but LPAD and RPAD cut their result to 4000 without a word.
- MySQL: row and packet limits apply; max_allowed_packet caps big values, and the 65535-byte row limit is shared across all columns.
- PostgreSQL is the outlier: TEXT holds up to 1 GB with no practical concatenation limit.
Questions about String length limits
Why did my 9000-character string get cut to 8000?
SQL Server typed the expression as varchar(8000) because every operand was a plain literal. One varchar(max) cast anywhere in the expression promotes the whole result.
What is ORA-01489?
Oracle's error when a concatenated or aggregated string passes 4000 bytes. Set MAX_STRING_SIZE=EXTENDED (32767), or build a CLOB instead: TO_CLOB on one operand, or XMLAGG / JSON_ARRAYAGG ... RETURNING CLOB for aggregates.
Does VARCHAR(max) have real limits?
SQL Server's max types hold 2 GB. The limit is per value, not per expression, but literal-only expressions still default to the 8000-byte trap above.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.