CONCAT function in SQL Server

Learn how to use the CONCAT () function in SQL Server to join two or more strings into a string.

The article will explore and show you how to use the CONCAT () function in SQL Server to join multiple strings into a string.

Describe

The CONCAT function in SQL Server is used to join two or more strings into a single large string.

Syntax

To run the CONCAT function in SQL Server, we use the following syntax:

 CONCAT(chuoi1, chuoi2,. chuoi_n) 

Parameters :

  1. series1, chuoi2, . cycle : strings want to connect with each other.

Note :

  1. Input CONCAT function has a maximum length of 255 strings, a minimum of 2 strings. If only one input string is passed, CONCAT will generate an error.
  2. If passing strings without characters, CONCAT will automatically convert those values ​​into strings before joining.
  3. The CONCAT function converts NULL to an empty string in VARCHAR data type (1).
  4. The CONCAT function can only be used in later versions of SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, and SQL Server 2012.

For example

Take a look and explore some examples of CONCAT functions in SQL Server.

 SELECT CONCAT('Quantrimang', '.com'); 
Result: 'TipsMake.com'

SELECT CONCAT('Quan', 'tri', 'mang', '.com');
Result: 'TipsMake.com'

SELECT CONCAT('Quan ', 'Tri ', 'Mang ');
Result: 'Quan Tri Mang'

When combining strings together, you can add whitespace characters to separate your linked values ​​for easier reading.

 SELECT CONCAT('Orange', ' ', 'Peach', ' ', 'Apple'); 
Result: 'Orange Peach Apple'

In this example, Quantrimang used the second and fourth parameters in the CONCAT function to add a space character between values ​​Orange, Peach and Apple. This will help the strings not be tied together.

Previous article: CHARINDEX function in SQL Server

Next lesson: '+' operator in SQL Server

4 ★ | 2 Vote