SQL CONCAT_WS函数
CONCAT_WS字符串函数在结构化查询语言中使用分隔符将两个或多个字符串连接成一个字符串。
CONCAT_WS字符串函数的语法
语法1: 此语法使用SQL表的多个列与CONCAT_WS函数:
从Table_Name中选择CONCAT_WS(separator, Column_Name1, column_Name2, Column_Name3,……. Column_NameN) AS Alias_Name;
语法1: 这个语法在SQL表中使用CONCAT_WS函数与多列进行拼接。
SELECT CONCAT_WS(separator, Column_Name1, column_Name2, Column_Name3,……. Column_NameN) AS Alias_Name FROM Table_Name;
在语法中,我们需要指定那些列的名称,我们想要使用特定分隔符连接其值。
后退10秒播放视频前进10秒
语法2: 这个语法使用CONCAT_WS函数与两个或更多字符串一起使用。
SELECT CONCAT_WS(Separator,String1, String2, String3, …… StringN);
语法3: 这个语法使用CONCAT_WS函数和两个或更多字符。
SELECT CONCAT_WS(Character_1, character_2, Character_3, ……., Character_N);
CONCAT_WS字符串函数的示例
示例1: 下面的SELECT查询将两个字符相加,形成一个新的字符串:
SELECT CONCAT_WS( *, 'H', 'i', 'i', 'i' ) AS;
输出:
'H*i*i*i'
示例2:以下SELECT查询通过添加超过2个字符来生成一个新的字符串。
SELECT CONCAT_WS( ',', 'C', 'o' 'n', 'g', 'r', 'a', 't', 'u', 'l', 'a', 't', 'i', 'o', 'n', 's') AS Wishes;
输出:
| Wishes |
| :---------------------------- |
| C,o,n,g,r,a,t,u,l,a,t,i,o,n,s |
示例3: 下面的SELECT查询将两个带有下划线符号的字符串相加。
SELECT CONCAT_WS( '_' 'NEW DELHI IS THE ', 'CAPITAL OF INDIA') AS Sentence;
输出:
| Sentence |
| :-------------------------------- |
| NEW DELHI IS THE_CAPITAL OF INDIA |
示例4:下面的SELECT查询将两个包含符号的字符串相加:
SELECT CONCAT_WS( '+', '#@##9Ski', 'Java958@%^&');
输出:
#@##9Ski+Java958@%^&
示例5:下面的SELECT查询在两个字符串之间使用空格:
SELECT CONCAT_WS( ' ', 'JavaTpoint', 'Website.');
输出:
JavaTpoint Website.
示例6:此示例在结构化查询语言中使用CONCAT_WS函数的表格。
在此示例中,我们需要通过创建一个新的SQL表格来执行CONCAT_WS()函数的列。
在SQL数据库中创建新表格的语法如下:
CREATE TABLE table_name
(
First_Column_of_table Data Type (character_size of 1st Column),
Second_Column_of_table Data Type (character_size of the 2nd column ),
Third_Column_of_table Data Type (character_size of the 3rd column),
...
Last_Column_of_table Data Type (character_size of the Nth column)
);
以下CREATE语句创建了Student_Marks表:
CREATE TABLE Student_Marks
(
Student_ID INT NOT NULL PRIMARY KEY,
Student_First_Name VARCHAR (100),
Student_Middle_Name VARCHAR (100),
Student_Last_Name VARCHAR (100),
Student_Class INT NOT NULL,
Student_City Varchar(120),
Student_State Varchar (80),
Student_Marks INT
);
以下INSERT查询将学院教职员工的记录插入到Student_Marks表中。
INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) VALUES (4001, Aman, Roy, Sharma, 4, Chandigarh, Punjab, 88);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES ( 4002, Vishal, Gurr, Sharma, 8, Murthal, Haryana, 95 );
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4007, Raj, singhania, Gupta, 6, Ghaziabad, Uttar Pradesh, 91);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4004, Yash, Chopra, Singhania, 9, Jaipur, Rajasthan, 85);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4011, Vinay, Sharma, Roy, 8, Chandigarh, Punjab, 94);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4006, Manoj, singhania, Gupta, 5, Ghaziabad, Uttar Pradesh, 83);
INSERT INTO Student_Marks
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)
VALUES (4010, Ram, Raheem, Gupta, 9, Lucknow, Uttar Pradesh, 89);
以下SELECT语句显示了上述Student_Marks表中插入的记录:
SELECT FROM Student_Marks;
Student_Id | Student_First_Name | Student_Middle_Name | Student_Last_Name | Student_Class | Student_City | Student_State | Student_Marks |
---|---|---|---|---|---|---|---|
4001 | Aman | Roy | Sharma | 4 | Chandigarh | Punjab | 88 |
4002 | Vishal | Gurr | Sharma | 8 | Murthal | Haryana | 95 |
4007 | Raj | singhania | Gupta | 6 | Ghaziabad | Uttar Pradesh | 91 |
4004 | Yash | Chopra | Singhania | 9 | Jaipur | Rajasthan | 85 |
4011 | Vinay | Sharma | Roy | 8 | Chandigarh | Punjab | 94 |
4006 | Manoj | Singhania | Gupta | 5 | Ghaziabad | Uttar Pradesh | 83 |
4010 | Ram | Raheem | Gupta | 9 | Lucknow | Uttar Pradesh | 89 |
查询 1: 下面的 SELECT 查询在上述的 Student_Marks 表中使用 CONCAT_WS 函数,针对 Student_First_Name、Student_Middle_Name 和 Student_Last_Name 列:
SELECT Student_First_Name, Student_Middle_Name, Student_Last_Name CONCAT_WS(',',Student_First_Name, Student_Middle_Name, Student_Last_Name) AS Name FROM Student_Marks;
该 SQL 语句连接每个学生的名字、中间名和姓氏的值。
输出:
Student_First_Name | Student_Middle_Name | Student_Last_Name | Name |
---|---|---|---|
Aman | Roy | Sharma | Aman,Roy,Sharma |
Vishal | Gurr | Sharma | Vushal,Gurr,Sharma |
Raj | singhania | Gupta | Raj,singhnia,Gupta |
Yash | Chopra | Singhania | Yash,Chopra,Singhania |
Vinay | Sharma | Roy | Vinay,sharma,Roy |
Manoj | Singhania | Gupta | Manoj,Singhania,Gupta |
Ram | Raheem | Gupta | Ram,Raheem,Gupta |
查询 2: 以下SELECT查询使用CONCAT_WS函数,对上述Student_Marks表中Student_Id大于4002的学生的Student_City和Student_State列进行操作:
SELECT Student_Id, CONCAT_WS(' ', Student_City, Student_State) AS Address FROM Student_Marks WHERE Student_Id >4002;
该SQL语句将城市和州的值以空格相连。
输出:
Student_Id | Address |
---|---|
4007 | Ghaziabad Uttar Pradesh |
4004 | Jaipur Rajasthan |
4011 | Chandigarh Punjab |
4006 | Ghaziabad Uttar Pradesh |
4010 | Lucknow Uttar Pradesh |