
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to get the longest VarChar length in MySQL?
To get the longest varchar length, you need to use CHAR_LENGTH().
The syntax is as follows
SELECT Max(CHAR_LENGTH(yourColumnName)) AS anyAliasName FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table CharLengthDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Sentence varchar(255) - > ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into CharLengthDemo(Sentence) values('Java is an object oriented programming language' - > ); Query OK, 1 row affected (0.45 sec) mysql> insert into CharLengthDemo(Sentence) values('MySQL is a Relational Database Management System'); Query OK, 1 row affected (0.26 sec) mysql> insert into CharLengthDemo(Sentence) values('JSP uses Java Programming Language'); Query OK, 1 row affected (0.24 sec) mysql> insert into CharLengthDemo(Sentence) values('Object is a root class in java'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from CharLengthDemo;
The following is the output
+----+--------------------------------------------------+ | Id | Sentence | +----+--------------------------------------------------+ | 1 | Java is an object oriented programming language | | 2 | MySQL is a Relational Database Management System | | 3 | JSP uses Java Programming Language | | 4 | Object is a root class in java | +----+--------------------------------------------------+ 4 rows in set (0.00 sec)
Here is the query to get the longest varchar length
mysql> SELECT Max(CHAR_LENGTH(Sentence)) AS MAXLENGTH FROM CharLengthDemo;
The following is the output displaying the value with the longest varchar length
+-----------+ | MAXLENGTH | +-----------+ | 48 | +-----------+ 1 row in set (0.00 sec)
- Related Articles
- How to get the length of longest string in a PHP array
- What is the maximum length of MySQL VARCHAR column?
- How to update a column of varchar type in MySQL to increase its length?
- MySQL query to get the max value with numeric values in varchar field?
- How to alter a MySQL Column from varchar(30) to varchar(100)?
- MySQL query to get max id from varchar type and the values in numeric?
- How to get a value more than a particular value from varchar column in MySQL?
- How to cast from VARCHAR to INT in MySQL?
- How do I get the average string length in MySQL?
- C++ Program to get length of longest subsequence in n times copied sequence
- Get maximum date from a list of varchar dates in MySQL
- How to sum varchar column and display the count in MySQL?
- Convert varchar to date in MySQL?
- How to convert varchar “time” to real time in MySQL?
- Convert varchar to unsigned integer in MySQL

Advertisements