SQL CHAR_LENGTH 函数

一、SQL CHAR_LENGTH 函数 语法

SQL语言的 CHAR_LENGTH 字符串函数返回给定字符串或单词的字符数。

语法1:此语法使用 CHAR_LENGTH 函数和 SQL 表的列名:

SELECT CHAR_LENGTH(Column_Name) AS Alias_Name FROM Table_Name;  

在第一种语法中,我们必须指定要在其上执行 CHAR_LENGTH 字符串函数以查找每个值的字符数的列的名称。

语法2:此语法使用 CHAR_LENGTH 函数和字符串:

SELECT CHAR_LENGTH(Original_String);

二、SQL CHAR_LENGTH 函数 示例

示例 1:以下查询显示给定 YIIDIAN 单词的字符总数:

SELECT CHAR_LENGTH(' YIIDIAN') AS CHAR_LENGTH_word;  

输出结果为:

CHAR_LENGTH_word
8

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

SELECT CHAR_LENGTH('YIIDIAN is a good website') AS CHAR_LENGTH_string;

输出结果为:

CHAR_LENGTH_string
25

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

SELECT CHAR_LENGTH( 'NEW DELHI IS THE CAPITAL OF INDIA') AS CHAR_LENGTH_Sentence;

输出结果为:

CHAR_LENGTH_Sentence
33

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

SELECT CHAR_LENGTH( ' ' ) AS CHAR_LENGTH_space;

输出结果为:

CHAR_LENGTH_space
1

示例 5:以下 SELECT 查询显示 NULL 字的长度:

SELECT CHAR_LENGTH( NULL ) AS Length;

输出结果为:

Length
NULL

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

在此示例中,我们将创建一个新的 SQL 表,我们要在该表上执行 Char_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)  
);  

下面的 INSERT 语句在Worker_Grade表中插入了 Workers with Grades 和 Remarks 的记录:

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 查询将 CHAR_LENGTH 函数与上述 Worker_Grade 表的 First_Name 列一起使用:

SELECT First_Name, CHAR_LENGTH(First_Name) AS CHAR_LENGTH_FirstName FROM Worker_Grade;  

此语句显示每个工人的名字的长度。

输出结果为:

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

查询 2:以下 SELECT 查询将 CHAR_LENGTH 函数与上述 Worker_Grade 表的 Last_Name 列一起使用:

SELECT Last_Name, CHAR_LENGTH(Last_Name) AS CHAR_LENGTH_LastName FROM Worker_Grade;  

此语句显示每个工人的姓氏长度。

输出结果为:

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

查询 3:以下 SELECT 查询将 CHAR_LENGTH 函数与上述 Worker_Grade 表的 First_City 和 New_City 列一起使用:

SELECT First_City, CHAR_LENGTH(First_City) AS CHAR_LENGTH_FirstCity, New_City, CHAR_LENGTH(New_City) AS CHAR_LENGTH_NewCity FROM Worker_Grade; 

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

输出结果为:

First_City CHARACTER_LENGTH_LastName New_City CHARACTER_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

热门文章

优秀文章