Home > Database > Mysql Tutorial > What is the advantage of CONCAT_WS() function over CONCAT() function when we want to concatenate values ​​in columns and the value of any column is NULL?

What is the advantage of CONCAT_WS() function over CONCAT() function when we want to concatenate values ​​in columns and the value of any column is NULL?

PHPz
Release: 2023-09-05 16:57:13
forward
997 people have browsed it

当我们想要连接列中的值并且任何列的值为 NULL 时,CONCAT_WS() 函数相对于 CONCAT() 函数有什么优势?

We know that if any parameter is NULL, the CONCAT() function will return NULL, but only when the first parameter (ie, the separator) is NULL, CONCAT_WS( ) function will return NULL, and it will ignore any other NULL. When we want to concatenate the values ​​in a column and the value of any column is NULL, we can say that this is the advantage of CONCAT_WS() function over CONCAT() function. To understand it, let's consider the example from the table "Student_name;" There is the following data in it-

mysql> Select * from Student_Name;
+---------+-------+---------+
| FName   | Mname | Lname   |
+---------+-------+---------+
| Rahul   | NULL  | Singh   |
| Gaurav  | Kumar | NULL    |
| Harshit | NULL  | Khurana |
| Yash    | Pal   | Sharma  |
+---------+-------+---------+
4 rows in set (0.00 sec)
Copy after login

Now, suppose we want to concatenate the values ​​of Fname, Mname and Lname columns, the output is as follows-

mysql> Select CONCAT(Fname,Mname,Lname)AS Name from Student_Name;
+---------------+
| Name          |
+---------------+
| NULL          |
| NULL          |
| NULL          |
| YashPalSharma |
+---------------+
4 rows in set (0.00 sec)
Copy after login

But, if we use CONCAT_WS() function, then It will ignore NULL as shown in the following query -

mysql> Select CONCAT_WS(' ',Fname,Mname,Lname)AS Name from student_name;
+-----------------+
| Name            |
+-----------------+
| Rahul Singh     |
| Gaurav Kumar    |
| Harshit Khurana |
| Yash Pal Sharma |
+-----------------+
4 rows in set (0.00 sec)
Copy after login

The above is the detailed content of What is the advantage of CONCAT_WS() function over CONCAT() function when we want to concatenate values ​​in columns and the value of any column is NULL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template