- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 convert height format to centimeters?
For this, use the CAST() method in MySQL. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentHeight varchar(40) ) ; Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentHeight) values('5\'10\"'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentHeight) values('4\'6\"'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentHeight) values('5\'8\"'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+---------------+ | Id | StudentHeight | +----+---------------+ | 1 | 5'10" | | 2 | 4'6" | | 3 | 5'8" | +----+---------------+ 3 rows in set (0.00 sec)
Following is the query to convert height format to centimeters −
mysql> select (cast(substr(StudentHeight,1,locate("'",StudentHeight)-1) as unsigned)*30.48)+ (cast(substr(StudentHeight,locate("'",StudentHeight)+1) as unsigned)*2.54) AS Centimeters from DemoTable;
This will produce the following output −
+-------------+ | Centimeters | +-------------+ | 177.80 | | 137.16 | | 172.72 | +-------------+ 3 rows in set, 3 warnings (0.05 sec)
- Related Articles
- How to convert US date format to MySQL format in INSERT query?
- Python Program to Read Height in Centimeters and convert the Height to Feet and Inches
- MySQL query to convert YYYY-MM-DD to DD Month, YYYY date format
- Convert MySQL Unix-Timestamp format to date format?
- Convert VARCHAR data to MySQL date format?
- MySQL query to convert timediff() to seconds?
- MySQL query to convert timestamp to month?
- Golang Program to Convert Centimeters into Feet and Inches
- How to convert Python date format to 10-digit date format for mysql?
- How to convert a date format in MySQL?
- Convert string (varchar) to timestamp format in MySQL?
- MySQL Query to convert from datetime to date?
- MySQL query to convert empty values to NULL?
- Convert output of MySQL query to UTF8?
- MySQL select to convert numbers to millions and billions format?

Advertisements