Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server truncation and 8192 limitation

In SQL Server 2005 I am trying to query a varchar(MAX) column which has some rows with text data that exceed the 8192. Yet, In Management Studio I have under Tools --> Options --> Query Results --> Results to Text --> Max numbers of characters displayed in each column = 8192, which is a maximum. Accordingly, it seems the truncation on these rows occurs only due to the limitation imposed by text output.

The only thing I see to get around this is to use a SUBSTRING function to grab say the first 8000 chars, then the next 8000 chars etc. etc. But this is ugly and error prone.

I should mention that SSIS and BCP are not options for me.

Does anyone have a better suggestion? Thanks!

like image 797
Ash Machine Avatar asked Jun 04 '09 18:06

Ash Machine


People also ask

What is Truncation in SQL?

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition to its data, use the DROP TABLE statement.

What are the limitations of SQL Server?

Limitations of SQL Server Express:1GB maximum memory used by the database engine. 10GB maximum database size. 1MB maximum buffer cache. CPU the lesser of one (1) socket or four (4) cores (number of SQL user connections NOT limited)

How do I increase the number of characters in SQL?

Right click in the query editor and select Query Options. Under Results, select Text. Uncheck “Include column headers in the result set” and change the maximum number of characters displayed to 8192. Click on in the editor and click Results To and choose Results to Text and click OK.


2 Answers

You can export the data to a flat file which will not be truncated. To do this:

  1. Right click the Database
  2. Click Tasks -> Export Data
  3. Select your Data Source (defaults should be fine)
  4. Choose "Flat File Destination" for the Destination type.
  5. Pick a file name for the output.
  6. On the "Specify Table Copy or Query", choose "Write a query to specify the data to transfer"
  7. Paste in your query

Remaining steps should be self explanatory. This will output the file to text and you can open it in your favorite text editor.

like image 109
Torre Lasley Avatar answered Sep 20 '22 22:09

Torre Lasley


I also use XML but a slightly different method that gets around most of the issues with XML entitisation.

declare @VeryLongText nvarchar(max) = '';  SELECT top 100 @VeryLongText = @VeryLongText + '  ' + OBJECT_DEFINITION(object_id)  FROM sys.all_objects  WHERE type='P' and is_ms_shipped=1  SELECT LEN(@VeryLongText)  SELECT @VeryLongText AS [processing-instruction(x)] FOR XML PATH('')  PRINT @VeryLongText /*WILL be truncated*/ 

Make sure that the "XML data" limit in SSMS is set sufficiently high!

Screenshot

like image 26
Martin Smith Avatar answered Sep 22 '22 22:09

Martin Smith