MySQL MySQL CONCAT() AND CONCAT_WS() functions

MySQL CONCAT() AND CONCAT_WS() functions

MySQL concat and concat-ws functions - techbriefers.com

MySQL CONCAT() function

MySQL CONCAT() function is basically a string function. That means we can use it to do operations on strings. This function is used to concatenate one or more strings. Here, we pass strings as arguments separated by a comma, followed by each string. Thus, it will return a string which will be the result of the concatenation of arguments. The concatenated string will have the same sequence, the arguments were passed.

If any of the arguments passed in CONCAT() function is NULL then it will return NULL. Any numeric argument is automatically converted to its equivalent non-binary string form (1 = ‘1’). If any argument id a binary string then the result will be a binary string otherwise a non-binary string will be returned as result.

Let’s see with examples

MySQL concat function techbriefers.com
MySQL concat function techbriefers.com
MySQL concat function with null argument techbriefers.com
MySQL concat function with null argument techbriefers.com
MySQL concat function with numeric argument techbriefers.com
MySQL concat function with numeric argument techbriefers.com
MySQL concat function with a binary string argument techbriefers.com

MySQL CONCAT_WS() function

Similarly, MySQL CONCAT_WS() function is also used to do concatenation operations on strings, but with a separator, unlike CONCAT() function. This function concatenates one or more strings passed as arguments with a custom separator. The first argument passed is the separator for all the following strings passed (separated by comma).

This function inserts the separator between two concatenating strings. Hence, the number of occurrences of the separator will 1 less than the number of strings. Eg: Concatenation of – one, two, three with separator will be ‘one-two-three‘.

If the separator passed in CONCAT_WS() function is NULL then it returns NULL. Otherwise NULL strings are ignored but blank values are not ignored.

Let’s see with examples

MySQL concat_ws function - techbriefers.com
MySQL concat_ws function – techbriefers.com
concat_ws function with null separator - techbriefers.com
MySQL concat_ws function with null separator – techbriefers.com
MySQL concat_ws function with blank separator - techbriefers.com
MySQL concat_ws function with blank separator – techbriefers.com
MySQL concat_ws function with blank string - techbriefers.com
MySQL concat_ws function with blank string – techbriefers.com
concat_ws function with null string - techbriefers.com
MySQL concat_ws function with null string – techbriefers.com

We can easily concatenate values and strings by using these functions. Below is the example which is a demonstration of the functions in an effective manner.
Click here to Learn SQL.

Example of CONCAT() function:

Example of CONCAT_WS() function:

MySQL concat_ws function example - techbriefers.com
MySQL concat_ws function example – techbriefers.com

Leave a Reply