

- 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
How do I force the column alias to be of specific data type in MySQL?
For this, you can use CASE statement. Let us first create a table −
mysql> create table DemoTable1505 -> ( -> Value integer unsigned, -> Status tinyint(1) -> ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1505 values(20,0); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1505 values(45,1); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1505;
This will produce the following output −
+-------+--------+ | Value | Status | +-------+--------+ | 20 | 0 | | 45 | 1 | +-------+--------+ 2 rows in set (0.00 sec)
Here is the query to force the column alias to be of specific data type −
mysql> select case status -> when 0 then cast(Value as signed)*1 -> when 1 then cast(Value as signed)*-1 -> end as AllValues from DemoTable1505;
This will produce the following output −
+-----------+ | AllValues | +-----------+ | 20 | | -45 | +-----------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- How do you force MySQL LIKE to be case sensitive?
- How do I add more members to my ENUM - type column in MySQL?
- How to export specific column data in MySQL?
- How can I modify an existing MySQL column’s data type?
- How can we change the data type of the column in MySQL table?
- How do I exclude a specific record in MySQL?
- How to alter the data type of a MySQL table’s column?
- How do I cast a type to a BigInt in MySQL?
- How to set time data type to be only HH:MM in MySQL?
- How do I modify a MySQL column to allow NULL?
- How do I update the decimal column to allow more digits in MySQL?
- How can I modify an existing column's data type?
- How do I alter a MySQL table column defaults?
- How do I set the default value for a column in MySQL?
- How do I set the timezone of MySQL?
Advertisements