

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- Concatenate all the columns in a single new column with MySQL
- MySQL query to count occurrences of distinct values and display the result in a new column?
- 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
- MySQL query to get the character length for all the values in a column?
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- How to round MySQL column with float values and display the result in a new column?
- MySQL query to get the count of values and display the count in a new column ordered in descending order
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- MySQL query to count the duplicate ID values and display the result in a separate column
- MySQL query to group by names and display the count in a new column
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- Select multiple columns and display in a single column in MySQL?
Advertisements