GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Database  >  SQL

 Print  |  
Question:  There is a Eno & gender in a table. Eno has primary key and gender has a check constraints for the values 'M' and 'F'. While inserting the data into the table M was misspelled as F and F as M. What is the update statement to replace F with M and M with F?

Answer: CREATE TABLE temp(
eno NUMBER CONSTRAINTS pk_eno PRIMARY KEY,
gender CHAR(1) CHECK (gender IN( 'M','F')));

INSERT INTO temp VALUES ('01','M');
INSERT INTO temp VALUES ('02','M');
INSERT INTO temp VALUES ('03','F');
INSERT INTO temp VALUES ('04','M');
INSERT INTO temp VALUES ('05','M');
INSERT INTO temp VALUES ('06','F');
INSERT INTO temp VALUES ('07','M');
INSERT INTO temp VALUES ('08','F');

COMMIT;


UPDATE temp SET gender =DECODE(gender,'M','F','F','M');

commit;


February 02, 2007 23:49:52 #2
 marutalksin   Member Since: February 2007    Total Comments: 10 

RE: There is a Eno & gender in a table. Eno has primar...
 
we can swap like

a=b;
b=c
c=a;

where a=f or F
           b=m or M
           c=temp
or

if gender = 'F'
then convert gender = 'm'
else if gender = 'M'
then convert gender = 'f'
// if we want only in caps
else if gender = 'f'
then convert gender = 'M'
else if gender = 'm'
then convert gender = 'F'
     

 

Back To Question