Skip to Main Content
  • Questions
  • Confused about the MAX size of varchar2

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Kobus.

Asked: February 26, 2013 - 6:35 am UTC

Last updated: February 26, 2013 - 11:18 am UTC

Version: 11.2.0

Viewed 100K+ times! This question is

You Asked

Hi Tom!

I've only worked with MS SQL Server in the past and only recently started with Oracle and must admit your knowledge/advice on this site always gets me past a problem.

I have something I want to ask though, not a problem, more a misunderstanding of the Varchar2 type maybe. Here goes:

I recently implemented a split function to help me with strings being sent from ASP.net, in order to loop through the string (using a delimiter) and do whatever with the values. (there might be a better way in oracle 11g now, since what I implemented came from 2004, but it works great nonetheless)

Now my question is this:

Everywhere I read I'm told the varchar2 max storage value is 4000 bytes or characters, thus creating a table with a varchar2 column can be a max of 4000 bytes/characters, which i understand.

This doesn't seem to be true for varchar2 variables in stored procs / functions though? The max size you can create a variable with is in terms of the numeric range 1 - 32767. Being useful only up to a point where you want to store a 4000+ varchar2 variable in a varchar2(4000) column.

My question: How come a varchar2 column in a table has a limit of 4000 chars, while the variable equal can go up to 32767? Is it simply a matter of Oracle allowing you to do string manipulation to a higher level "in memory", but limits you to storing only 4000 for the sake of, what, performance or physical database size?

Sorry if it's a question that won't contribute to the real knowledge base, curiosity got the better of me.

Thanks!

and Tom said...

In the past, in version 7.3 and before, varchars were limited to 255 bytes (not characters - byte, in a multibyte character set you might be able to only store a few characters - far fewer than 255).

Starting in 8.0 - the limit was raised to 4000 bytes (again, not characters - in a multibyte character set you might be only able to store a few hundred characters in a varchar2(4000)).

In plsql, the limit for strings was 32k (and if you use clobs - virtually unlimited, as is the database type clob)

It was a mismatch, plsql gave us longer strings for working with clobs and to some extent the LONG datatype.

It is a block storage/performance issue. Storing large pieces of text on a database block is not necessarily a good idea. That is why clobs (also introduced in 8.0) store the data of text that exceeds 4000 bytes "out of line" - not on the block itself.

I can say that soon, this mismatch will go away. Varchar2's in the database will have the same limits as they do in plsql. Also - if the text data exceeds 4000 bytes - the data will not be stored in line on the block, but rather out of line much like a clob is today.

Rating

  (6 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

4000+ for varchar2 table column

Ranjan, February 26, 2013 - 11:09 am UTC

Hi Tom,

You explained well.

I didnt understand below thing.

"I can say that soon, this mismatch will go away. Varchar2's in the database will have the same limits as they do in
plsql."

Is that for any 11 g latest release or 12c oracle version or
you meant to say oracle may change in new versions??
I know so far the size of varchar2 for column is 4000!

Regards,
Ranjan

Tom Kyte
February 26, 2013 - 11:18 am UTC

all I can say is that in 11g and below, the limit is definitely 4000 bytes whereas plsql is 32k. And, this mismatch will go away (future tense) - soon.

:) ,thank You Tom.

Ranjan, February 26, 2013 - 12:37 pm UTC


Kobus du Toit, February 27, 2013 - 1:02 am UTC


12c increases DB VARCHAR2 limit to match PL/SQL

Simon Kissane, November 05, 2013 - 1:14 am UTC

Tom alluded to (at the time) future plans to increase the size limit of VARCHAR2 columns in the DB to match that of PL/SQL. And now with 12c, the fruits of those plans have been released. See "1.1.6.4 Increased Size Limit for VARCHAR2, NVARCHAR2, and RAW Data Types" in http://docs.oracle.com/cd/E16655_01/server.121/e17906/chapter1.htm#FEATURENO09739

What happened to this nice plan to increase varchar2 size in 12c?

Stephan Eichenlaub, March 14, 2014 - 3:01 pm UTC

Either I missed some configuration or Oracle 12c is not yet capable of longer VARCHAR2. Doing a few simple tests:

select * from v$version;
=> Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
etc.

create table a (word varchar2 (4001));
=> SQL Error: ORA-00910: specified length too long for its datatype
00910. 00000 - "specified length too long for its datatype"
*Cause: for datatypes CHAR and RAW, the length specified was > 2000;
otherwise, the length specified was > 4000.
*Action: use a shorter length or switch to a datatype permitting a
longer length such as a VARCHAR2, LONG CHAR, or LONG RAW

create table a (grp number, word varchar2 (4000));
insert into a (grp, word) select 1, lpad ('1', 2000,'#') from dual;
insert into a (grp, word) select 1, lpad ('2', 2000,'#') from dual;
insert into a (grp, word) select 1, lpad ('3', 2000,'#') from dual;

=> table A created.
1 rows inserted.
1 rows inserted.
1 rows inserted.


SELECT grp, LISTAGG(word,',') WITHIN GROUP( ORDER BY grp) A
FROM a
GROUP BY grp
;

=> ORA-01489: result of string concatenation is too long
01489. 00000 - "result of string concatenation is too long"
*Cause: String concatenation result is more than the maximum size.
*Action: Make sure that the result is less than the maximum size.


Nothing happened with the plan to increase the varchar2 size. It's there.

Gerrat, July 21, 2014 - 2:17 pm UTC

Just as a follow up to Stephan, it is likely you missed a configuration step.

From here: ( http://docs.oracle.com/cd/E16655_01/server.121/e17209/sql_elements001.htm#SQLRF55623 ):

"Beginning with Oracle Database 12c, you can specify a maximum size of 32767 bytes for the VARCHAR2, NVARCHAR2, and RAW data types. You can control whether your database supports this new maximum size by setting the initialization parameter MAX_STRING_SIZE as follows:"


More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here