SQL ELT函数
结构化查询语言中的ELT字符串函数根据给定的索引号从字符串列表中返回字符串。如果在给定的索引位置找不到字符串,则此函数返回NULL。
ELT字符串函数的语法
在SQL中,我们可以使用ELT函数与表的列、字符串和字符一起使用。
语法 1:
SELECT ELT(Index_Value, Column_Name1, column_Name2, Column_Name3,……. Column_NameN) AS Alias_Name FROM Table_Name;
在这个语法中,我们使用了带有现有SQL表的ELT函数。在这里,我们需要定义表的名称和列,我们想要执行ELT函数的列。
语法2:
SELECT ELT(Index_Value, "String1", "String2", "String3", …… "StringN") AS Alias_Name;
在这个语法中,我们使用了带有字符串列表的ELT函数。
语法3:
SELECT ELT(Index_Value, "Character_1", "character_2", "Character_3", ……., "Character_N") AS Alias_Name;
在这个语法中,我们使用ELT函数和字符列表。
ELT字符串函数的示例
示例1: 以下查询使用ELT函数和字符串列表:
SELECT ELT( 5, ''H'', ''I'', ''A'', ''P'', ''Q'', ''S'', ''R'', ''T'', ''V'', ''M'' ) AS String_at_1stPosition;
输出:
String_at_1thPosition
---
H
示例2: 以下查询搜索了不在字符串列表中的“旧”字符串:
SELECT ELT(7, ''New'', ''Delhi'', ''is'', ''the'', ''Capital'', ''of'', ''India'') AS String_at_7thPosition;
输出:
String_at_7thPosition
---
India
示例3: 以下是使用ELT函数和数字列表的SELECT查询示例:
SELECT ELT(5, ''this'', ''is'', ''the'', ''JavaTpoint'' ) AS String_at_5thPosition;
输出:
String_at_5thPosition
---
NULL
示例4:此示例使用结构化查询语言中的ELT函数与表格。
在这第四个示例中,我们将通过创建新的表格来执行ELT函数与表格值:
以下代码块显示了在SQL中创建新表格的语法:
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语句创建 Fresher_Marks 表:
CREATE TABLE Fresher_Marks
(
Fresher_ID INT PRIMARY KEY,
First_Name VARCHAR (100),
Middle_Name Varchar(120),
Last_Name VARCHAR (200),
City Varchar(120),
Aptitude_Marks INT,
Reasoning_Marks INT,
Technical_Marks INT,
Percentage INT
);
下面的INSERT查询将新生的记录插入到Fresher_Marks表中,包括成绩和详细信息:
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (501, Vinay, Roy, Gupta, Lucknow, 85, 92, 78, 85 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (502, Monu, Roy, Singhania, Chandigarh, 54, 68, 98, 88 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (504, Ravi, Roy, Kumar, Lucknow, 71, 82, 69, 71 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (507, Shyam, Roy, Sharma, Delhi, 85, 90, 68, 78 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (510, Abhay, Kumar, Gupta, Chandigarh, 45, 68, 82, 78);
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (509, Riya, Roy, Sharma, Delhi, 68, 90, 69, 91 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (505, Vishal, Kumar, Sharma, Mumbai, 75, 65, 88, 75 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (512, Romika, Roy, Kumar, Mumbai, 78, 95, 68, 85 );
以下SELECT语句显示了上述 Fresher_Marks 表中插入的记录:
SELECT * FROM Fresher_Marks;
Fresher_ID | First_Name | Middle_Name | Last_Name | City | Aptitude_Marks | Reasoning_Marks | Technical_Marks | Percentage |
---|---|---|---|---|---|---|---|---|
501 | Vinay | Roy | Gupta | Lucknow | 85 | 92 | 78 | 85 |
502 | Monu | Roy | Singhania | Chandigarh | 54 | 68 | 98 | 88 |
504 | Ravi | Roy | Kumar | Lucknow | 71 | 82 | 69 | 71 |
507 | Shyam | Roy | Sharma | Delhi | 85 | 90 | 68 | 78 |
510 | Abhay | Kumar | Gupta | Chandigarh | 45 | 68 | 82 | 78 |
509 | Riya | Roy | Sharma | Delhi | 68 | 90 | 69 | 91 |
505 | Vishal | Kumar | Sharma | Mumbai | 75 | 65 | 88 | 75 |
512 | Romika | Roy | Kumar | Mumbai | 78 | 95 | 68 | 85 |
查询 1: 以下 SELECT 查询在上述个人成绩表的 First_Name、Middle_Name 和 Last_Name 列中使用了 ELT 函数:
SELECT First_Name, Middle_Name, Last_Name, ELT(3, First_Name, Middle_Name, Last_Name) AS Value_at_3 FROM Fresher_Marks;
这个SQL语句显示了上述表中每个新人的第3个索引位置的值。 rd
输出:
First_Name | Middle_Name | Last_Name | Value_at_3 |
---|---|---|---|
Vinay | Roy | Gupta | Gupta |
Monu | Roy | Singhania | Singhania |
Ravi | Roy | Kumar | Kumar |
Shyam | Roy | Sharma | Sharma |
Abhay | Kumar | Gupta | Gupta |
Riya | Roy | Sharma | Sharma |
Vishal | Kumar | Sharma | Sharma |
Romika | Roy | Kumar | Kumar |
查询 2: 下面的SELECT查询在上面的Fresher_Marks表中使用了ELT函数,该函数使用Aptitude_Marks、Reasoning_Marks和Technical_Marks列的值,其中Fresher_ID大于502的那些新人:
SELECT Fresher_ID, Aptitude_Marks, Reasoning_Marks, Technical_Marks, ELT(2, Aptitude_Marks, Reasoning_Marks, Technical_Marks) AS Number_at_2nd_position FROM Fresher_Marks WHERE Fresher_ID > 502;
输出:
Fresher_ID | Aptitude_Marks | Reasoning_Marks | Technical_Marks | Number_at_2nd_position |
---|---|---|---|---|
504 | 71 | 82 | 69 | 82 |
507 | 85 | 90 | 68 | 90 |
510 | 45 | 68 | 82 | 68 |
509 | 68 | 90 | 69 | 90 |
505 | 75 | 65 | 88 | 65 |
512 | 78 | 95 | 68 | 95 |