
- 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
MySQL query to add dots if string has more than 10 words?
For this, use CASE statement. Let us first create a table −
mysql> create table DemoTable -> ( -> Title text -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('My name is John and this is my first tutorial on MySQL'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('This is Carol and I work on MongoDB'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------------------------------------------------------+ | Title | +--------------------------------------------------------+ | My name is John and this is my first tutorial on MySQL | | This is Carol and I work on MongoDB | +--------------------------------------------------------+ 2 rows in set (0.00 sec)
Following is the query to add dots if string has more than 10 words −
mysql> select (case when substring_index(tbl.Title, ' ', 10) = tbl.Title then tbl.Title -> else concat(substring_index(tbl.Title, ' ', 10), '....................') -> end) AS AddDots -> from DemoTable tbl;
Output
This will produce the following output −
+-------------------------------------------------------------------+ | AddDots | +-------------------------------------------------------------------+ | My name is John and this is my first tutorial.................... | | This is Carol and I work on MongoDB | +-------------------------------------------------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to check if a certain row has only two words?
- MySQL search if more than one string contains special characters?\n
- MySQL query to find a value appearing more than once?
- MySQL query to fetch date more recent than 14 days?
- MySQL query to count where more than three columns values are true?
- MySQL query to add 0's to numbers with less than 9 digits?
- MySQL query to check if a string contains a word?
- MySQL query to find all rows where string contains less than four characters?
- MongoDB query to find records with keys containing dots?
- MySQL query to display only 15 words from the left?
- How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?
- MySQL query to select top 10 records?
- MySQL query to return a string as a result of IF statement?
- MySQL query to delete table rows if string in cell is matched
- MySQL query to search a string ‘demo’ if someone mistakenly types ‘deom’?

Advertisements