SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need to merge multiple rows into one with carriage return


Need to merge multiple rows into one with carriage return

Author
Message
tan110
tan110
SSC Eights!
SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)

Group: General Forum Members
Points: 888 Visits: 712
DECLARE @Sample TABLE
(
ID INT
, Description varchar(100)
)

INSERT @Sample
SELECT 1234, 'I' UNION ALL
SELECT 1234, 'love' UNION ALL
SELECT 1234, 'sql server' UNION ALL
SELECT 3234, 'i' UNION ALL
SELECT 3234, 'despise' UNION ALL
SELECT 3234, 'oracle'

Select * from @Sample

I want to return 2 rows, and the Description to have carriage returns between each. Like this:

1234 I
love
sql server

3234 i
despise
oracle
GSquared
GSquared
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60017 Visits: 9730
String concatenation, and adding some hard returns into it, should do that.

But why would you want to do presentation-level tasks like that in the database? Do that kind of thing in the web page/app.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Derrick Smith
Derrick Smith
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2350 Visits: 715
Use CHAR(13)

so something like this..

DECLARE @str varchar(50)

SELECT @str = 'I'+CHAR(13)+'LOVE'+CHAR(13)+'SQL SERVER'

PRINT @str
tan110
tan110
SSC Eights!
SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)

Group: General Forum Members
Points: 888 Visits: 712
Unfortunately, my boss wants me to do this at the database level. The problem is, I won't know how many rows I'll get. So how do i concatenate row description based on N number of rows?
GSquared
GSquared
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60017 Visits: 9730
Search online for "XML path string concatenation". It's a pretty slick trick.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
_DJ
_DJ
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 97
try like this
SELECT         
ID,
[Description] = MAX(o.list)
FROM
@Sample T1
CROSS APPLY
(
SELECT ISNULL([Description], '') AS [data()]
FROM @Sample T2
WHERE T2.ID = T1.ID
FOR XML PATH ('')
)o(list)
GROUP BY ID


tan110
tan110
SSC Eights!
SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)

Group: General Forum Members
Points: 888 Visits: 712
Getting closer:

Using

SELECT
ID,
[Description] = MAX(o.list)
FROM
@Sample T1
CROSS APPLY
(
SELECT ISNULL([Description], '' ) AS [data()]
FROM @Sample T2
WHERE T2.ID = T1.ID
FOR XML PATH ('')
)o(list)
GROUP BY ID

My results come out: (Using results to text)

1234 I love sql server
3234 i despise oracle


How do I add a carriage return, so my output looks like this:

1234 I
love
sql server



3234 i
despise
oracle
steve-893342
steve-893342
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1229 Visits: 2670
try this

;WITH cte AS 
(
SELECT DISTINCT ID
FROM @Sample
)
SELECT cte.ID, Z.Descriptions
FROM cte
CROSS APPLY
(
SELECT (STUFF((SELECT CHAR(13) + Description
FROM @Sample AS Sample
WHERE Sample.ID = cte.ID
FOR XML PATH(''), TYPE).value('.[1]', 'varchar(MAX)'), 1, 1, ''))
) AS Z (Descriptions)



tan110
tan110
SSC Eights!
SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)

Group: General Forum Members
Points: 888 Visits: 712
Wow! That is awesome, Thank you very much. Works perfectly.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search