Javatpoint Logo
Javatpoint Logo

DATALENGTH Function in SQL

The DATALENGTH string function of Structured Query Language returns the number of bytes used to indicate the expression.

Syntax of DATALENGTH String Function

Syntax1: This syntax uses the DATALENGTH function with the column name of the SQL table:

In the syntax, we have to specify the name of that column on which we want to perform the DATALENGTH string function for finding the number of bytes used to represent the string or expression.

Syntax2: This syntax uses the DATALENGTH function with the string:

Examples of DATALENGTH String function

Example 1: The following SELECT query shows the total number of characters of the given JAVATPOINT word:

Output:

DATALENGTH_word
11

Example 2: The following SELECT query shows the total number of characters of the given string:

Output:

DATALENGTH_string
28

Example 3: The following SELECT query shows the DATALENGTH of the given sentence:

Output:

DATALENGTH_Sentence
33

Example 4: The following SELECT query shows the DATALENGTH of the given string:

Output:

DATALENGTH_space
1

Example 5: The following SELECT query shows the DATALENGTH of the NULL word:

Output:

DATALENGTH
NULL

Example 6: This example uses the DATALENGTH function with the table in Structured Query Language.

In this example, we will create a new SQL table on which we want to execute the DATALENGTH function.

The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Employee_Grade table:

The below INSERT queries insert the records of Employees with Grades and Remarks in the Employee_Grade table:

The following SELECT statement displays the inserted records of the above Employee_Grade table:


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

Query 1: The following SELECT query uses the DATALENGTH function with the First_Name column of the above Employee_Grade table:

This statement shows the number of bytes of the first name of each employee who works in the industry.

Output

First_Name DATALENGTH_FirstName
Ramesh 6
Yadu 4
Vijay 5
Bhanu 5
Harry 5
Akash 5
Ram 3

Query 2: The following SELECT query uses the DATALENGTH function with the Last_Name column of the above Employee_Grade table:

This statement shows the number of bytes of the last name of each employee.

Output:

Last_Name DATALENGTH_LastName
Sharma 6
Sharma 6
Ramna 5
Rangopalr 9
Roy 3
Ramna 5
Ramna 5

Query 3: The following SELECT query uses the DATALENGTH function with the First_City and New_City column of the above Employee_Grade table:

This SQL statement shows the number of bytes of the first and new city of each employee.

Output:


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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA