Hello,
I have data i table with multiple rows of data and want to combine to one row
id |
no_id |
date |
text |
1 |
12234 |
4/5/2021 |
sample text |
2 |
12234 |
4/6/2024 |
sample text 2 |
3 |
12234 |
4/6/2021 |
sample test5 |
4 |
12285 |
4/7/2021 |
new notes text |
5 |
12285 |
4/8/2021 |
new note sample text |
want to see as
12234 sample text sample text 2 sample test5
12285 new notes text new note sample text
how to acomplish this
thanks
Hello,
You can use the function STRING_AGG for that if you have SQL Server 2017 (14.x) and later:
STRING_AGG (Transact-SQL) - SQL Server | Microsoft Learn
DROP TABLE IF EXISTS #Example;
SELECT 12234 AS [id], 'sample text' AS [text]
INTO #Example
UNION ALL
SELECT 12234, 'sample text 2'
UNION ALL
SELECT 12234, 'sample text 5';
SELECT * FROM #Example;
SELECT id, STRING_AGG (CONVERT(NVARCHAR(max),[text]), CHAR(13)) AS csv
FROM #Example
GROUP BY id;
hello
but the issue would be the there would be thousands of record and id, so cannot to single select for each and id.
is there other process?
thanks
It's just an example with example data, you can use
SELECT id, STRING_AGG (CONVERT(NVARCHAR(max),[text]), CHAR(13)) AS [text]
FROM YourTable
GROUP BY id;