Using CONCAT_WS

The CONCAT_WS function is similar to the CONCAT function. It also concatenates a given expression, but unlike with the CONCAT function, here you can specify a separator. The separator is any string type and the number of characters for the separator is not limited (you can even use more than 8,000 characters).

The following query illustrates the usage of the CONCAT_WS function:

SELECT CONCAT_WS(' / ',name, compatibility_level, collation_name) FROM sys.databases WHERE database_id < 5;

Here is the output returned by the previous query:

 db-----------------------------------------master / 140 / SQL_Latin1_General_CP1_CI_AStempdb / 140 / SQL_Latin1_General_CP1_CI_ASmodel / 140 / SQL_Latin1_General_CP1_CI_ASmsdb / 130 / SQL_Latin1_General_CP1_CI_AS ...

Get SQL Server 2017 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.