SQL Server CONCAT_WS() Function

The CONCAT_WS() function in SQL Server concatenates (joins) two or more strings together, inserting a separator that you specify between them. This is a very useful function for combining strings in a controlled and readable way.



CONCAT_WS(): Definition and Usage

CONCAT_WS() is particularly helpful when you want to combine multiple strings and need a separator between them. The first argument you provide is the separator string, followed by the strings you want to concatenate. This makes building combined strings much easier to read than using multiple + operators.

Syntax

Syntax

CONCAT_WS(separator, string1, string2, ..., string_n)
      

Parameter Values

Parameter Description
separator The string to use as a separator between the other strings. This is required.
string1, string2, ..., string_n The strings to be joined together. At least one string is required.

Related Functions

SQL Server also supports string concatenation using the + operator and the CONCAT() function (which doesn't automatically add separators).

Examples

Concatenating Strings with a Dot (.) Separator

This example combines three strings using a dot (.) as the separator.

Syntax

SELECT CONCAT_WS('.', 'www', 'W3Schools', 'com');
      
Output

www.W3Schools.com
      

Concatenating Strings with a Different Separator

This example uses a hyphen (-) as the separator.

Syntax

SELECT CONCAT_WS('-', 'SQL', ' is', ' fun!');
      
Output

SQL-is-fun!