SQL CONCAT函数

SQL CONCAT函数

该字符串函数将两个或多个字符或字符串相加,形成一个新的字符串。如果在函数中只传递一个字符串,则在输出中显示错误。因此,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);

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(  'JavaTpoint', ' ', 'Website.');

输出:

JavaTpoint Website.  

示例 7: 此示例使用Structured Query Language中的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查询使用了上面的Student_Marks表中的Student_First_Name、Student_Middle_Name和Student_Last_Name列的CONCAT函数:

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查询使用了Student_Marks表中Student_Id大于4002的学生的Student_City和Student_State列与CONCAT函数的组合:

SELECT Student_Id, CONCAT(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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程