SQL BIT_LENGTH 函数
一、SQL BIT_LENGTH 函数 语法
SQL语言的 BIT_LENGTH 字符串函数以位为单位返回字符串的长度。
语法1:此语法使用 BIT_LENGTH 函数和 SQL 表的列名:
SELECT BIT_LENGTH(Column_Name) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要在其上执行 BIT_LENGTH 字符串函数以查找字符串长度(以位为单位)的列的名称。
语法2:此语法使用带有字符串的 BIT_LENGTH 函数:
SELECT BIT_LENGTH(Original_String);
二、SQL BIT_LENGTH 函数 示例
示例 1:以下 SELECT 查询以位为单位显示字的长度:
SELECT BIT_LENGTH( 'YIIDIAN') AS BIT_LENGTH_word;
输出结果为:
Output:
BIT_LENGTH_word |
---|
56 |
示例 2:以下 SELECT 查询显示给定字符串的长度(以位为单位):
SELECT BIT_LENGTH( 'YIIDIAN is a good website') AS BIT_LENGTH_string;
输出结果为:
BIT_LENGTH_string |
---|
200 |
示例 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 函数。
在这里,我们将创建一个要在其上执行 BIT_LENGTH 函数的新 SQL 表。
下面的 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表的插入记录:
SELECT * FROM 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 查询对上述 Employee_Grade 表的 First_City 和 New_City 列使用 BIT_LENGTH 函数:
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 |
热门文章
优秀文章