
- 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
Display substring in MySQL if the string is less than a specific length or display a custom message if it is more?
For this, you can use substring() function in MySQL. For conditions, use MySQL CASE statement. Let us first create a −
mysql> create table DemoTable1402 -> ( -> EmployeeName varchar(40) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1402 values('Adam Smith'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1402 values('Chris Brown'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1402 values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1402 values('Carol Taylor'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select −
mysql> select * from DemoTable1402;
This will produce the following output −
+--------------+ | EmployeeName | +--------------+ | Adam Smith | | Chris Brown | | David Miller | | Carol Taylor | +--------------+ 4 rows in set (0.00 sec)
Here is the query to display substring in MySQL if the string is less than a specific length or display a custom message if it is more −
mysql> select *, case when char_length(EmployeeName) <=11 then substring(EmployeeName,1,5) -> else 'EmployeeName is greater than 11' -> end as Result -> from DemoTable1402;
This will produce the following output −
+--------------+---------------------------------+ | EmployeeName | Result | +--------------+---------------------------------+ | Adam Smith | Adam | | Chris Brown | Chris | | David Miller | EmployeeName is greater than 11 | | Carol Taylor | EmployeeName is greater than 11 | +--------------+---------------------------------+ 4 rows in set (0.04 sec)
- Related Articles
- MySQL IF() to display custom YES or NO messages
- How To Check If Time Is Greater Than Or Less Than A Specific Time In Excel?
- How to display the result more readable if the string is long stored in MySQL VARCHAR?
- Count values greater and less than a specific number and display count in separate MySQL columns?
- MySQL query to display a substring before a special character in a string
- Check if table exists in MySQL and display the warning if it exists?
- Display the warning message when a FLOAT value is inserted into DECIMAL in MySQL?
- Display records with more than two occurrences in MySQL?
- Check if the frequency of any character is more than half the length of the string in Python
- MySQL query that returns a specific string if column is null?
- Find if there is a path of more than k length from a source in Python
- Find if there is a path of more than k length from a source in C++
- Is it possible to display substring from object entries in JavaScript?
- Display only a specific duplicate record in MySQL
- How to check if string or a substring of string starts with substring in Python?

Advertisements