SQL BIT_LENGTH函数
Structured Query Language(SQL)的BIT_LENGTH字符串函数返回字符串的位长度。
BIT_LENGTH字符串函数的语法
语法1: 此语法使用SQL表的列名与BIT_LENGTH函数一起使用:
SELECT BIT_LENGTH(Column_Name) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要对其执行BIT_LENGTH字符串函数以查找字符串位数的列的名称。
语法2: 此语法使用BIT_LENGTH函数与字符串:
SELECT BIT_LENGTH(Original_String);
计算机领域的BIT_LENGTH字符串函数示例
示例1: 以下SELECT查询显示了单词的位长度:
SELECT BIT_LENGTH( 'JAVATPOINT') AS BIT_LENGTH_word;
输出:
BIT_LENGTH_word
---
80
示例 2: 下面的SELECT查询显示了给定字符串的位长度:
SELECT BIT_LENGTH( 'JAVATPOINT is a good website') AS BIT_LENGTH_string;
输出:
BIT_LENGTH_string
---
224
示例 3: 下面的SELECT查询显示了给定句子的位长度:
SELECT BIT_LENGTH( 'NEW DELHI IS THE CAPITAL OF INDIA') AS BIT_LENGTH_Sentence;
输出:
BIT_LENGTH_Sentence
---
264
示例4: 以下SELECT查询显示给定空间的位长度:
SELECT BIT_LENGTH( ' ' ) AS BIT_LENGTH_space;
输出:
BIT_LENGTH_space
---
8
示例 5: 下面的SELECT查询显示了NULL单词的位长度:
SELECT BIT_LENGTH( NULL ) AS Length;
输出:
Length
---
NULL
示例6:本示例使用BIT_LENGTH函数与结构化查询语言中的表格一起使用。
这里,我们要在一个新的SQL表格上执行BIT_LENGTH函数。
下面的CREATE TABLE语句是在数据库中创建新的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语句创建了 Employee_Grade 表:
CREATE TABLE Employee_Grade
(
Employee_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查询将员工的等级和备注记录插入到 Employee_Grade 表中:
INSERT INTO Employee_Grade (Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade) VALUES (10, Ramesh, Sharma, Lucknow Aurangabad, Ghaziabad, 88, 95, A2);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES ( 02, Yadu, Sharma, Aurangabad, Ghaziabad, Noida, 95, 82, A1 );
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (07, Vijay, Ramna, Noida, Ghaziabad, Lucknow, 91, 95, A1);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (04, Bhanu, Rangopalr, Ghaziabad, Noida, Lucknow, 85, 82, A2);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (11, Harry, Roy, Noida, Kanpur, Ghaziabad, 95, 97, A1);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (16, Akash, Ramna, Ghaziabad, Meerut, Aurangabad, 95, 90, B1);
INSERT INTO Employee_Grade
(Employee_ID, First_Name, Last_Name, First_City, Second_City, New_City, Attendance_Remarks, Work_Remarks, Grade)
VALUES (19, Ram, Ramna, Lucknow, Ghaziabad, Aurangabad, 89, 95, A2);
以下SELECT语句显示了上述 Employee_Grade 表中插入的记录:
Employee_ID | First_Name | Last_Name | First_City | Second_City | New_City | Attendance_Remarks | Work_Remarks | Grade |
---|---|---|---|---|---|---|---|---|
10 | Ramesh | Sharma | Lucknow | Aurangabad | Ghaziabad | 88 | 95 | A2 |
02 | Yadu | Sharma | Aurangabad | Ghaziabad | Ghaziabad | 95 | 82 | A1 |
07 | Vijay | Ramna | Noida | Ghaziabad | Lucknow | 91 | 95 | A1 |
04 | Bhanu | Rangopalr | Ghaziabad | Noida | Lucknow | 85 | 82 | A2 |
11 | Harry | Roy | Noida | Kanpur | Ghaziabad | 95 | 97 | A1 |
16 | Akash | Ramna | Ghaziabad | Meerut | Aurangabad | 95 | 90 | B1 |
19 | Ram | Ramna | Lucknow | Ghaziabad | Aurangabad | 89 | 95 | A2 |
查询 1: 以下 SELECT 查询使用 BIT_LENGTH 函数与上述 Employee_Grade 表的 First_Name 列:
SELECT First_Name, BIT_LENGTH(First_Name) AS BIT_LENGTH_FirstName FROM Employee_Grade;
此语句显示每个员工名的长度(以位为单位)。
First_Name | BIT_LENGTH_FirstName |
---|---|
Ramesh | 48 |
Yadu | 32 |
Vijay | 40 |
Bhanu | 40 |
Harry | 40 |
Akash | 40 |
Ram | 24 |
查询2: 下面的SELECT查询使用了BIT_LENGTH函数和上述Employee_Grade表的Last_Name列:
SELECT Last_Name, BIT_LENGTH(Last_Name) AS BIT_LENGTH_LastName FROM Employee_Grade;
这个语句显示每个员工姓氏的位数。
输出:
Last_Name | BIT_LENGTH_LastName |
---|---|
Sharma | 48 |
Sharma | 48 |
Ramna | 40 |
Rangopalr | 72 |
Roy | 24 |
Ramna | 40 |
Ramna | 40 |
查询3: 下面的SELECT查询使用了BIT_LENGTH函数,以前面Employee_Grade表中的First_City和New_City列为参数:
SELECT First_City, BIT_LENGTH(First_City) AS BIT_LENGTH_FirstCity, New_City, BIT_LENGTH(New_City) AS BIT_LENGTH_NewCity FROM Employee_Grade;
该SQL语句显示了每个员工的第一个和新城市的长度(以位为单位)。
输出:
First_City | BIT_LENGTH_FirstCity | New_City | BIT_LENGTH_NewCity |
---|---|---|---|
Lucknow | 56 | Ghaziabad | 72 |
Aurangabad | 80 | Ghaziabad | 72 |
Noida | 40 | Lucknow | 56 |
Ghaziabad | 72 | Lucknow | 56 |
Noida | 40 | Ghaziabad | 72 |
Ghaziabad | 72 | Aurangabad | 80 |
Lucknow | 56 | Aurangabad | 80 |