
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - LEN() Function
The SQL LEN() function is used to retrieve the length of the characters of a string. The LENGTH() is the synonym of the LEN() function i.e we can use either of these to calculate the length. It accepts a string value as a parameter and returns the number of characters present in a String. The number of characters will be the actual length of the given String.
If we pass a NULL value as an argument, this function will return NULL. The trailing spaces at the end of the string are not included in the length. However, leading spaces at the beginning of the string are included.
Syntax
Following is the syntax of the SQL LEN() function with a string −
LEN ( expression )
Parameters
- expression − It is a string whose length is to be counted.
Return value
This function returns the length of the given string.
Example
In the following example,we are using the SQL LEN() function to retrieve the length of the String âTutorialsPointâ.
SELECT LEN('TutorialsPointâ') as Length_of_String;
Output
Following is the output of the above query −
+------------------+ | Length_of_String | +------------------+ | 15 | +------------------+
Example
If we pass an empty string as an argument to the function, this function returns zero.
In the following example, we are passing an empty string as an argument to the SQL LEN() function to get the length of the string.
SELECT LEN('') as Length_of_String;
Output
The above SQL query produces the following output −
+------------------+ | Length_of_String | +------------------+ | 0 | +------------------+
Example
If any argument passes as a NULL value as an argument to the function, it returns NULL.
In this example, we are passing a NULL value as an argument to the LEN() function to find the length of the given value.
SELECT LEN(NULL);
Output
On executing the above query, it will produce the following output −
+--------------+ | LEN(NULL) | +--------------+ | NULL | +--------------+
Example
You can pass a table column as an argument to the LEN() function to find the length of the content of the column. Assume we have created a table named CUSTOMERS using the CREATE statement as follows −
CREATE TABLE CUSTOMERS( ID INT NOT NULL, FIRST_NAME VARCHAR (20), LAST_NAME VARCHAR(20), AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2));
Now let's insert four records into the customers table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (2, 'Ramesh','KUMAR', 32, 'Ahmedabad', 2000.00 ); INSERT INTO CUSTOMERS VALUES (3, 'Khilan','Verma', 25, 'Delhi', 1500.00 ); INSERT INTO CUSTOMERS VALUES (3, 'kaushik','Gupta', 23, 'Kota', 2000.00 ); INSERT INTO CUSTOMERS VALUES (5, 'Chaitali','Pal', 25, 'Mumbai', 6500.00 );
The Following SQL query retrieve the length of the content of the FIRST_NAME column of Customers table −
SELECT ID, FIRST_NAME, LAST_NAME, LEN(FIRST_NAME) AS NAME_LENGTH FROM CUSTOMERS;
Output
After executing the above statement, it produces the following output −
+----+------------+-----------+-------------+ | ID | FIRST_NAME | LAST_NAME | NAME_LENGTH | +----+------------+-----------+-------------+ | 2 | Ramesh | KUMAR | 6 | | 3 | Khilan | Verma | 6 | | 3 | kaushik | Gupta | 7 | | 5 | Chaitali | Pal | 8 | +----+------------+-----------+-------------+
Example
You can also pass the numeric value to the LEN() function. In this example, we are passing a numeric value 123455 to LEN() function to retrieve the length of the passed value.
SELECT LEN(123455);
Output
After executing the above statement, it produces the following output −
+----------------+ | LEN(123455) | +----------------+ | 6 | +----------------+