SQL REPLICATE函数

SQL REPLICATE函数

REPLICATE是SQL中的字符串函数。它将输入的字符串在输出中重复给定次数。

REPLICATE字符串函数的语法

语法1: 此语法描述了如何使用REPLICATE与结构化表的字段。

SELECT REPLICATE(Column_Name, Repetation_value) AS Alias_Name FROM Table_Name;

如果我们想要执行复制函数,那么我们必须指定我们想要重复的表中的列的名称。

语法2: 此语法描述了如何使用字符串或句子和REPLICATE函数:

SELECT REPLICATE(Original_String, Repetation_value) AS Alias_Name;

语法3: 此语法描述如何使用REPLICATE函数与个别字符:

SELECT REPLICATE(Character, Repetation_value) AS Alias_Name;

REPLICATE字符串函数的例子

例子1: 下面的SELECT查询在结果中将JavaTpoint字符串重复四次显示:

SELECT REPLICATE(  'JAVATPOINT ', 4)AS REPLICATE_4_string;

输出:

REPLICATE_4_string   
---  
JAVATPOINT JAVATPOINT JAVATPOINT JAVATPOINT   

示例2: 下面的SELECT查询在输出结果中显示给定的单词集两次:

SELECT REPLICATE(  'NEW DELHI IS THE CAPITAL OF INDIA', 2) AS REPLICATE_2_string;

输出:

REPLICATE_2_string   
---  
NEW DELHI IS THE CAPITAL OF INDIANEW DELHI IS THE CAPITAL OF INDIA

示例 3: 以下SELECT查询在结果中显示了5次S字符:

SELECT REPLICATE(  'S', 5) AS REPLICATE_5_character;
REPLICATE_5_character   
---  
SSSSS   

示例 4: 下面的SELECT查询将在结果中显示一组数共六次:

SELECT REPLICATE(  '9825 ', 6) AS REPLICATE_6_numbers;
REPLICATE_6_numbers  
---  
9825 9825 9825 9825 9825 9825   

示例5: 在这个例子中,我们将对表的字符串和整数列执行REPLICATE函数。

所以,首先我们将创建一个新的表,通过这个表我们将执行REPLICATE函数。

以下的CREATE查询显示了如何在结构化查询语言中创建新表:

CREATE TABLE Name_of_New_Table
(
First_Column_of_table Data Type (character_size of First Column),  
Second_Column_of_table Data Type (character_size of the Second column ),  
Third_Column_of_table Data Type (character_size of the Third column),  
.......,  
Last_Column_of_table Data Type (character_size of the Last column)
);  

现在,我们使用上面的CREATE语法创建新的表格 Student_Sem_Marks 这将帮助存储大学学生每学期的成绩。

CREATE TABLE Student_Sem_Marks
(
Student_ID INT,
Student_First_Name Varchar(80),
Student_Last_Name Varchar(80),
First_Sem INT,
Second_Sem INT,
Third_Sem INT,
Fourth_Sem INT, 
Total INT, 
Percentage INT,
Grade Varchar(10)
);

下面是多个INSERT查询,将学生的姓名和学期成绩插入记录中:

INSERT INTO Student_Sem_Marks (Student_ID, Student_First_Name, Student_Last_Name, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Total, Percentage, Grade) VALUES (11254, Akash, Sharma, 58.857, 65.547, 98.245, 60.254, 282.9038, 70, B1);

INSERT INTO Student_Sem_Marks (Student_ID, Student_First_Name, Student_Last_Name, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Total, Percentage, Grade) VALUES (11250, Ram, Sharan, 28.247, 58.987, 78.548, 77.254, 243.036, 60, C2);

INSERT INTO Student_Sem_Marks (Student_ID, Student_First_Name, Student_Last_Name, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Total, Percentage, Grade) VALUES (11255, Manoj, Gupta, 55.847, 25.548, 68.244, 88.785, 238.424, 78, B1);

INSERT INTO Student_Sem_Marks (Student_ID, Student_First_Name, Student_Last_Name, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Total, Percentage, Grade) VALUES (11257, Parul, Chaudhary, 85.547, 98.278, 62.525, 35.858, 281.935, 65, B2);

INSERT INTO Student_Sem_Marks (Student_ID, Student_First_Name, Student_Last_Name, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Total, Percentage, Grade) VALUES (11251, Monu, Sharma, 72.254, 63.354, 36.258, 96.639, 268.505, 67, B2);

INSERT INTO Student_Sem_Marks (Student_ID, Student_First_Name, Student_Last_Name, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Total, Percentage, Grade) VALUES (11258, Srishti, Chaudhary, 85.589, 78.478, 98.785, 89.698, 352.55, 88, A2);

INSERT INTO Student_Sem_Marks (Student_ID, Student_First_Name, Student_Last_Name, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Total, Percentage, Grade) VALUES (11260, Bhavesh, Bardiya, 78.256, 87.658, 82.963, 91.365, 340.242, 85, A2);

以下SELECT语句显示了上述 Student_Sem_Marks 表中插入的记录:

SELECT * FROM Student_Sem_Marks; 
Student_ID Student_First_Name Student_Last_Name First_Sem Second_Sem Third_Sem Fourth_Sem Total Percentage Grade
11254 Akash Sharma 58.857 65.547 98.245 60.254 282.903 70 B1
11250 Ram Sharan 28.247 58.987 78.548 77.254 243.036 60 C2
11255 Manoj Gupta 55.847 25.548 68.244 88.785 238.424 78 B1
11257 Parul Chaudhary 85.547 98.278 62.525 35.858 281.935 65 B2
11251 Monu Sharma 72.254 63.354 36.258 96.639 268.505 67 B2
11258 Srishti Chaudhary 85.589 78.478 98.785 89.698 352.55 88 A2
11260 Bhavesh Bardiya 78.256 87.658 82.963 91.365 340.242 85 A2

查询1: 下面的SELECT查询使用了上述Student_Sem_Marks表中的Student_First_Name列的REPLICATE函数:

SELECT Student_First_Name, REPLICATE(Student_First_Name, 3) AS REPLICATE_3FirstName FROM Student_Sem_Marks;

这个SQL语句在结果中显示每个学生的名字三次。

输出:

Student_First_Name REPLICATE_3FirstName
Akash AkashAkashAkash
Ram RamRamRam
Manoj ManojManojManoj
Parul ParulParulParul
Monu MonuMonuMonu
Srishti SrishtiSrishtiSrishti
Bhavesh BhaveshBhaveshBhavesh

查询2: 下面的SELECT查询使用了上面的Student_Sem_Marks表中的Student_Last_Name列的REPLICATE函数:

SELECT Student_Last_Name, REPLICATE(Student_Last_Name, 2) AS REPLICATE_2LastName FROM Student_Sem_Marks;

这个SQL语句在结果中以两次的方式显示了每个学生的姓氏。

输出:

Student_Last_Name REPLICATE_2LastName
Sharma SharmaSharma
Sharan SharanSharan
Gupta GuptaGupta
Chaudhary ChaudharyChaudhary
Sharma SharmaSharma
Chaudhary ChaudharyChaudhary
Bardiya BardiyaBardiya

查询 3: 下面的 SELECT 查询使用了上述 Student_Sem_Marks 表的 Grade 列的 REPLICATE 函数:

SELECT Grade, REPLICATE(Grade, 4) AS REPLICATE_4Grade FROM Student_Sem_Marks;

这个SQL语句在结果中显示每个学生的成绩四次。

输出:

Grade REPLICATE_4Grade
B1 B1B1B1B1
C2 C2C2C2C2
B1 B1B1B1B1
B2 B2B2B2B2
B2 B2B2B2B2
A2 A2A2A2A2
A2 A2A2A2A2

查询4: 以下SELECT查询使用了上述Student_Sem_Marks表的名字和姓氏列的复制函数拼接:

SELECT Student_First_Name, Student_Last_Name, REPLICATE(Student_First_Name + Student_Last_Name, 2) AS REPLICATE_2Studentname FROM Student_Sem_Marks;

这个SQL语句在结果中显示了每个学生的名字和姓两次。

输出结果:

Student_First_Name Student_Last_Name REPLICATE_2Studentname
Akash Sharma AkashSharmaAkashSharma
Ram Sharan RamSharanRamSharan
Manoj Gupta ManojGuptaManojGupta
Parul Chaudhary ParulChaudharyParulChaudhary
Monu Sharma MonuSharmaMonuSharma
Srishti Chaudhary SrishtiChaudharySrishtiChaudhary
Bhavesh Bardiya BhaveshBardiyaBhaveshBardiya

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程