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_IDFirst_NameLast_NameFirst_CitySecond_CityNew_CityAttendance_RemarksWork_RemarksGrade
10RameshSharmaLucknowAurangabadGhaziabad8895A2
02YaduSharmaAurangabadGhaziabadGhaziabad9582A1
07VijayRamnaNoidaGhaziabadLucknow9195A1
04BhanuRangopalrGhaziabadNoidaLucknow8582A2
11HarryRoyNoidaKanpurGhaziabad9597A1
16AkashRamnaGhaziabadMeerutAurangabad9590B1
19RamRamnaLucknowGhaziabadAurangabad8995A2

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_NameDATALENGTH_FirstName
Ramesh6
Yadu4
Vijay5
Bhanu5
Harry5
Akash5
Ram3

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_NameDATALENGTH_LastName
Sharma6
Sharma6
Ramna5
Rangopalr9
Roy3
Ramna5
Ramna5

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_CityDATALENGTH_FirstCityNew_CityDATALENGTH_NewCity
Lucknow7Ghaziabad9
Aurangabad10Ghaziabad9
Noida5Lucknow7
Ghaziabad9Lucknow7
Noida5Ghaziabad9
Ghaziabad9Aurangabad10
Lucknow7Aurangabad10





Latest Courses