SQL Query to Concatenate text [duplicate]












0















This question already has an answer here:




  • Concatenate one field after GROUP BY

    1 answer



  • How to make a query with group_concat in sql server [duplicate]

    4 answers



  • Simulating group_concat MySQL function in Microsoft SQL Server 2005?

    9 answers



  • get a comma delimited string from rows [duplicate]

    2 answers




I am trying to find the best way to consolidate the string in the fields that have the same value. Please look at the example below:



Table1:



Column1 | Column2 | Column3 | Column4
test1 test2 test3 string1
test1 test2 test3 string2
test4 test5 test6 string3
test4 test5 test6 string4


The result need to be inserted to Table2 like following:



Table2:



Column1 | Column2 | Column3 | Column4
test1 test2 test3 string1 string2
test4 test5 test6 string3 string4


I need to avoid Cursors please. Thank you.
Please note: I can not use the XML PATH.



The following query wouldn't work:



SELECT
m.Column1,m.Column2,m.Column3
, Column4 = STUFF((
SELECT ' ' + Column4
FROM dbo.Table1
FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 1, '')
FROM dbo.Table1 m









share|improve this question















marked as duplicate by Zohar Peled sql
Users with the  sql badge can single-handedly close sql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 21:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Do you have only two rows that match?
    – Gordon Linoff
    Nov 20 at 21:33






  • 1




    Investigate "group by" on the first three columns, and the use of STUFF... check out this answer: stackoverflow.com/questions/13647394/…
    – pmbAustin
    Nov 20 at 21:35










  • just another similar answer stackoverflow.com/questions/18910134/…
    – morsik
    Nov 20 at 21:36












  • Similar @morsik but for SQL Server they'll need this one: stackoverflow.com/questions/17591490/… which is mentioned in your post but as the third answer. MySQL was accepted.
    – scsimon
    Nov 20 at 21:37








  • 1




    And the list of dupes can get even bigger...
    – Zohar Peled
    Nov 20 at 21:41
















0















This question already has an answer here:




  • Concatenate one field after GROUP BY

    1 answer



  • How to make a query with group_concat in sql server [duplicate]

    4 answers



  • Simulating group_concat MySQL function in Microsoft SQL Server 2005?

    9 answers



  • get a comma delimited string from rows [duplicate]

    2 answers




I am trying to find the best way to consolidate the string in the fields that have the same value. Please look at the example below:



Table1:



Column1 | Column2 | Column3 | Column4
test1 test2 test3 string1
test1 test2 test3 string2
test4 test5 test6 string3
test4 test5 test6 string4


The result need to be inserted to Table2 like following:



Table2:



Column1 | Column2 | Column3 | Column4
test1 test2 test3 string1 string2
test4 test5 test6 string3 string4


I need to avoid Cursors please. Thank you.
Please note: I can not use the XML PATH.



The following query wouldn't work:



SELECT
m.Column1,m.Column2,m.Column3
, Column4 = STUFF((
SELECT ' ' + Column4
FROM dbo.Table1
FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 1, '')
FROM dbo.Table1 m









share|improve this question















marked as duplicate by Zohar Peled sql
Users with the  sql badge can single-handedly close sql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 21:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Do you have only two rows that match?
    – Gordon Linoff
    Nov 20 at 21:33






  • 1




    Investigate "group by" on the first three columns, and the use of STUFF... check out this answer: stackoverflow.com/questions/13647394/…
    – pmbAustin
    Nov 20 at 21:35










  • just another similar answer stackoverflow.com/questions/18910134/…
    – morsik
    Nov 20 at 21:36












  • Similar @morsik but for SQL Server they'll need this one: stackoverflow.com/questions/17591490/… which is mentioned in your post but as the third answer. MySQL was accepted.
    – scsimon
    Nov 20 at 21:37








  • 1




    And the list of dupes can get even bigger...
    – Zohar Peled
    Nov 20 at 21:41














0












0








0








This question already has an answer here:




  • Concatenate one field after GROUP BY

    1 answer



  • How to make a query with group_concat in sql server [duplicate]

    4 answers



  • Simulating group_concat MySQL function in Microsoft SQL Server 2005?

    9 answers



  • get a comma delimited string from rows [duplicate]

    2 answers




I am trying to find the best way to consolidate the string in the fields that have the same value. Please look at the example below:



Table1:



Column1 | Column2 | Column3 | Column4
test1 test2 test3 string1
test1 test2 test3 string2
test4 test5 test6 string3
test4 test5 test6 string4


The result need to be inserted to Table2 like following:



Table2:



Column1 | Column2 | Column3 | Column4
test1 test2 test3 string1 string2
test4 test5 test6 string3 string4


I need to avoid Cursors please. Thank you.
Please note: I can not use the XML PATH.



The following query wouldn't work:



SELECT
m.Column1,m.Column2,m.Column3
, Column4 = STUFF((
SELECT ' ' + Column4
FROM dbo.Table1
FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 1, '')
FROM dbo.Table1 m









share|improve this question
















This question already has an answer here:




  • Concatenate one field after GROUP BY

    1 answer



  • How to make a query with group_concat in sql server [duplicate]

    4 answers



  • Simulating group_concat MySQL function in Microsoft SQL Server 2005?

    9 answers



  • get a comma delimited string from rows [duplicate]

    2 answers




I am trying to find the best way to consolidate the string in the fields that have the same value. Please look at the example below:



Table1:



Column1 | Column2 | Column3 | Column4
test1 test2 test3 string1
test1 test2 test3 string2
test4 test5 test6 string3
test4 test5 test6 string4


The result need to be inserted to Table2 like following:



Table2:



Column1 | Column2 | Column3 | Column4
test1 test2 test3 string1 string2
test4 test5 test6 string3 string4


I need to avoid Cursors please. Thank you.
Please note: I can not use the XML PATH.



The following query wouldn't work:



SELECT
m.Column1,m.Column2,m.Column3
, Column4 = STUFF((
SELECT ' ' + Column4
FROM dbo.Table1
FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 1, '')
FROM dbo.Table1 m




This question already has an answer here:




  • Concatenate one field after GROUP BY

    1 answer



  • How to make a query with group_concat in sql server [duplicate]

    4 answers



  • Simulating group_concat MySQL function in Microsoft SQL Server 2005?

    9 answers



  • get a comma delimited string from rows [duplicate]

    2 answers








sql sql-server sql-server-2008






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:49

























asked Nov 20 at 21:33









user3314399

1371720




1371720




marked as duplicate by Zohar Peled sql
Users with the  sql badge can single-handedly close sql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 21:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Zohar Peled sql
Users with the  sql badge can single-handedly close sql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 21:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Do you have only two rows that match?
    – Gordon Linoff
    Nov 20 at 21:33






  • 1




    Investigate "group by" on the first three columns, and the use of STUFF... check out this answer: stackoverflow.com/questions/13647394/…
    – pmbAustin
    Nov 20 at 21:35










  • just another similar answer stackoverflow.com/questions/18910134/…
    – morsik
    Nov 20 at 21:36












  • Similar @morsik but for SQL Server they'll need this one: stackoverflow.com/questions/17591490/… which is mentioned in your post but as the third answer. MySQL was accepted.
    – scsimon
    Nov 20 at 21:37








  • 1




    And the list of dupes can get even bigger...
    – Zohar Peled
    Nov 20 at 21:41


















  • Do you have only two rows that match?
    – Gordon Linoff
    Nov 20 at 21:33






  • 1




    Investigate "group by" on the first three columns, and the use of STUFF... check out this answer: stackoverflow.com/questions/13647394/…
    – pmbAustin
    Nov 20 at 21:35










  • just another similar answer stackoverflow.com/questions/18910134/…
    – morsik
    Nov 20 at 21:36












  • Similar @morsik but for SQL Server they'll need this one: stackoverflow.com/questions/17591490/… which is mentioned in your post but as the third answer. MySQL was accepted.
    – scsimon
    Nov 20 at 21:37








  • 1




    And the list of dupes can get even bigger...
    – Zohar Peled
    Nov 20 at 21:41
















Do you have only two rows that match?
– Gordon Linoff
Nov 20 at 21:33




Do you have only two rows that match?
– Gordon Linoff
Nov 20 at 21:33




1




1




Investigate "group by" on the first three columns, and the use of STUFF... check out this answer: stackoverflow.com/questions/13647394/…
– pmbAustin
Nov 20 at 21:35




Investigate "group by" on the first three columns, and the use of STUFF... check out this answer: stackoverflow.com/questions/13647394/…
– pmbAustin
Nov 20 at 21:35












just another similar answer stackoverflow.com/questions/18910134/…
– morsik
Nov 20 at 21:36






just another similar answer stackoverflow.com/questions/18910134/…
– morsik
Nov 20 at 21:36














Similar @morsik but for SQL Server they'll need this one: stackoverflow.com/questions/17591490/… which is mentioned in your post but as the third answer. MySQL was accepted.
– scsimon
Nov 20 at 21:37






Similar @morsik but for SQL Server they'll need this one: stackoverflow.com/questions/17591490/… which is mentioned in your post but as the third answer. MySQL was accepted.
– scsimon
Nov 20 at 21:37






1




1




And the list of dupes can get even bigger...
– Zohar Peled
Nov 20 at 21:41




And the list of dupes can get even bigger...
– Zohar Peled
Nov 20 at 21:41

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python