ComputersSoftware

What does the SQL CONCAT function do?

Working with databases is constantly associated with obtaining query results. And in some cases this information needs to be displayed on a screen in a certain way or combined with other data. To solve this problem, there is a function SQL - CONCAT.

What does the CONCAT function do?

When performing some work with databases, there is a need for connecting strings with additional data or with each other. To solve such a problem there is a function SQL - CONCAT. When you use it, two or more rows will be merged into one. In this case, the string concatenation operation will be correctly performed both when working with strings, and with numerical variables.

In some databases, SQL CONCAT has several varieties. So, in MySQL it has its own analogue - CONCAT_WS. The difference between the functions is not significant: in the first case, when combined with an argument whose value is NULL, the concatenation result will be NULL, and if using the second variant of the join, the null operand will simply be omitted.

Function syntax

When using the CONCAT function, the SQL syntax obliges the programmer to use arguments in the order in which the operands must be connected. The syntax of the string concatenation operation is quite simple: after the CONCAT keyword, all the necessary arguments or lines are ordered in comma separated by commas, and after the closing parenthesis, if necessary, the keyword AS and the name of the variable to which the result is written is indicated. The CONCAT function pattern looks like this:

CONCAT (line1, line2 [, line 3, ...]) [AS variable_name].

It is worth noting that both the operand of a numeric and string value, as well as some function that returns a result, can be used as function arguments. For example, the operation itself is SQL CONCAT. Thus, the operation supports the principle of nesting.

How to do without using CONCAT?

In the case when there is a need to do without using an operation, or the programmer does not know the CONCAT function, SQL offers one more option for concatenating strings. To do this, you need to use the operator "+", but this approach has a number of features that must be taken into account when working with it.

If you use the "+" function, if the operands are numeric values, the result of the operation will be the ordinary addition of numbers, and if necessary, combine the number with a string without an explicit conversion, the program will generate an error. So, when the next query is executed, the result is the value of the number "6":

SELECT 1 + 2 + 3.

In other respects, both methods are identical and return the same result.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.