SQL OCTET_LENGTH函数

SQL OCTET_LENGTH函数

结构化查询语言中的OCTET_LENGTH字符串函数返回给定字符串或单词的字符数。

OCTET_LENGTH字符串函数的语法

语法1: 此语法使用SQL表的列名与OCTET_LENGTH函数结合使用:

SELECT OCTET_LENGTH(Column_Name) AS Alias_Name FROM Table_Name;

在这个第一个语法中,我们需要指定我们想要执行OCTET_LENGTH字符串函数的列的名称,以找到每个值的字符数。

语法2: 这个语法使用OCTET_LENGTH函数与字符串一起使用:

SELECT OCTET_LENGTH(Original_String);

OCTET_LENGTH字符串函数的示例

范例1: 下面的查询显示了给定JAVATPOINT单词的字符总数:

SELECT OCTET_LENGTH(  '  JAVATPOINT') AS OCTET_LENGTH_word;

输出:

OCTET_LENGTH_word   
---  
11   

示例2: 下面的SELECT查询显示给定字符串的总字符数:

SELECT OCTET_LENGTH(  'JAVATPOINT is a good website') AS OCTET_LENGTH_string;

输出:

OCTET_LENGTH_string   
---  
28   

示例3: 以下SELECT查询显示给定句子中的前16个字符的OCTET_LENGTH:

SELECT OCTET_LENGTH(  'NEW DELHI IS THE CAPITAL OF INDIA') AS OCTET_LENGTH_Sentence;

输出:

OCTET_LENGTH_Sentence   
---  
33   

示例 4: 下面的SELECT查询显示了给定字符串的长度:

SELECT OCTET_LENGTH(  '  ' ) AS OCTET_LENGTH_space;

输出:

OCTET_LENGTH_space   
---  
1   

示例5: 下面的SELECT查询显示了NULL单词的长度:

SELECT OCTET_LENGTH( NULL ) AS Length;

输出:

Length   
---  
NULL   

示例6:此示例使用结构化查询语言中的OCTET_LENGTH函数与表格。

在该示例中,我们将创建一个新的SQL表格,其中我们想要执行OCTET_LENGTH函数。

下面的CREATE语句是在SQL中创建新表格的语法:

CREATE TABLE table_name
(
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语句创建了 Worker_Grade 表:

CREATE TABLE Worker_Grade
(
Worder_ID INT PRIMARY KEY,  
First_Name VARCHAR (100),  
Last_Name VARCHAR (100), 
First_City Varchar(120),
Second_City Varchar(120),
New_City Varchar(120),
Attendance_Remarks INT, 
Work_Remarks INT, 
Grade Varchar (80)
);

以下是将Worker_Grade表中具有成绩和备注的Workers的记录插入的INSERT查询:

INSERT INTO Worker_Grade (Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (10, Aman, Sharma, Lucknow Chandigarh, Ghaziabad, 88, 95, A2);

INSERT INTO Worker_Grade 
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) 
VALUES ( 02, Vishal, Sharma, Chandigarh, Ghaziabad, Delhi, 95, 82, A1 );

INSERT INTO Worker_Grade 
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) 
VALUES (07, Raj, Gupta, Delhi, Ghaziabad, Lucknow, 91, 95, A1);

INSERT INTO Worker_Grade 
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)  
VALUES (04, Yash, Singhania, Ghaziabad, Delhi, Lucknow, 85, 82, A2);

INSERT INTO Worker_Grade 
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) 
VALUES (11, Vinay, Roy, Delhi, Kanpur, Ghaziabad, 95, 97, A1);

INSERT INTO Worker_Grade 
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) 
VALUES (16, Manoj, Gupta, Ghaziabad, Meerut, Chandigarh, 95, 90, B1);


INSERT INTO Worker_Grade 
(Worder_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) 
VALUES (19, Ram, Gupta, Lucknow, Ghaziabad, Chandigarh, 89, 95, A2);

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

SELECT * FROM Worker_Grade;   
Worder_ID First_Name Last_Name First_City Second_City New_City Attendance_Remarks Work_Remarks Grade
10 Aman Sharma Lucknow Chandigarh Ghaziabad 88 95 A2
02 Vishal Sharma Chandigarh Ghaziabad Ghaziabad 95 82 A1
07 Raj Gupta Delhi Ghaziabad Lucknow 91 95 A1
04 Yash Singhania Ghaziabad Delhi Lucknow 85 82 A2
11 Vinay Roy Delhi Kanpur Ghaziabad 95 97 A1
16 Manoj Gupta Ghaziabad Meerut Chandigarh 95 90 B1
19 Ram Gupta Lucknow Ghaziabad Chandigarh 89 95 A2

查询1: 以下SELECT查询使用上面的Worker_Grade表中的First_Name列的OCTET_LENGTH函数:

SELECT First_Name, OCTET_LENGTH(First_Name) AS OCTET_LENGTH_FirstName FROM Worker_Grade;

这个声明显示了每个工人的名字的长度。

First_Name OCTET_LENGTH_FirstName
Aman 4
Vishal 6
Raj 3
Yash 4
Vinay 5
Manoj 5
Ram 3

查询2: 以下SELECT查询使用了OCTET_LENGTH函数与上述Worker_Grade表的Last_Name列:

SELECT Last_Name, OCTET_LENGTH(Last_Name) AS OCTET_LENGTH_LastName FROM Worker_Grade;

这个陈述显示每个工人的姓的长度。

输出:

Last_Name OCTET_LENGTH_LastName
Sharma 6
Sharma 6
Gupta 5
Singhania 9
Roy 3
Gupta 5
Gupta 5

查询3: 下面的SELECT查询使用了上面Worker_Grade表中的First_City和New_City列的OCTET_LENGTH函数:

SELECT First_City, OCTET_LENGTH(First_City) AS OCTET_LENGTH_FirstCity, New_City, OCTET_LENGTH(New_City) AS OCTET_LENGTH_NewCity FROM Worker_Grade;

这个 SQL 语句显示了每个工人的第一个和新城市的长度。

输出:

First_City OCTET_LENGTH_LastName New_City OCTET_LENGTH_LastName
Lucknow 7 Ghaziabad 9
Chandigarh 10 Ghaziabad 9
Delhi 5 Lucknow 7
Ghaziabad 9 Lucknow 7
Delhi 5 Ghaziabad 9
Ghaziabad 9 Chandigarh 10
Lucknow 7 Chandigarh 10

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程