SQL ELT 函数

一、SQL ELT 函数 语法

SQL语言中的 ELT 字符串函数根据给定的索引号从字符串列表中返回字符串。如果在给定的索引位置没有找到字符串,则此函数返回 NULL。

在 SQL 中,我们可以对表的列、字符串和字符使用 ELT 函数。

语法 1

SELECT ELT(Index_Value, Column_Name1, column_Name2, Column_Name3,……. Column_NameN) AS Alias_Name FROM Table_Name;  

在这种语法中,我们将 ELT 函数与现有的 SQL 表一起使用。在这里,我们必须定义要在其上执行 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 函数。

二、SQL 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:以下 SELECT 查询使用带有数字列表的 ELT 函数:

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 查询将 ELT 函数与上述 Fresher_Marks 表的 First_Name、Middle_Name、Last_Name 列一起使用:

SELECT First_Name, Middle_Name, Last_Name, ELT(3, First_Name, Middle_Name, Last_Name) AS Value_at_3 FROM Fresher_Marks;  

该 SQL 语句显示了上表中每个更新的第 3 个索引位置的值。

输出结果为:

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 查询将 ELT 函数与上述 Fresher_Marks 表中 Fresher_ID 大于 502 的那些新生的 Aptitude_Marks、Reasoning_Marks 和 Technical_Marks 列一起使用:

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

热门文章

优秀文章