Better Grouping with Easy SQL Functions

Here are some easy SQL commands you can use for better grouping options.  Quite often, I run into situations where I need to combine multiple values or descriptions into a column when it comes to reporting.

Examples:

There are a few SQL functions you can use to do this:

  • CONCAT – allows you to concatenate of combine two or more string values. The string values append at the end of the prior string.
  • STUFF – allows you to insert a string value into another string value, or replace part of a string with another one.
  • FOR XML PATH – add this to the end of your query to output the results of the query as XML elements.

This is what the query looks like without anything:

In this example, the FOR XML PATH function will format the data into one row, like this:

, INV025834, PRJ3633 nepsa holdings

The STUFF function will remove the 1st comma, so using STUFF and FOR XML PATH together, I get one nicely formatted row.

 

Give it a try! Our team is full of tips & tricks, learn more on our blog.

Leave a Reply