SQL CHARACTER_LENGTH函数
结构化查询语言中的CHARACTER_LENGTH字符串函数返回给定字符串的字符数。该函数在SQL中是CHAR_LENGTH函数的同义词。
CHARACTER_LENGTH字符串函数的语法
语法1: 该语法使用带有SQL表的列名的CHARACTER_LENGTH函数:
SELECT CHARACTER_LENGTH(Column_Name) AS Alias_Name FROM Table_Name;
在第一个语法中,我们需要指定要执行CHARACTER_LENGTH字符串函数的列的名称,以便找到每个值的字符数。
语法2: 此语法使用包含字符串的CHARACTER_LENGTH函数:
SELECT CHARACTER_LENGTH(Original_String);
CHARACTER_LENGTH字符串函数示例
示例1: 以下SELECT查询显示给定JAVATPOINT单词的总字符数:
SELECT CHARACTER_LENGTH( ' JAVATPOINT') AS CHARACTER_LENGTH_word;
输出:
CHARACTER_LENGTH_word |
---|
11 |
示例2: 以下SELECT查询显示给定字符串的字符总数:
SELECT CHARACTER_LENGTH( 'JAVATPOINT is a good website') AS CHARACTER_LENGTH_string;
输出:
CHARACTER_LENGTH_string |
---|
28 |
示例 3: 下面的SELECT查询显示给定句子中的前16个字符的CHARACTER_LENGTH:
SELECT CHARACTER_LENGTH( 'NEW DELHI IS THE CAPITAL OF INDIA') AS CHARACTER_LENGTH_Sentence;
输出:
CHARACTER_LENGTH_Sentence |
---|
33 |
示例4: 下面的SELECT查询显示给定字符串的长度:
SELECT CHARACTER_LENGTH( ' ' ) AS CHARACTER_LENGTH_space;
输出:
CHARACTER_LENGTH_space |
---|
1 |
示例5: 下面的SELECT查询显示了NULL单词的长度:
SELECT CHARACTER_LENGTH( NULL ) AS Length;
输出:
Length |
---|
NULL |
示例 6: 这个示例使用了结构化查询语言中的CHARACTER_LENGTH函数来处理表格。
在这个示例中,我们将创建一个新的SQL表格,并在其上执行CHARACTER_LENGTH函数。
在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 表中:
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查询使用了CHARACTER_LENGTH函数与上面Worker_Grade表的First_Name列:
SELECT First_Name, CHARACTER_LENGTH(First_Name) AS CHARACTER_LENGTH_FirstName FROM Worker_Grade;
该语句显示每个工人的名字的长度。
First_Name | CHARACTER_LENGTH_FirstName |
---|---|
Aman | 4 |
Vishal | 6 |
Raj | 3 |
Yash | 4 |
Vinay | 5 |
Manoj | 5 |
Ram | 3 |
查询2: 下面的SELECT查询使用CHARACTER_LENGTH函数与上述Worker_Grade表的Last_Name列:
SELECT Last_Name, CHARACTER_LENGTH(Last_Name) AS CHARACTER_LENGTH_LastName FROM Worker_Grade;
这个语句显示了每个工人的姓氏长度。
输出结果:
Last_Name | CHARACTER_LENGTH_LastName |
---|---|
Sharma | 6 |
Sharma | 6 |
Gupta | 5 |
Singhania | 9 |
Roy | 3 |
Gupta | 5 |
Gupta | 5 |
查询 3: 下面的SELECT查询使用了字符长度函数CHARACTER_LENGTH和上述Worker_Grade表的First_City和New_City列:
SELECT First_City, CHARACTER_LENGTH(First_City) AS CHARACTER_LENGTH_FirstCity, New_City, CHARACTER_LENGTH(New_City) AS CHARACTER_LENGTH_NewCity FROM Worker_Grade;
这个SQL语句显示了每个工人的第一个和新城市的长度。