What is the best way to store a large amount of text in a SQL server table?

Sql Server

Sql Server Problem Overview


What is the best way to store a large amount of text in a table in SQL server?

Is varchar(max) reliable?

Sql Server Solutions


Solution 1 - Sql Server

In SQL 2005 and higher, VARCHAR(MAX) is indeed the preferred method. The TEXT type is still available, but primarily for backward compatibility with SQL 2000 and lower.

Solution 2 - Sql Server

I like using VARCHAR(MAX) (or actually NVARCHAR) because it works like a standard VARCHAR field. Since it's introduction, I use it rather than TEXT fields whenever possible.

Solution 3 - Sql Server

Varchar(max) is available only in SQL 2005 or later. This will store up to 2GB and can be treated as a regular varchar. Before SQL 2005, use the "text" type.

Solution 4 - Sql Server

According to the text found here, varbinary(max) is the way to go. You'll be able to store approximately 2GB of data.

Solution 5 - Sql Server

Split the text into chunks that your database can actually handle. And, put the split up text in another table. Use the id from the text_chunk table as text_chunk_id in your original table. You might want another column in your table to keep text that fits within your largest text data type.

CREATE TABLE text_chunk (
     id NUMBER,
     chunk_sequence NUMBER,
     text BIGTEXT)

Solution 6 - Sql Server

In a BLOB

BLOBs are very large variable binary or character data, typically documents (.txt, .doc) and pictures (.jpeg, .gif, .bmp), which can be stored in a database. In SQL Server, BLOBs can be text, ntext, or image data type, you can use the text type

text

Variable-length non-Unicode data, stored in the code page of the server, with a maximum length of 231 - 1 (2,147,483,647) characters.

Solution 7 - Sql Server

Depending on your situation, a design alternative to consider is saving them as .txt file to server and save the file path to your database.

Solution 8 - Sql Server

Use nvarchar(max) to store the whole chat conversation thread in a single record. Each individual text message (or block) is identified in the content text by inserting markers.

Example:

{{UserId: Date and time}}<Chat Text>. 

On display time UI should be intelligent enough to understand this markers and display it correctly. This way one record should suffice for a single conversation as long as size limit is not reached.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrunoView Question on Stackoverflow
Solution 1 - Sql ServerJohn RudyView Answer on Stackoverflow
Solution 2 - Sql ServerStephen WrightonView Answer on Stackoverflow
Solution 3 - Sql ServerInstantsoupView Answer on Stackoverflow
Solution 4 - Sql ServerHuuuzeView Answer on Stackoverflow
Solution 5 - Sql ServerMark StockView Answer on Stackoverflow
Solution 6 - Sql ServerPaul WhelanView Answer on Stackoverflow
Solution 7 - Sql ServerGreat EfueView Answer on Stackoverflow
Solution 8 - Sql Serveruser9018039View Answer on Stackoverflow