// strings reference

SQL string length limits

Every engine caps strings somewhere, and the caps apply at surprising places: literals, concatenations and aggregate results, not just columns.

类别
Strings
Returns
string
In this category
70 of 172

语法

VARCHAR2(32767)
nvarchar(max)
TEXT

Each block is a separate form. The support table and the examples show which engine takes which.

引擎支持

引擎Support备注
SQL Server 是 Supported under this name.
PostgreSQL 是 Supported under this name.
MySQL 是 Supported under this name.
SQLite 是 Supported under this name.
Oracle 是 Supported under this name.

示例

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

值得了解

  • 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.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

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.