SQL CONCAT 函数
一、SQL CONCAT 函数 语法
SQL CONCAT 函数 添加两个或多个字符或字符串以在结果中形成一个新字符串。如果您在函数中只传递一个字符串,那么它会在输出中显示错误。因此,CONCAT() 函数至少需要两个字符串。
语法1:此语法使用带有 SQL 表的两个或多个列名的 CONCAT 函数:
SELECT CONCAT(Column_Name1, column_Name2, Column_Name3,....... Column_NameN) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要连接其值的那些列的名称。
语法2:此语法使用带有两个或多个字符串的 CONCAT 函数:
SELECT CONCAT(String1, String2, String3, ...... StringN);
语法3:此语法使用带有两个或多个字符的 CONCAT 函数:
SELECT CONCAT(Character_1, character_2, Character_3, ......., Character_N);
二、SQL CONCAT 函数 示例
示例 1:以下 SELECT 查询添加两个字符以形成一个新字符串:
SELECT CONCAT( 'H', 'i' ) AS ;
输出结果为:
'Hi'
示例 2:以下 SELECT 查询通过添加超过 2 个字符来提供一个新字符串:
SELECT CONCAT( 'C', 'o' 'n', 'g', 'r', 'a', 't', 'u', 'l', 'a', 't', 'i', 'o', 'n', 's') AS Wishes;
输出结果为:
Wishes |
---|
Congratulations |
示例 3:以下 SELECT 查询添加两个字符串:
SELECT CONCAT( 'NEW DELHI IS THE ', 'CAPITAL OF INDIA') AS Sentence;
输出结果为:
Sentence |
---|
NEW DELHI IS THE CAPITAL OF INDIA |
示例 4:以下 SELECT 查询添加了两个以上的字符串:
SELECT CONCAT( 'Have', 'a', 'Nice', 'day', '.' );
输出结果为:
Have a Nice Day
示例 5:以下 SELECT 查询添加了这两个包含符号的字符串:
SELECT CONCAT( '#@##9Ski', ' Java958@%^&');
输出结果为:
#@##9Ski Java958@%^&
示例 6:以下 SELECT 查询在两个字符串之间使用空格:
SELECT CONCAT( 'Yiidian', ' ', 'Website.');
输出结果为:
JavaTpoint Website.
示例 7:此示例将 CONCAT 函数与结构化查询语言中的表一起使用。
在这个例子中,我们必须创建一个新的 SQL 表,通过它我们将在列上执行 Concat() 函数。在 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 函数与上述 Student_Marks 表的 Student_First_Name、Student_Middle_Name 和 Student_Last_Name 列一起使用:
SELECT Student_First_Name, Student_Middle_Name, Student_Last_Name CONCAT(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 | AmanRoySharma |
Vishal | Gurr | Sharma | VishalGurrSharma |
Raj | singhania | Gupta | RajsinghniaGupta |
Yash | Chopra | Singhania | YashChopraSinghania |
Vinay | Sharma | Roy | VinaysharmaRoy |
Manoj | Singhania | Gupta | ManojSinghaniaGupta |
Ram | Raheem | Gupta | RamRaheemGupta |
查询 2:以下 SELECT 查询将 CONCAT 函数与上述 Student_Marks 表中 Student_Id 大于 4002 的学生的 Student_City 和 Student_State 列一起使用:
SELECT Student_Id, CONCAT(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 |
热门文章
优秀文章