SQL CHAR_LENGTH函数

SQL CHAR_LENGTH函数

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

CHAR_LENGTH字符串函数的语法

语法1: 此语法使用SQL表的列名与CHAR_LENGTH函数一起使用:

SELECT CHAR_LENGTH(Column_Name) AS Alias_Name FROM Table_Name;

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

语法2: 这个语法使用CHAR_LENGTH函数与字符串:

SELECT CHAR_LENGTH(Original_String);

CHAR_LENGTH字符串函数的示例

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

SELECT CHAR_LENGTH(' JAVATPOINT') AS CHAR_LENGTH_word;

输出:

CHAR_LENGTH_word
11

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

SELECT CHAR_LENGTH(‘JAVATPOINT是一个很好的网站’) AS CHAR_LENGTH_string;

输出:

CHAR_LENGTH_string
28

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

SELECT CHAR_LENGTH(’NEW DELHI IS THE CAPITAL OF INDIA’)作为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函数和Structured Query Language中的表格。

在此示例中,我们将创建一个新的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表中的Worker等级和备注的记录插入:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程