310

I have variable length character data and want to store in SQL Server (2005) database. I want to learn some best practices about how to choose TEXT SQL type or choose VARCHAR SQL type, pros and cons in performance/footprint/function.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
George2
  • 44,761
  • 110
  • 317
  • 455
  • 18
    If Google sent you here: the [MSDN SQL Data Types page](http://msdn.microsoft.com/en-us/library/ms187752.aspx) may help. – Jeroen Nov 02 '12 at 09:11

4 Answers4

301

TEXT is used for large pieces of string data. If the length of the field exceeed a certain threshold, the text is stored out of row.

VARCHAR is always stored in row and has a limit of 8000 characters. If you try to create a VARCHAR(x), where x > 8000, you get an error:

Server: Msg 131, Level 15, State 3, Line 1

The size () given to the type ‘varchar’ exceeds the maximum allowed for any data type (8000)

These length limitations do not concern VARCHAR(MAX) in SQL Server 2005, which may be stored out of row, just like TEXT.

Note that MAX is not a kind of constant here, VARCHAR and VARCHAR(MAX) are very different types, the latter being very close to TEXT.

In prior versions of SQL Server you could not access the TEXT directly, you only could get a TEXTPTR and use it in READTEXT and WRITETEXT functions.

In SQL Server 2005 you can directly access TEXT columns (though you still need an explicit cast to VARCHAR to assign a value for them).

TEXT is good:

  • If you need to store large texts in your database
  • If you do not search on the value of the column
  • If you select this column rarely and do not join on it.

VARCHAR is good:

  • If you store little strings
  • If you search on the string value
  • If you always select it or use it in joins.

By selecting here I mean issuing any queries that return the value of the column.

By searching here I mean issuing any queries whose result depends on the value of the TEXT or VARCHAR column. This includes using it in any JOIN or WHERE condition.

As the TEXT is stored out of row, the queries not involving the TEXT column are usually faster.

Some examples of what TEXT is good for:

  • Blog comments
  • Wiki pages
  • Code source

Some examples of what VARCHAR is good for:

  • Usernames
  • Page titles
  • Filenames

As a rule of thumb, if you ever need you text value to exceed 200 characters AND do not use join on this column, use TEXT.

Otherwise use VARCHAR.

P.S. The same applies to UNICODE enabled NTEXT and NVARCHAR as well, which you should use for examples above.

P.P.S. The same applies to VARCHAR(MAX) and NVARCHAR(MAX) that SQL Server 2005+ uses instead of TEXT and NTEXT. You'll need to enable large value types out of row for them with sp_tableoption if you want them to be always stored out of row.

As mentioned above and here, TEXT is going to be deprecated in future releases:

The text in row option will be removed in a future version of SQL Server. Avoid using this option in new development work, and plan to modify applications that currently use text in row. We recommend that you store large data by using the varchar(max), nvarchar(max), or varbinary(max) data types. To control in-row and out-of-row behavior of these data types, use the large value types out of row option.

Community
  • 1
  • 1
Quassnoi
  • 413,100
  • 91
  • 616
  • 614
  • 2
    1. "If you do not search on the value of the column " -- could you show me what do you mean "search"? You mean select this column, order this column, LIKE this column or using some string manipulation function on this column? – George2 Feb 19 '09 at 12:53
  • 2
    2. "VARCHAR is always stored in row and has a limit of 8000 characters." -- sorry I do not agree with you. VARCHAR could be longer than 8000 and if longer than 8000, VARCHAR will be stored other than in columns. Any comments? – George2 Feb 19 '09 at 12:54
  • 1
    3. Mladen Prajdic mentioned in this thread, TEXT type is deprecated, but I do not find any documents covers this. Do you have any documents covers this? – George2 Feb 19 '09 at 12:55
  • 2
    Cool Quassnoi! You are so knowlegeable! :-) One more question -- "This of course does not concern VARCHAR(MAX), which is as for SQL SERVER 2005 a synonym for TEXT." "This" you mean what? – George2 Feb 19 '09 at 13:33
  • "This of course does not concern VARCHAR(MAX), which is as for SQL SERVER 2005 a synonym for TEXT." -- do you have any documents which says TEXT is the same as VARCHAR in SQL Server 2005? I did some search but can not find official documents. :-) – George2 Feb 19 '09 at 13:34
  • BTW: I do not think TEXT and VARCHAR are exactly the same, since TEXT could be longer than 8000 characters. Any comments? – George2 Feb 19 '09 at 13:35
  • VARCHAR and VARCHAR(MAX) are very different types. TEXT and VARCHAR(MAX) are same in terms of how are stored, but different in terms of how you can access them. – Quassnoi Feb 19 '09 at 13:43
  • Of course TEXT and VARCHAR(8000) are not the same. You cannot create a VARCHAR(8001), for instance, you'll need VARCHAR(MAX). – Quassnoi Feb 19 '09 at 13:51
  • Sorry, I am confused. I always think there is only one data type in SQL Server called VARCHAR, so VARCHAR and VARCHAR(MAX) are the same. Am I wrong? :-( Could you provide some links or documents about this topic please? – George2 Feb 19 '09 at 15:14
  • "but different in terms of how you can access them" -- what do you mean access? Could you show me an example please? :-) – George2 Feb 19 '09 at 15:14
  • VARCHAR and VARCHAR(MAX) are different types IN TERMS OF HOW THEY ARE STORED. As for TEXT values, you cannot do UPDATE table SET text_column = integer_column, you'll need to do UPDATE table SET text_column = CAST(integer_column AS VARCHAR) – Quassnoi Feb 19 '09 at 15:31
  • since SQL server 2005 text has been "replaced" with varchar(max) or nvarchar(max), so the answer is based on a fallacy. Sorry. – Nikos Steiakakis Apr 23 '10 at 09:24
236

If you're using SQL Server 2005 or later, use varchar(MAX). The text datatype is deprecated and should not be used for new development work. From the docs:

Important

ntext , text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Mladen Prajdic
  • 15,457
  • 2
  • 43
  • 51
  • 3
    Thanks Mladen, I am surprised to see TEXT is deprecated. Do you have any official documents mentioning this? – George2 Feb 19 '09 at 12:26
  • 1
    While this is not "official" it does cover the basics. Text is in fact depreciated & also does not support everything that varchar(max) does, like ability to search & index. http://blog.sqlauthority.com/2007/05/26/sql-server-2005-replace-text-with-varcharmax-stop-using-text-ntext-image-data-types/ – achinda99 Feb 19 '09 at 13:58
  • 33
    this is as offcial as it gets :) http://msdn.microsoft.com/en-us/library/ms187993.aspx – Mladen Prajdic Feb 19 '09 at 14:01
  • 1
    Cool achinda99 and Mladen Prajdic! What you provided is what I am looking for. :-) One more question, how do we choose whether to use VARCHAR or VARCHAR(MAX) in different situations? – George2 Feb 19 '09 at 15:29
  • 1
    Official MS info about it is deprecated: http://msdn.microsoft.com/en-us/library/ms187993%28v=sql.90%29.aspx – Fanda Apr 29 '13 at 07:50
  • Hmm... even the official docs linked above don't specify *why* this was removed. Any idea? I'm curious. – exhuma Nov 02 '16 at 14:13
  • because the architecture was pretty much obsolete. (max) data types are more optimized for current times – Mladen Prajdic Nov 03 '16 at 13:11
46

In SQL server 2005 new datatypes were introduced: varchar(max) and nvarchar(max) They have the advantages of the old text type: they can contain op to 2GB of data, but they also have most of the advantages of varchar and nvarchar. Among these advantages are the ability to use string manipulation functions such as substring().

Also, varchar(max) is stored in the table's (disk/memory) space while the size is below 8Kb. Only when you place more data in the field, it's is stored out of the table's space. Data stored in the table's space is (usually) retrieved quicker.

In short, never use Text, as there is a better alternative: (n)varchar(max). And only use varchar(max) when a regular varchar is not big enough, ie if you expect the string that you're going to store will exceed 8000 characters.

As was noted, you can use SUBSTRING on the TEXT datatype,but only as long the TEXT fields contains less than 8000 characters.

Ricardo Sanchez
  • 4,935
  • 11
  • 56
  • 86
edosoft
  • 17,121
  • 25
  • 77
  • 111
  • 1
    Thanks Edoode, you answered quite whole how good VARCHAR is, but any comments or ideas about when to use VARCHAR and when to use TEXT? My question is about choosing 1 from 2 issue. :-) – George2 Feb 19 '09 at 12:43
  • 1
    Actually, in MS SQL Server 2005 you can use SUBSTRING and other functions on TEXT columns too. – Quassnoi Feb 19 '09 at 14:08
  • 1
    Thanks Quassnoi! Looks like TEXT is deprecated. One more question, how do we choose whether to use VARCHAR or VARCHAR(MAX) in different situations? – George2 Feb 19 '09 at 15:30
  • 1
    Only use varchar(max) when a regular varchar is not big enough (8Kb should be enough for everybody ;) – edosoft Feb 20 '09 at 08:58
7

There has been some major changes in ms 2008 -> Might be worth considering the following article when making a decisions on what data type to use. http://msdn.microsoft.com/en-us/library/ms143432.aspx

Bytes per

  1. varchar(max), varbinary(max), xml, text, or image column 2^31-1 2^31-1
  2. nvarchar(max) column 2^30-1 2^30-1
Draz
  • 178
  • 2
  • 4