I tried to use STRING_ARG to concatenate a string of comments and found that one comment was being cut off. I was not going over the NVARCHAR(4000) or even anything close to it. I thin my total length was around 400, but it was cutting off the last string.
I was originally working with some JSON (fake data):
SELECT STRING_AGG([CALC].[Comment], CHAR(13))
FROM
OPENJSON('[{"Comment":"This is one comment.","CommentDateTime":"2020-02-05T17:15:09.397","CommentAddedByUser":"Lenka"},{"Comment":"This is another comment. Just a test done to see what JSON data would look like.","CommentDateTime":"2020-02-05T17:14:09.397","CommentAddedByUser":"Penka"},{"Comment":"...and here is one last comment, just so we have more than 2.","CommentDateTime":"2020-02-05T17:12:09.397","CommentAddedByUser":"John"}]')
WITH ( [Comment] NVARCHAR(255) '$.Comment', [CommentDateTime] [DATETIME] '$.CommentDateTime', [CommentAddedByUser] [NVARCHAR](50) '$.CommentAddedByUser' ) AS [OJ]
CROSS APPLY ( SELECT CONCAT('Comment: ', [OJ].[Comment], ' Comment Date Time: ', [OJ].[CommentDateTime], ' Comment Added By User: ', [OJ].[CommentAddedByUser]) AS [Comment] ) [CALC]([Comment]);There are three rows of JSON data, but the STRING_AGG function was returning only two. The total combined length of the three rows is 392.
I thought that maybe it was the JSON that was causing the "confusion", so I took the strings:
SELECT STRING_AGG([IQ].[Comment], CHAR(13)) FROM ( VALUES ( 'Comment: This is one comment. Comment Date Time: Feb 5 2020 5:15PM Comment Added By User: Lenka' ) , ( 'Comment: This is another comment. Just a test done to see what JSON data would look like. Comment Date Time: Feb 5 2020 5:14PM Comment Added By User: Penka' ) , ( 'Comment: ...and here is one last comment, just so we have more than 2. Comment Date Time: Feb 5 2020 5:12PM Comment Added By User: John' ) ) [IQ] ( [Comment] );There was no change.
...so I tried this:
SELECT STRING_AGG(LEFT([IQ].[Comment], 100), CHAR(13)) FROM ( VALUES ( 'Comment: This is one comment. Comment Date Time: Feb 5 2020 5:15PM Comment Added By User: Lenka' ) , ( 'Comment: This is another comment. Just a test done to see what JSON data would look like. Comment Date Time: Feb 5 2020 5:14PM Comment Added By User: Penka' ) , ( 'Comment: ...and here is one last comment, just so we have more than 2. Comment Date Time: Feb 5 2020 5:12PM Comment Added By User: John' ) ) [IQ] ( [Comment] );
...and based on this I can see that it's cutting of at 256 characters. I have not found anything that would tell me why STRING_AGG cuts off at 256.
Can anyone offer any thoughts on this?
Thanks.