Select*fromStudent_Name;+---------+-------+---"/> Select*fromStudent_Name;+---------+-------+---">

Home  >  Article  >  Database  >  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
PHPzforward
2023-09-05 16:57:13927browse

当我们想要连接列中的值并且任何列的值为 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)

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)

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)

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete