Re-Creates table with test strings:
- -- Re-Create and fill table with test strings
- IF OBJECT_ID('dbo.TestTexts') IS NOT NULL
- DROP TABLE dbo.TestTexts;
- GO
- CREATE TABLE dbo.TestTexts (SomeText VARCHAR(20));
- GO
- INSERT INTO dbo.TestTexts
- VALUES
- ('Aaa'),
- ('Bbb'),
- ('Ccc'),
- ('Ddd');
- GO
Simple concatination of strings:
Query Result:
- SELECT GROUP_CONCAT = '' + (
- SELECT '' + SomeText
- FROM dbo.TestTexts
- ORDER BY SomeText
- FOR XML PATH('')
- )
GROUP_CONCAT ------------ AaaBbbCccDdd
Concatinates Strings with delimiter:
Query Result:
- SELECT
- -- Remove First Comma
- GROUP_CONCAT = STUFF(
- (
- -- Concatinate Strings
- SELECT ',' + SomeText
- FROM dbo.TestTexts
- ORDER BY SomeText
- FOR XML PATH('')
- ),
- 1,1,''
- );
- GO
GROUP_CONCAT --------------- Aaa,Bbb,Ccc,Ddd