Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to print out the contents of a text field in SQL Server

I need to output the contents of a text field using MS Query Analyzer. I have tried this:

select top 1 text from myTable

(where text is a text field)

and

DECLARE @data VarChar(8000) 
select top 1 @data = text from myTable
PRINT @data

The first one prints only the first 2000 or so characters and the second only prints the first 8000 characters. Is there any way to get all of the text?

Notes:

  • must work with SQL Server 7
like image 366
alumb Avatar asked Oct 20 '08 22:10

alumb


People also ask

How do I print the value of a variable in SQL?

Example 3: SQL Server PRINT statement to print an integer value. We can specify only CHAR, NCHAR, VARCHAR or NVARCHAR data types in the PRINT statement. In this case, it implicitly converts an integer value to the VARCHAR data type internally.


2 Answers

I don't think you can use varchar(MAX) in MSSQL7, so here's something that will give you all the data (note, what I'm understanding is you just want to visually see the data, and you aren't going put it in a variable or return it).

So, this will print off the entire string so you can visually see what's in the field:

DECLARE @limit as int,
        @charLen as int,
        @current as int,
        @chars as varchar(8000)

SET @limit = 8000

SELECT  TOP 1 @charLen = LEN(text)
FROM    myTable

SET @current = 1

WHILE @current < @charLen
BEGIN
    SELECT  TOP 1 @chars = SUBSTRING(text,@current,@limit)
    FROM    myTable
    PRINT @chars

    SET @current = @current + @limit
END
like image 190
Ryan Abbott Avatar answered Oct 14 '22 08:10

Ryan Abbott


I haven't used Query Analyzer in a while, however you can adjust the maximum amount of characters displayed in the results window in the Options window. See the MSDN documentation.

like image 35
tbreffni Avatar answered Oct 14 '22 08:10

tbreffni