SQL CONCAT_WS 函数
一、SQL CONCAT_WS 函数 语法
SQL语言中的 CONCAT_WS 字符串函数使用分隔符将两个或多个字符串连接到一个字符串中。
语法1:此语法将 CONCAT_WS 函数与 SQL 表的多个列一起使用:
SELECT CONCAT_WS(separator, Column_Name1, column_Name2, Column_Name3,……. Column_NameN) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定那些我们想用特定分隔符连接其值的列的名称。
语法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);
二、SQL 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( ' ', 'Yiidian', 'Website.');
输出结果为:
Yiidian 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 查询将 CONCAT_WS 函数与上述 Student_Marks 表的 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 语句将 city 和 state 的值加上空格。
输出结果为:
Student_Id | Address |
---|---|
4007 | Ghaziabad Uttar Pradesh |
4004 | Jaipur Rajasthan |
4011 | Chandigarh Punjab |
4006 | Ghaziabad Uttar Pradesh |
4010 | Lucknow Uttar Pradesh |
热门文章
优秀文章