Skip to content
Advertisement

STUFF doesn’t work well with NULL Values and Grouping

I have table with below schema and data.

Table data:

SearchTest

I would like to do grouping using Alphanumeric and Integers column and get the DocumentNumber as comma separated value in my final result.

My final result should look like this,

Final Result

Here is my query that gives the above output,

However the above query breaks if I have null values in Integers column.

For example, if I have NULL value in my integer columns as shown here:

Search Test With NULL Values

Now my query breaks and I get the wrong results in my stuff query as shown below

enter image description here

Grouping works fine in the above query but STUFF part which gives DocumentNumbers gives wrong result. In this case it has be 2 in first row and 1 in second row.

Here is the expected result:

Please assist on where I’m going wrong

Advertisement

Answer

Following @GordonLinoff comments in the question. This can be easily achieved using STRING_AGG() provided you’re using SQL Server 2017 and above. This simplifies the query as well.

Query:

Expected Output:

Answer

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement