SQL REPLACE函数
这个Structured Query Language的字符串函数用给定的新字符或子字符串替换原始字符串中的所有出现。该函数还将列的值替换为新值。
REPLACE字符串函数的语法
语法1: 这种语法使用SQL表的列名与REPLACE函数一起使用:
SELECT REPLACE(Column_Name, Character/string_to_replace, new_String/character ) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要替换其值的列的名称。
语法2: 该语法使用带有字符串的REPLACE函数:
SELECT REPLACE(Original_String, String_to_Replace, New_String) AS Alias_Name;
语法 3: 此语法使用 REPLACE 函数和单个字符:
SELECT REPLACE(Original_String, character_to_Replace, New_Character) AS Alias_Name;
REPLACE字符串函数示例
示例 1: 下面的SELECT查询将原始字符串中的字符“R”替换为“P”:
SELECT REPLACE( 'JAVATROINT', 'R', 'P' ) AS Website_Name;
输出:
Website_Name
---
JAVATPOINT
示例2: 下面的SELECT查询将原始字符串中所有的字符’S’替换为新字符’T’:
SELECT REPLACE( 'JAVASPOINS', 'S', 'T') AS Website_Name;
输出:
Website_Name
---
JAVATPOINT
示例3: 下面的SELECT查询将会把原始字符串中的子字符串’Tutorials’替换为新的单词’Articles’:
SELECT REPLACE( 'JavaTpoint provides various Tutorials.', 'Tutorials', 'Articles') AS JavaTpoint_Sentence;
输出:
JavaTpoint_Sentence
JAVATPOINT提供各种文章。
例子 4: 下面的SELECT查询将原始字符串中的符号替换为一个新符号:
SELECT REPLACE( '####98221545###', '#', '$') AS Replace_Symbol ;
输出:
Replace_Symbol
---
$$98221545
示例5: 下面的SELECT查询将原始字符串中的年份替换为:
SELECT REPLACE( '2021JavaTpoint2021', '2021', '2022');
输出:
2022JavaTpoint2022
示例6:这个例子展示了如何在结构化查询语言中使用REPLACE函数和表格合并。
在这个示例中,我们需要创建一个新的SQL表格来执行REPLACE()函数在列上的操作。在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_Last_Name列的REPLACE函数:
SELECT Student_Last_Name, REPLACE(Student_Last_Name, 'a', 'r') AS REPLACE_a_r FROM Student_Marks;
这个SQL语句将会替换每个学生的姓中所有出现的 ‘a’ 为 ‘r’:
输出:
Student_Last_Name | Replace_a_r |
---|---|
Sharma | Shrrmr |
Sharma | Shrrmr |
Gupta | Guptr |
Singhania | Singhrnir |
Roy | Roy |
Gupta | Guptr |
Gupta | Guptr |
查询2: 下面的SELECT查询使用REPLACE函数与上面学生分数表中Student_Id大于4002的学生的Student_City和Student_State列:
SELECT Student_Id, REPLACE(Student_City, 'Chandigarh', 'Munnar'), REPLACE(Student_State, 'Punjab', 'Kerala ) FROM Student_Marks WHERE Student_Id >4002;
这个SQL语句替换了那些学生中Student_ID大于4002的人的Chandigarh城市和Punjab州。
输出:
Student_Id | REPLACE(Student_City, ‘Chandigarh’, ‘Munnar’) | REPLACE(Student_State, ‘Punjab’, ‘Kerala ) |
---|---|---|
4007 | Ghaziabad | Uttar Pradesh |
4004 | Jaipur | Rajasthan |
4011 | Munnar | Kerala |
4006 | Ghaziabad | Uttar Pradesh |
4010 | Lucknow | Uttar Pradesh |