Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 joining strings together

Author  Topic 

curious
Starting Member

8 Posts

Posted - 2009-01-14 : 22:21:59

I'm hoping someone could help me with my query.
I have a table with two columns

paragraph# text
1 "line one"
1 "line two"
1 "line three"
2 "another line 1"
2 "another line 2"

I need to join the text lines in each paragraph together and insert
then into another table as

paragraph# text
1 "line one" + char(13)+"line two"+char(13)+"line three"
2 "another line 1"+char(13)+"another line2"

any suggestions would be greatly appreciated.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-14 : 22:58:39
[code]SELECT DISTINCT t.[paragraph#],STUFF(SELECT ','+ text FROM table WHERE [paragraph#]=t.[paragraph#] FOR XML PATH(''),1,1,'')
FROM Table t
[/code]
Go to Top of Page

curious
Starting Member

8 Posts

Posted - 2009-01-14 : 23:13:41
quote:
Originally posted by curious


I'm hoping someone could help me with my query.
I have a table with two columns

paragraph# text
1 "line one"
1 "line two"
1 "line three"
2 "another line 1"
2 "another line 2"

I need to join the text lines in each paragraph together and insert
then into another table as

paragraph# text
1 "line one" + char(13)+"line two"+char(13)+"line three"
2 "another line 1"+char(13)+"another line2"

any suggestions would be greatly appreciated.



thanks for the response.

I tried your code and got the following error message

<<Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '1'.>>

any ideas?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-14 : 23:36:36
[code]DECLARE @Test table
(
[paragraph#] int,
[text] varchar(20)
)
insert into @Test
SELECT 1, 'line one' union all
SELECT 1,'line two' union all
SELECT 1, 'line three' union all
SELECT 2, 'another line 1' union all
SELECT 2, 'another line 2'


SELECT DISTINCT t.[paragraph#],STUFF((SELECT ','+ [text] FROM @Test WHERE [paragraph#]=t.[paragraph#] FOR XML PATH('')),1,1,'')
FROM @Test t

output
-------------------------------
paragraph# Values
1 line one,line two,line three
2 another line 1,another line 2
[/code]
Go to Top of Page

curious
Starting Member

8 Posts

Posted - 2009-01-14 : 23:45:36
quote:
Originally posted by curious


I'm hoping someone could help me with my query.
I have a table with two columns

paragraph# text
1 "line one"
1 "line two"
1 "line three"
2 "another line 1"
2 "another line 2"

I need to join the text lines in each paragraph together and insert
then into another table as

paragraph# text
1 "line one" + char(13)+"line two"+char(13)+"line three"
2 "another line 1"+char(13)+"another line2"

any suggestions would be greatly appreciated.



It worked. thanks...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-14 : 23:55:58
welcome
Go to Top of Page
   

- Advertisement -