Swap column value in the same table

Write a query for swap value of a column in same row and table

Example:


Table: Employee
ID  FNAME       LNAME
1   Narottam    Singh
2   Ram         Singh

Result :

ID  FNAME   LNAME
1   Singh   Narottam
2   Singh   Ram


Approach

Mysql

// on safe mode for update
SET SQL_SAFE_UPDATES = 0;
// For all row
update TABLE_1 set FNAME=LNAME, LNAME=FNAME;
// for particular row
update TABLE_1 set FNAME=LNAME, LNAME=FNAME WHERE ID=1;


Oracle

// For all row
update TABLE_1 set FNAME=LNAME, LNAME=FNAME;
// for particular row
update TABLE_1 set FNAME=LNAME, LNAME=FNAME WHERE ID=1;

No comments:

Post a Comment