SQL Server CONCAT() Function

The CONCAT() function in SQL Server combines two or more strings into a single string. This is a fundamental operation for working with and manipulating text data.



CONCAT(): Definition and Usage

CONCAT() offers a convenient way to join strings together. It's frequently used to create combined text fields, build more descriptive labels, construct file paths, or perform any task requiring the merging of multiple text strings. It handles NULL values gracefully; if any input is NULL, the result is NULL.

Syntax

Syntax

CONCAT(string1, string2, ..., string_n)
      

Parameter Values

Parameter Description
string1, string2, ..., string_n The strings you want to concatenate. At least one string is required.

Related Functions

SQL Server also allows string concatenation using the + operator. For concatenating strings with a separator, use the CONCAT_WS() function.

Examples

Concatenating Two Strings

This example combines "W3Schools" and ".com".

Syntax

SELECT CONCAT('W3Schools', '.com');
      
Output

W3Schools.com
      

Concatenating Three Strings

This example shows how to concatenate three strings together.

Syntax

SELECT CONCAT('SQL', ' is', ' fun!');
      
Output

SQL is fun!
      

Concatenating Strings with Spaces

Adding spaces between the concatenated strings for better readability.

Syntax

SELECT CONCAT('SQL', ' ', 'is', ' ', 'fun!');
      
Output

SQL is fun!