

- 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
Can we replace a number with a String in a MySQL result set?
Yes, we can do that using the CASE statement. Let us first create a table −
mysql> create table DemoTable -> ( -> isMarried boolean -> ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+-----------+ | isMarried | +-----------+ | 1 | | 0 | | 0 | | 1 | +-----------+ 4 rows in set (0.00 sec)
Here is the query to replace a number with a String in a MySQL result set −
mysql> select isMarried, CASE isMarried WHEN 1 THEN 'Married' ELSE 'Not Married' END AS Status from DemoTable -> ;
Output
+-----------+-------------+ | isMarried | Status | +-----------+-------------+ | 1 | Married | | 0 | Not Married | | 0 | Not Married | | 1 | Married | +-----------+-------------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- Can we order a MySQL result with mathematical operations?
- How can we handle a result set inside MySQL stored procedure?
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- How can we get only unique values of a column in MySQL result set?
- In MySQL, how can we pad a string with another string?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- How can we get the summary output of a column in MySQL result set itself?
- How can we modify a MySQL view with CREATE OR REPLACE VIEW statement?
- How can we extract a substring from a string in MySQL?
- Can we set a single value in MySQL SELECT IN()?
- How can we get all the unique rows in MySQL result set?
- Can we create a database with a numeric name with MySQL?
- How i can replace number with string using Python?
- Can we use the result of a SUM() function in MySQL WHERE clause
- In MySQL, how can we randomize set of rows or values in the result set?
Advertisements