Microsoft SQL Server: TSQL Formatting of Results Columns

All we need is an easy explanation of the problem, so here it is.

I have a result set of data that has duplicates because certain companies are tied to multiple banks is there a way to combine the values in the BankName column either into a single column.

So the result would be Bendigo Bank, *** Merchant Services on a single row

Or Split them so each row has two bank columns BankName1 with Bendigo Bank As the Value and BankName2 with *** Merchant Services As the value

Microsoft SQL Server: TSQL Formatting of Results Columns

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You could use STRING_AGG to aggregate the bank names. For your query, it might look something like:

SELECT … STRING_AGG([BankName], ', ') as [BankNames], …

The STRING_AGG() function will work on all versions of SQL Server since SQL Server 2017.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply