SQL STRCMP函数

SQL STRCMP函数

STRCMP字符串函数在SQL中比较作为STRCMP函数参数传递的两个字符串。此函数返回0,如果第一个字符串的长度等于第二个字符串的长度。如果第一个字符串的长度大于第二个字符串的长度,则返回1,否则返回-1。

STRCMP字符串函数的语法

语法1: 此语法在SQL表的两列中使用STRCMP函数:

SELECT STRCMP (Column_Name1, column_Name2) AS Alias_Name FROM Table_Name;

在语法中,我们需要定义那些我们想要比较长度的列的名称。

语法3: 此语法使用STRCMP函数与两个字符串:

SELECT STRCMP (String_1, String_2);

STRCMP字符串函数的示例

示例1: 以下SELECT查询比较JAVATPOINT和EXAMPLES字符串的长度:

SELECT STRCMP ('JAVATPOINT', 'EXAMPLES');

输出:

1

示例 2: 以下SELECT查询比较传入STRCMP函数的两个句子的长度:

SELECT STRCMP ('Website is good', 'javatpoint is good');

输出:

-1

示例3: 以下SELECT查询比较了两个城市的长度:

SELECT STRCMP (  'Delhi', 'Noida');

输出:

0

示例4:这个示例使用了带有结构化查询语言中的表的STRCMP函数。

现在,我们创建一个新的SQL表,以帮助理解STRCMP字符串函数。

在SQL数据库中创建新表的语法如下:

CREATE TABLE table_name
(
1st_Column Data Type (character_size of 1st Column),  
2nd_Column Data Type (character_size of the 2nd column ),  
3rd_Column Data Type (character_size of the 3rd column),  
...  

Nth_Column Data Type (character_size of the Nth column)
);  

下面的CREATE语句创建了 Worker_Info 表:

CREATE TABLE Worker_Info
(
Worker_ID INT NOT NULL PRIMARY KEY,  
Worker_First_Name VARCHAR (100),  
Worker_Last_Name VARCHAR (100),  
Worker_Dept_Id INT NOT NULL,
Worker_Address Varchar(120),
Worker_City Varchar (80),
Worker_Salary INT 
);

下面的INSERT查询将行业工人的记录插入到 Worker_Info 表中:

INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1001, Arush, Sharma, 4001, Aman Vihar, Delhi, 20000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1002, Bulbul, Roy, 4002, Nirman Vihar, Delhi, 38000 );
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1004, Saurabh, Sharma, 4001, Sector 128, Mumbai, 45000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1005, Shivani, Singhania, 4001, Vivek Vihar, Kolkata, 42000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1006, Avinash, Sharma, 4002, Sarvodya Calony, Delhi, 28000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary)VALUES (1007, Shyam, Besas, 4003, Krishna Nagar, Lucknow, 35000);

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

SELECT * FROM Worker_Info;
Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Address Worker_City Worker_Salary
1001 Arush Sharma 4001 Aman Vihar Delhi 20000
1002 Bulbul Roy 4002 Nirman Vihar Delhi 38000
1004 Saurabh Roy 4001 Sector 128 Mumbai 45000
1005 Shivani Singhania 4001 Vivek Vihar Kolkata 42000
1006 Avinash Sharma 4002 Sarvodya Calony Delhi 28000
1007 Shyam Besas 4003 Krishna Nagar Lucknow 35000

以下SELECT查询使用了上述Worker_Info表的Worker_First_Name和Worker_City列的STRCMP函数:

SELECT Worker_First_Name, Worker_City, STRCMP (Worker_First_Name, Worker_City) AS Compare_Firstname_City FROM Worker_Info;

此SQL查询比较了每个工作者的名字和城市。

输出:

Worker_First_Name Worker_City Compare_Firstname_City
Arush Delhi 0
Bulbul Delhi 1
Saurabh Mumbai 1
Shivani Kolkata 0
Avinash Delhi 1
Shyam Lucknow -1

下面的SELECT查询在上述Worker_Info表的Worker_first_Name和Worker_Last_Name列中使用STRCMP()函数:

SELECT Worker_First_Name, Worker_Last_Name, STRCMP(Worker_First_Name, Worker_Last_Name) AS STRCMP_Firstname_Lastname FROM Worker_Info;

这个SQL语句比较每个Worker的名字和姓氏。

输出:

Worker_First_Name Worker_Last_Name STRCMP_Firstname_Lastname
Arush Sharma -1
Bulbul Roy 1
Saurabh Roy 1
Shivani Singhania -1
Avinash Sharma 1
Shyam Besas 0

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程