1

So I have this table wherein it has columns named AMEX and CashOnly. Both of them have values of either 1 or 0. Now, instead of 1 and 0, I want to change all the '1' values to 'YES' and all the '0' values to 'NO' so it would be much more understandable to the viewer.

What is the most efficient SQL query I could use to accomplish this?

2
  • What are the types of your AMEX and CashOnly columns? Commented Jul 1, 2016 at 2:34
  • AMEX and CashOnly are set to int. After changing it to varchar, what should be the proper "UPDATE" query? Commented Jul 1, 2016 at 2:41

4 Answers 4

1

Use a single UPDATE statement:

UPDATE yourTable
SET AMEX     = CASE WHEN AMEX = '0'     THEN 'NO' ELSE 'YES' END,
    CashOnly = CASE WHEN CashOnly = '0' THEN 'NO' ELSE 'YES' END

Note that this assumes that you have converted your AMEX and CashOnly columns to VARCHAR. Also note that there is no WHERE clause because you want to update all records in your table.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I tried it but it gave an error saying "#1064 - Erreur de syntaxe près de 'SET CashOnly = CASE WHEN CashOnly = '0' THEN 'NO' ELSE 'YES' END' à la ligne 6
@lexus Sorry I had an extra SET statement in there. It should work now.
0

Just use a case statement:

select (case when AMEX = 1 then 'YES' else 'NO' end) as AMEX,
       (case when cash = 1 then 'YES' else 'NO' end) as cash
. . .

2 Comments

Hi, it worked, however, it only selected the field entries and not really changed the value. I think the proper keyword should be "UPDATE" but I am not sure about the syntax on how to use it for this kind of issue.
Use a view instead. Don't change the values. The existing value is an integer and you want to change it to a character.
0

If you want to change your column type then you need to do following: (As int field can't store yes/no as value)

create 2 extra enum field name them whatever you want for now. let them be amex1 and cash1. declare their values as 'yes', 'no' set default value as no.

Now execute following 2 queries one by one.

UPDATE table_name SET amex1 = 'yes' WHERE amex = 1;
UPDATE table_name SET cash1 = 'yes' WHERE cash = 1;

Now delete cash and amex column rename cash1 and amex1 to cash and amex respectively. hope this helps you.

Comments

0

try this :

update table set amex=(case when AMEX = 1 then 'YES' else 'NO' end) ,
CashOnly=(case when CashOnly= 1 then 'YES' else 'NO' end);

OR

update table set amex='Yes' where amex=1;
update table set amex='No' where amex=0;
update table set CashOnly='Yes' where CashOnly=1;
update table set CashOnly='No' where CashOnly=0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.