CONCAT function in SQL Server

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

May be interested

  • The '+' operator in SQL ServerPhoto of The '+' operator in SQL Server
    learn how to use the + operator in sql server to join two or more strings into a string.
  • DATALENGTH function in SQL ServerPhoto of DATALENGTH function in SQL Server
    the article will explore and show you how to use the datalength function in sql server to display the number of bytes used to represent an expression ..
  • LEFT function in SQL ServerPhoto of LEFT function in SQL Server
    the left function in sql server allows you to extract a substring from a large string, starting from the leftmost character.
  • LEN function in SQL ServerPhoto of LEN function in SQL Server
    the len function in sql server returns the length of the specified string. it is important that the len function does not include whitespace characters at the end of the string when calculating length.
  • LOWER function in SQL ServerPhoto of LOWER function in SQL Server
    the article will explore and show you how to use the lower function in sql server to convert letters in a specified string to lowercase.
  • LTRIM function in SQL ServerPhoto of LTRIM function in SQL Server
    ltrim function in sql server is used to delete all space characters from the first position (left positions) of the string.