Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Why is Concatenation of 2 nvarchar(max) values truncating?

Posted on 2009-04-27
2
Medium Priority
?
759 Views
Last Modified: 2012-06-22
I'm using a cursor to concatenate all the values of the OriginalComments column into one variable called @Comments.  Both are declared as nvarchar(max).  Why does the line where concatenation occurs result in an empty string?

Declare @Comments nvarchar(max)
Declare @Question nvarchar(max)
Declare @DisplayOrder nvarchar(max)
Declare @OriginalComments nvarchar(max)
 
OPEN Comments_Cursor
FETCH NEXT FROM Comments_Cursor INTO @Question, @DisplayOrder, @OriginalComments
 
WHILE @@FETCH_STATUS = 0
BEGIN
	Select @Comments = rtrim(@Comments) + rtrim(@OriginalComments) + '; '
	Print @OriginalComments
	Print rtrim(@Comments)
FETCH NEXT FROM Comments_Cursor INTO @Question, @DisplayOrder, @OriginalComments
END
CLOSE Comments_Cursor
DEALLOCATE Comments_Cursor
 
Print @Comments

Open in new window

0
Comment
Question by:KentDBerry
2 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 2000 total points
ID: 24245980
Hi,

Your @Comments is null and it might be that one or more @OriginalComments are null.

Try

Declare @Comments nvarchar(max)
Declare @Question nvarchar(max)
Declare @DisplayOrder nvarchar(max)
Declare @OriginalComments nvarchar(max)
Set @Comments=''

OPEN Comments_Cursor
FETCH NEXT FROM Comments_Cursor INTO @Question, @DisplayOrder, @OriginalComments
 
WHILE @@FETCH_STATUS = 0
BEGIN
        Select @Comments = rtrim(@Comments) + rtrim(isnull(@OriginalComments,'')) + '; '
        Print @OriginalComments
        Print rtrim(@Comments)
FETCH NEXT FROM Comments_Cursor INTO @Question, @DisplayOrder, @OriginalComments
END
CLOSE Comments_Cursor
DEALLOCATE Comments_Cursor
 
Print @Comments


/peter
0
 

Author Closing Comment

by:KentDBerry
ID: 31575205
Thanks Peter.  That did it.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question