CHARACTER_LENGTH Function in SQL

The CHARACTER_LENGTH string function of Structured Query Language returns the number of characters of the given string. This function is a synonym of CHAR_LENGTH function in SQL.

Syntax of CHARACTER_LENGTH String Function

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

In the first syntax, we have to specify the name of that column on which we want to execute the CHARACTER_LENGTH string function for finding the number of characters of each value.

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

Examples of CHARACTER_LENGTH String function

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

Output:

CHARACTER_LENGTH_word
11

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

Output:

CHARACTER_LENGTH_string
28

Example 3: The following SELECT query shows the CHARACTER_LENGTH 16 characters from the given sentence:

Output:

CHARACTER_LENGTH_Sentence
33

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

Output:

CHARACTER_LENGTH_space
1

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

Output:

Length
NULL

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

In this example, we are going to create a new SQL table on which we want to execute the CHARACTER_LENGTH function.

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

The following CREATE statement creates the Worker_Grade table:

The below INSERT queries insert the records of Workers with Grades and Remarks in the Worker_Grade table:

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


Worder_IDFirst_NameLast_NameFirst_CitySecond_CityNew_CityAttendance_RemarksWork_RemarksGrade
10AmanSharmaLucknowChandigarhGhaziabad8895A2
02VishalSharmaChandigarhGhaziabadGhaziabad9582A1
07RajGuptaDelhiGhaziabadLucknow9195A1
04YashSinghaniaGhaziabadDelhiLucknow8582A2
11VinayRoyDelhiKanpurGhaziabad9597A1
16ManojGuptaGhaziabadMeerutChandigarh9590B1
19RamGuptaLucknowGhaziabadChandigarh8995A2

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

This statement shows the length of the first name of each worker.

First_NameCHARACTER_LENGTH_FirstName
Aman4
Vishal6
Raj3
Yash4
Vinay5
Manoj5
Ram3

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

This statement shows the length of the last name of each worker.

Output:

Last_NameCHARACTER_LENGTH_LastName
Sharma6
Sharma6
Gupta5
Singhania9
Roy3
Gupta5
Gupta5

Query 3: The following SELECT query uses the CHARACTER_LENGTH function with the First_City and New_City columns of the above Worker_Grade table:

This SQL statement shows the length of the first and new city of each worker.

Output:






Latest Courses