58

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!

Cade Roux
  • 88,164
  • 40
  • 182
  • 265
Ash Machine
  • 9,601
  • 11
  • 45
  • 52
  • 1
    Are you concerned with how the output is displaying in Query Analyzer? If you're just using it to look over data then I wouldn't get too hung up on "error prone" and "ugly" code. If you're using it for production work then that is another issue altogether. – Tom H Jun 04 '09 at 18:23
  • Tom: I am not just looking at data. I am using the query output to generate new stored procedure calls for a remote server, so the result has to be 'perfect'. – Ash Machine Jun 04 '09 at 18:26
  • 1
    But why use SSMS to execute the query? What happens if you execute the same query from a program? – John Saunders Jun 04 '09 at 18:48
  • 1
    SSMS2008 even truncates when I select output to file. Huh!!! – IsmailS Feb 24 '11 at 13:05
  • 1
    The easiest way for me was to convert the string to xml (as seen on http://stackoverflow.com/a/2760023/278044 ). Management Studio can be configured to allow unlimited XML size. – Eldritch Conundrum Nov 09 '15 at 12:39

10 Answers10

78

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.

Torre Lasley
  • 7,243
  • 3
  • 24
  • 12
  • +1 for a solution that works without having to write a custom tool. I needed to export XML data without changing the formatting (spacing), so exporting as XML was not an option. – mlhDev Jun 18 '12 at 18:16
  • 3
    Did not work for me. Still truncating. Is there any other crucial step I might have missed (mentioned as self explanatory) ? – mythicalcoder Sep 01 '15 at 07:12
  • @Maven this will export to a file, not change anything inside the editor...are you saying it's truncating inside of the output file? Haven't seen that before... – Torre Lasley Sep 02 '15 at 23:09
  • 1
    Yes. In the output file, each of the cells with more than 4k characters are getting truncated. Anyway, I had to get the definition of each of the stored procedures via query. Got a workaround.. – mythicalcoder Sep 03 '15 at 18:34
  • 1
    The defaults during Export did not work; I manually chose the Data Source ".Net Framework Data Provider for SQL Server"... manually specified Data Source Initial Catalog, Integrated Security; and used Unicode flat file output; in my case I used `FOR JSON PATH` – Nate Anderson Aug 14 '19 at 19:18
  • I'm getting a `Source data type "22" was not found in the data type mapping file.` error when trying to export. – Sphynx Feb 22 '21 at 00:31
30

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

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • is there any way to have the non xml data shown as unlimited and waiver of the limit ? I can see that the lower limit is 30 and higher is 65535. – Pradip Jan 15 '16 at 11:43
  • @BabekoofCoder - Unfortunately not. Hence the need for the casting non xml as XML workaround. – Martin Smith Jan 15 '16 at 12:20
  • This is a good answer, but it is confusing. You just need the one line with 'XML PATH'. – N73k Nov 28 '16 at 17:21
  • 1
    Open a new query window after making changes to the query options - existing windows not affected. – Resource Feb 22 '17 at 13:26
  • select @variable for xml path('') worked great for me, bypassed the 65536 character limit I was getting. Thanks! – tsilb Mar 06 '20 at 03:43
15

Did you try this simple solution? Only 2 clicks away!

At the query window,

  1. set query options to "Results to Grid", run your query
  2. Right click on the results tab at the grid corner, save results as any files

You will get all the text you want to see in the file!!! I can see 130,556 characters for my result of a varchar(MAX) field

Just Two Clicks away!

Jenna Leaf
  • 2,255
  • 21
  • 29
11

My solution was a bit round-about but got me there (as long as the output is less than 65535 characters):

  1. In SQL Management Studio, set the limit for grid results to 65535 (Tools > Options > Query Results > SQL Server > Results to Grid > Non XML data)
  2. Run the query, output to grid
  3. Right-click the results, choose "Save Results As..." and save the results to a file
  4. Open the file in notepad or similar to get the output

UPDATE: To demonstrate that this works, here's some SQL that selects a single 100,000 character column. If I save the grid output to a csv file, all 100,000 characters are there with no truncation.

DECLARE @test nvarchar(MAX), @i int, @line nvarchar(100)
SET @test = ''; SET @i = 100
WHILE @i < 100000
BEGIN
    SET @test = @test + STUFF(REPLICATE('_', 98) + CHAR(13) + CHAR(10), 1, LEN(CAST(@i AS nvarchar)), CAST(@i AS nvarchar))
    SET @i = @i + 100
END
SELECT @test

Notes:

  1. It doesn't seem to make any difference what the character length setting is, as I orignally thought.
  2. I'm using SQL 2008 R2 (both the server and Management Studio)
  3. It doesn't seem to make a difference if the long column is stored in a local variable (as in this example), or selected from an actual table
5

I ran in to this trying to export XML. This is the solution I used:

Select the Result to Grid option, right click the link that shows up in the Results pane, then select Save Results As, choose the All Files file type, give the file a name and click Save. All the xml data is saved correctly to a file.

I'm using SSMS 10, and I could't get Torre's solution to work. The export wizard kept thinking the input column was an image:

The data type for "input column "XML_F52E2B61-18A1-11d1-B105-00805F49916B" (26)" is DT_IMAGE

Cindy Conway
  • 1,814
  • 3
  • 18
  • 19
  • I had that `DT_IMAGE` problem too but worked around it by adding another `, type`, an an outer `select() as columnname` around my XML-generating statement, and checking the Unicode checkbox in the export wizard. – Nathan Sep 29 '15 at 13:45
2

In SSMS if you select data from a row it is limited to a small number of characters, but if you Edit data from a row, the full value will be there. It might not always be there but if you ctrl-a, ctrl-c then past it in an editor it will all be there.

Larry K
  • 21
  • 2
0

Another workaround , use HeidiSql for this tricky queries. It does not have the limits in the field lenght.

cpsaez
  • 314
  • 1
  • 11
0

I usually use XML to get huge debug string as output (using test harness from Luke):

    declare @test nvarchar(max), @i int, @line nvarchar(100)
    set @test = ''; set @i = 100
    while @i < 100000
    begin
        set @test = @test + stuff(replicate('_', 98) + char(13) + char(10), 1, len(cast(@i as nvarchar)), cast(@i as nvarchar))
        set @i = @i + 100
    end
    -- ctrl+d for "results to grid" then click the xml output
    --select cast('<root>' + @test + '</root>' as xml)

-- revised
select @test for xml path(''), type;
nathan_jr
  • 9,092
  • 3
  • 40
  • 55
0

The truncation you are talking about only happens in Management Studio. If you pull the column into another app, it will not be truncated.

There's no way you're using Query Analyzer to talk to SQL Server 2005. Do you mean Management Studio?

Bridge
  • 29,818
  • 9
  • 60
  • 82
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • I think he does mean Management Studio. If you switch to "Results to Grid", it shows the max per column as 65535. Not sure if that will get you any closer, though - sounds like you're trying to code-gen in SQL? – GalacticCowboy Jun 04 '09 at 18:40
  • Sorry, I do mean Management Studio. – Ash Machine Jun 04 '09 at 18:45
0

If given a choice I would have the query return the data as "For XML Auto" or "For XML Raw" or "For XML explicit" that way the limitations are much higher and you can do much more with the outputed results.

Avitus
  • 15,640
  • 6
  • 43
  • 53