CHAR_LENGTH Function in SQL
The CHAR_LENGTH string function of Structured Query Language returns the number of characters of the given string or word.
Syntax of CHAR_LENGTH String Function
Syntax1: This syntax uses the CHAR_LENGTH function with the column name of the SQL table:
In this first syntax, we have to specify the name of that column on which we want to execute the CHAR_LENGTH string function for finding the number of characters of each value.
Syntax2: This syntax uses the CHAR_LENGTH function with the string:
Examples of CHAR_LENGTH String function
Example 1: The below query shows the total number of characters of the given tutoraspire word:
Output:
CHAR_LENGTH_word |
11 |
Example 2: The following SELECT query shows the total number of characters of the given string:
SELECT CHAR_LENGTH(‘tutoraspire is a good website’) AS CHAR_LENGTH_string;
Output:
CHAR_LENGTH_string |
28 |
Example 3: The following SELECT query shows the CHAR_LENGTH 16 characters from the given sentence:
SELECT CHAR_LENGTH( ‘NEW DELHI IS THE CAPITAL OF INDIA’) AS CHAR_LENGTH_Sentence;
Output:
CHAR_LENGTH_Sentence |
33 |
Example 4: The following SELECT query shows the length of the given string:
SELECT CHAR_LENGTH( ‘ ‘ ) AS CHAR_LENGTH_space;
Output:
CHAR_LENGTH_space |
1 |
Example 5: The following SELECT query shows the length of the NULL word:
SELECT CHAR_LENGTH( NULL ) AS Length;
Output:
Length |
NULL |
Example 6: This example uses the CHAR_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 Char_Length function.
The below CREATE statement is the syntax for creating the new table in SQL:
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_ID | First_Name | Last_Name | First_City | Second_City | New_City | Attendance_Remarks | Work_Remarks | Grade |
---|---|---|---|---|---|---|---|---|
10 | Aman | Sharma | Lucknow | Chandigarh | Ghaziabad | 88 | 95 | A2 |
02 | Vishal | Sharma | Chandigarh | Ghaziabad | Ghaziabad | 95 | 82 | A1 |
07 | Raj | Gupta | Delhi | Ghaziabad | Lucknow | 91 | 95 | A1 |
04 | Yash | Singhania | Ghaziabad | Delhi | Lucknow | 85 | 82 | A2 |
11 | Vinay | Roy | Delhi | Kanpur | Ghaziabad | 95 | 97 | A1 |
16 | Manoj | Gupta | Ghaziabad | Meerut | Chandigarh | 95 | 90 | B1 |
19 | Ram | Gupta | Lucknow | Ghaziabad | Chandigarh | 89 | 95 | A2 |
Query 1: The following SELECT query uses the CHAR_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_Name | CHAR_LENGTH_FirstName |
---|---|
Aman | 4 |
Vishal | 6 |
Raj | 3 |
Yash | 4 |
Vinay | 5 |
Manoj | 5 |
Ram | 3 |
Query 2: The following SELECT query uses the CHAR_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_Name | CHAR_LENGTH_LastName |
---|---|
Sharma6 | |
Sharma6 | |
Gupta5 | |
Singhania9 | |
Roy3 | |
Gupta5 | |
Gupta5 |
Query 3: The following SELECT query uses the CHAR_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:
First_City | CHARACTER_LENGTH_LastName | New_City | CHARACTER_LENGTH_LastName |
---|---|---|---|
Lucknow | 7 | Ghaziabad | 9 |
Chandigarh | 10 | Ghaziabad | 9 |
Delhi | 5 | Lucknow | 7 |
Ghaziabad | 9 | Lucknow | 7 |
Delhi | 5 | Ghaziabad | 9 |
Ghaziabad | 9 | Chandigarh | 10 |
Lucknow | 7 | Chandigarh | 10 |