LISTAGG Function - Oracle to SQL Server to Migration

In Oracle you can use LISTAGG function to concatenate strings from multiple rows into a single row value.

In SQL Server you can use STRING_AGG function but note that it is available since SQL Server 2017 only.

Consider a sample table:

  CREATE TABLE cities (name VARCHAR(30));
 
  -- Insert sample rows
  INSERT INTO cities VALUES ('Seville');
  INSERT INTO cities VALUES ('Warsaw');
  INSERT INTO cities VALUES ('Boston');

Oracle:

  -- Concatenate values using ';' delimiter
  SELECT LISTAGG(name, ';') WITHIN GROUP (ORDER BY name)
  FROM cities;
  # Boston;Seville;Warsaw

SQL Server:

  -- Concatenate values using ';' delimiter
  SELECT STRING_AGG(name, ';') WITHIN GROUP (ORDER BY name)
  FROM cities;
  # Boston;Seville;Warsaw

For more information, see Oracle to SQL Server Migration.