
- 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 get the length of all columns and display the result in a single new column?
To get the length of all columns i.e. the count of all the characters for column values, use char_length(). Let us first create a table. Here, we have two columns, therefore we will calculate for each row consisting of both the values FirstName and LastName −
mysql> create table DemoTable ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (1.07 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','Brown'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('David','Miller'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Carol','Taylor'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable −
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Chris | Brown | | David | Miller | | Carol | Taylor | +-----------+----------+ 3 rows in set (0.00 sec)
Following is the query to get the length of all the columns −
mysql> select char_length(concat(FirstName,LastName)) from DemoTable;
This will produce the following output −
+-----------------------------------------+ | char_length(concat(FirstName,LastName)) | +-----------------------------------------+ | 10 | | 11 | | 11 | +-----------------------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- Display substrings of different length with a single MySQL query and combine the result
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Concatenate all the columns in a single new column with MySQL
- Select distinct names from two columns in MySQL and display the result in a single column
- Divide numbers from two columns and display result in a new column with MySQL
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- MySQL query to get the character length for all the values in a column?
- How to round MySQL column with float values and display the result in a new column?
- MySQL query to combine two columns in a single column?
- Select multiple columns and display in a single column in MySQL?
- MySQL query to group by names and display the count in a new column
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- MySQL query to count the duplicate ID values and display the result in a separate column
- Count multiple rows and display the result in different columns (and a single row) with MySQL

Advertisements