# GROUP_CONCAT / STRING_AGG result truncated or fails (group_concat_max_len, string length limit)

**Code:** `GROUP_CONCAT` · **Category:** SQL · **Seen on:** SQL, MySQL, SQL Server

An aggregated string is cut off at 1024 characters (MySQL) or fails with a size error (SQL Server STRING_AGG, 8000 bytes).

## Meaning

MySQL's `group_concat_max_len` defaults to 1024; raise it per session (`SET SESSION group_concat_max_len = 1000000`). SQL Server's `STRING_AGG` returns varchar(8000) unless the input is `varchar(max)` (cast first, error 9829 otherwise). PostgreSQL `string_agg` has no practical limit but the result can exceed client limits.

## Common causes

- Default `group_concat_max_len` = 1024
- STRING_AGG over varchar/nvarchar without MAX
- Large groups building huge strings
- Client or driver size limits
- Silent truncation unnoticed

## Quick fix

1. MySQL: `SET SESSION group_concat_max_len = 1000000;`
2. SQL Server: `STRING_AGG(CAST(col AS varchar(max)), ',')`
3. Aggregate in the application for very large sets

## Detailed fix by platform

### SQL

1. 

```sql
SET SESSION group_concat_max_len = 1000000;
SELECT STRING_AGG(CAST(name AS varchar(max)), ', ') FROM people;
```


## How to diagnose

1. **Result** — Truncated?
2. **Limit** — Which setting?
3. **Cast** — To MAX?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/sql/group-concat-result-truncated-max-len)
