Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Concatenating a varchar(max) column Expand / Collapse
Author
Message
Posted Thursday, November 20, 2008 3:24 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, May 10, 2009 6:05 PM
Points: 9, Visits: 35
Hi, I'm having trouble concatenating multiple rows in a table into a single varchar(max) variable. I am trying to concat several other columns (date etc) with some character formatting and output the entire result. My problem is that the output always seems to get truncated; sometimes around 500 characters. Here is the code I am using;

	DECLARE @Summary varchar(MAX),
@count int, /*create local @@fetch_status*/
@iRow INT, /*row pointer (index)*/
@KeyValue Varchar(20),
@UserTitle VARCHAR(20),
@Date VARCHAR(20),
@details VARCHAR(MAX)

SET @Summary = ''

DECLARE @Reccom TABLE(
RowID INT IDENTITY(1, 1),
KeyValue VARCHAR(20),
UserTitle VARCHAR(50),
Date VARCHAR(20),
Details VARCHAR(MAX))

INSERT INTO @Reccom

SELECT cd.KeyValue,
rec.userTitle AS UserTitle,
CONVERT(varchar(20), rec.RecommendationDate, 103) AS Date,
rec.Details

from t_table rec
left outer join t_ControlData cd on rec.code= cd.KeyCode and cd.KeyName = 'name'
where rec.id= 2508
order by cd.KeyIndex ASC


/*get array Upper Bound (highest ID number)*/
SET @count = @@ROWCOUNT
/*initialize index counter*/
SET @iRow = 1

--loop though the active components of the drug
WHILE @iRow <= @count
BEGIN
SELECT @KeyValue = KeyValue,
@UserTitle = UserTitle,
@Date = Date,
@Details = Details
FROM @Reccom
WHERE RowID = @iRow

SELECT @Summary = @Summary + @KeyValue + ' Note - ' + @UserTitle + ', ' + @Date + CHAR(13)

+ @details + CHAR(13)

--increment the counter
SELECT @iRow = @iRow + 1
END

Any help would be appreciated
Post #606146
Posted Thursday, November 20, 2008 3:52 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 11,264, Visits: 13,023
Are you just doing this in SSMS? If so the SSMS query window only output a certain number of characters. If you go into tools -> options and then go to query results -> Sql Server there are settings under Results to Grids and Results to Text for the size of data to display.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #606161
Posted Thursday, November 20, 2008 3:57 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, August 6, 2014 9:43 PM
Points: 298, Visits: 711
Hi,

I'm not sure what else you are doing with this data - but I don't think you need to loop through all your rows and put everything into a table variable as you have.

Try something like this (it should be roughly correct, but check the columns against your database). This concatenates all the values (make sure they are varchar) and then joins the next row on... etc


DECLARE @var varchar(max)

SELECT @var = COALESCE(@var + ', ', '') + cd.KeyValue + ' Note - ' + rec.userTitle + ', ' + CONVERT(varchar(20), rec.RecommendationDate, 103) + CHAR(13) + rec.Details + CHAR(13)
FROM t_table rec LEFT OUTER JOIN t_ControlData cd on rec.code= cd.KeyCode and cd.KeyName = 'name'
WHERE rec.id= 2508 ORDER BY cd.KeyIndex ASC

SELECT @var




B
Post #606163
Posted Thursday, November 20, 2008 4:13 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, May 10, 2009 6:05 PM
Points: 9, Visits: 35
Thanks guys, tried both those ideas however Jack, the limit is massive and checking len(@Summary) at the end of the code (not posted) shows my lengths to be tiny..

B, I tried that solution.. in the DB, for testing, 5 rows match the record, all with the "Details" fields set to "abcdefghijklmnopqrstuvwxyZabcdefghijklmnopqrstuvwxyZabcdefghijklmnopqrstuvwxyZ"

this is the output I get from your implementation...

, user Note - Paul user, 28/03/2007
abcdefghijklmnopqrstuvwxyZabcdefghijklmnopqrstuvwxyZabcdefghijklmnopqrstuvwxyZ

Only one row! This is the same as what I found when I tried to concatenate using the select query itself:

SELECT @Summary = @Summary + @KeyValue + ' Note - ' + @UserTitle + ', '  + @Date + CHAR(13) etc..


Post #606165
Posted Thursday, November 20, 2008 4:17 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 11,264, Visits: 13,023
Can you post some sample data? Like suggested in the first link in my signature.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #606168
Posted Thursday, November 20, 2008 4:21 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, May 10, 2009 6:05 PM
Points: 9, Visits: 35
I tested with the code below and varchar(max) acts as expected when concatenating with the + operator when the number of rows are known.. However a programmatic solution still seems elusive...

DECLARE @C VARCHAR(MAX)
DECLARE @Code VARCHAR(20)
DECLARE @Value VARCHAR(MAX)


DECLARE @Temp TABLE(
RowID INT IDENTITY(1, 1),
Code VARCHAR(20),
MaxValue VARCHAR(MAX))

INSERT INTO @Temp (
Code,
MaxValue
) VALUES (
/* Code - VARCHAR(20) */ 'A note',
/* Value - VARCHAR(MAX) */ REPLICATE(CAST('A' AS VARCHAR(MAX)), 10000 ) )

INSERT INTO @Temp (
Code,
MaxValue
) VALUES (
/* Code - VARCHAR(20) */ 'B note',
/* Value - VARCHAR(MAX) */ REPLICATE(CAST('B' AS VARCHAR(MAX)), 10000 ) )

SET @C = ''

SELECT @Code = Code, @Value=MaxValue FROM @Temp WHERE RowID = 1
SELECT @C = @Code + @Value
SELECT @Code = Code, @Value=MaxValue FROM @Temp WHERE RowID = 2
SELECT @C = @C + @Code + @Value

SELECT @C
SELECT LEN(@C)

len output: 20012

Post #606170
Posted Thursday, November 20, 2008 4:27 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, May 10, 2009 6:05 PM
Points: 9, Visits: 35
Sure thing jack just use multiples of this:

   INSERT INTO @Reccom (
KeyValue,
UserTitle,
Date,
Details
) VALUES (
'Doctor Note',
'Mr Bob Saggett',
'23/10/1986',
CAST('the brown fox jumped over the silky terrier' AS VARCHAR(MAX)) )

The data itself isn't so important, but the details field can grow to be larger than 8000 chars, hence the use of max.
Post #606172
Posted Thursday, November 20, 2008 4:52 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 11,264, Visits: 13,023
This worked for me:

Declare @Reccom table (KeyValue varchar(20), USerTitle varchar(25), Date varchar(14), Details varchar(max))   
Declare @temp table(row_id int,KeyValue varchar(20), USerTitle varchar(25), Date varchar(14), Details varchar(max))
Declare @count int
Declare @data varchar(max)

Set @count = 1

WHile @Count <= 10
Begin
INSERT INTO @Reccom (
KeyValue,
UserTitle,
Date,
Details
) VALUES
(
'Doctor Note',
'Mr Bob Saggett',
'23/10/1986',
CAST('the brown fox jumped over the silky terrier' AS VARCHAR(MAX))
)

Set @count = @count + 1
End

Set @count = 1
Set @data = ''

Insert into @temp
Select
row_number() Over(Order By Date) as row_id,
*
From
@Reccom

While @count <= (Select Max(row_id) From @temp)
Begin
Select
@data = @data + keyvalue + ' Note - ' + usertitle + ', ' +
date + char(13) + details + char(13)
From
@temp

Set @count = @count + 1
End

Select Len(@data), @data





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #606181
Posted Thursday, November 20, 2008 5:10 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, May 10, 2009 6:05 PM
Points: 9, Visits: 35
Thanks heaps Jack, that seems to work perfectly :) Many thanks
Post #606187
Posted Wednesday, November 27, 2013 4:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 27, 2013 6:28 AM
Points: 6, Visits: 2
what is the limitation of concatenation which is causing below issue?
Any help?



This query does not work:
SET @tSprocSQL1 = @tMidSQL1+@tMidSQL2+@tMidSQL3+@tMidSQL4+@tMidSQL5+@tMidSQL6+@tMidSQL7+@tMidSQL8+@tMidSQL9+@tMidSQL10+@tEndSproc+ CHAR(13);

Whereas query works:
SET @tSprocSQL1 = @tMidSQL1+@tMidSQL2+@tMidSQL3+@tMidSQL4+@tMidSQL5+@tMidSQL6+@tMidSQL7+@tMidSQL8
SET @tSprocSQL1 = @tSprocSQL1+@tMidSQL9+@tMidSQL10+@tEndSproc+ CHAR(13);

___________________________________________________________
@tSprocSQL1 - is nvarchar(max)
while all tMidSQL are navarchar(4000)
Post #1517992
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse