Print Varchar(max) field contents

The following illustrates an example of how to output the full text of the contents stored in a varchar(max) column in SQL Management Studio.
-- declare some variables
DECLARE @lRowID int, @txtExcp nvarchar(max)
set @lRowID = 163020	-- set the value of the identity column of the row of interest

-- select the value from the column into our varchar(max) variable.
SELECT @txtExcp = a.txtException
from MyData a
where a.lRowID = @lRowID

-- cast the varchar(max) to a text during the print to the output
PRINT convert(ntext, @txtExcp)


-- use the rest of this if the output is greater than 16000 characters
declare @idx int = 1
	, @size int = 16000
	, @fulllen int = len(@txtExcp)

while (@idx < @fulllen)
begin

	print convert(text, substring(@txtExcp, @idx, @size))
	set @idx = @idx + @size

end


Last modified by Mohit @ 2/4/2026 10:48:34 AM