Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change value from 1 To Yes In MySQL select statement

Tags:

sql

mysql

case

I'm only able to SELECT on a table.

The table has a column called inty with a value of 0 or 1

I am currently selecting inty as:

SELECT inty AS InternetApproved FROM Table1

Is there a way to reformat the data within the SQL SELECT so that if the value is 0 make it No and if the value is 1 make it Yes for display purposes in the output SELECT results?

like image 239
Rocco The Taco Avatar asked Feb 21 '14 13:02

Rocco The Taco


People also ask

How do you assign a selected value to a variable in MySQL?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.

What is the difference between SELECT * and SELECT 1?

There is no difference between EXISTS with SELECT * and SELECT 1. SQL Server generates similar execution plans in both scenarios. EXISTS returns true if the subquery returns one or more records. Even if it returns NULL or 1/0.


2 Answers

Simple and easy way to achieve this is:

SELECT IF(inty = 1, 'YES', 'No') AS internetApproved FROM Table1
like image 154
Deepak Rai Avatar answered Sep 30 '22 02:09

Deepak Rai


SELECT 
CASE 
    WHEN inty = 0 then 'No'
    WHEN inty = 1 then 'Yes'
    ELSE 'Maybe'
END
AS InternetApproved
FROM Table1
like image 20
Linger Avatar answered Sep 30 '22 02:09

Linger