SQL DATALENGTH函数
结构化查询语言中的DATALENGTH字符串函数返回用于表示该表达式的字节数。
DATALENGTH字符串函数的语法
语法1: 此语法使用DATALENGTH函数和SQL表的列名:
SELECT DATALENGTH(Column_Name) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要对其执行DATALENGTH字符串函数以查找表示字符串或表达式所使用的字节数的列的名称。
语法2: 此语法与字符串一起使用DATALENGTH函数:
SELECT DATALENGTH(Original_String);
DATALENGTH字符串函数的示例
示例1: 以下SELECT查询显示了给定JAVATPOINT单词的总字符数:
SELECT DATALENGTH( ' JAVATPOINT') AS DATALENGTH_word;
输出:
DATALENGTH_word
---
11
示例2: 下面的SELECT查询显示了给定字符串的总字符数:
SELECT DATALENGTH( 'JAVATPOINT is a good website') AS DATALENGTH_string;
输出:
DATALENGTH_string
---
28
示例3: 下面的SELECT查询显示了给定句子的DATALENGTH:
SELECT DATALENGTH( 'NEW DELHI IS THE CAPITAL OF INDIA') AS DATALENGTH_Sentence;
输出:
DATALENGTH_Sentence
---
33
示例 4: 下面的SELECT查询显示了给定字符串的DATALENGTH:
SELECT DATALENGTH( ' ' ) AS DATALENGTH_space;
输出:
DATALENGTH_space
---
1
示例 5: 下面的SELECT查询显示了NULL单词的DATALENGTH:
SELECT DATALENGTH( NULL ) AS DATALENGTH;
输出:
DATALENGTH
---
NULL
示例6:此示例使用DATALENGTH函数和Structured Query Language中的表格。
在这个示例中,我们将创建一个新的SQL表格,在该表格上我们要执行DATALENGTH函数。
在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查询使用DATALENGTH函数与上述Employee_Grade表的First_Name列:
SELECT First_Name, DATALENGTH(First_Name) AS DATALENGTH_FirstName FROM Employee_Grade;
这个语句显示了在该行业工作的每位员工的名字的字节数量。
输出结果
First_Name | DATALENGTH_FirstName |
---|---|
Ramesh | 6 |
Yadu | 4 |
Vijay | 5 |
Bhanu | 5 |
Harry | 5 |
Akash | 5 |
Ram | 3 |
查询2: 以下SELECT查询使用DATALENGTH函数查询上述Employee_Grade表中的Last_Name列:
SELECT Last_Name, DATALENGTH(Last_Name) AS DATALENGTH_LastName FROM Employee_Grade;
该语句显示每个员工的姓氏的字节数。
输出:
Last_Name | DATALENGTH_LastName |
---|---|
Sharma | 6 |
Sharma | 6 |
Ramna | 5 |
Rangopalr | 9 |
Roy | 3 |
Ramna | 5 |
Ramna | 5 |
查询3: 下面的SELECT查询使用了DATALENGTH函数,并选择了上面Employee_Grade表的First_City和New_City列:
SELECT First_City, DATALENGTH(First_City) AS DATALENGTH_FirstCity, New_City, DATALENGTH(New_City) AS DATALENGTH_NewCity FROM Employee_Grade;
这个SQL语句显示了每个员工的第一个和新城市的字节数。
输出:
First_City | DATALENGTH_FirstCity | New_City | DATALENGTH_NewCity |
---|---|---|---|
Lucknow | 7 | Ghaziabad | 9 |
Aurangabad | 10 | Ghaziabad | 9 |
Noida | 5 | Lucknow | 7 |
Ghaziabad | 9 | Lucknow | 7 |
Noida | 5 | Ghaziabad | 9 |
Ghaziabad | 9 | Aurangabad | 10 |
Lucknow | 7 | Aurangabad | 10 |