tags:

views:

75

answers:

4

Hi

Pl find code below

SQL> desc aaa
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ENAME                                              VARCHAR2(1)

SQL> select * from aaa;

E
-
2
2
2
5
5
5
5

7 rows selected.  

I need to update 2 with 5 and 5 with 2 with a single sql.

+2  A: 

According to the information provided and requirement:

In t-sql you can do this like

update aaa set Ename = case when Ename = '2' then '5' else '2' end

Change case statement with oracle equivalent

Muhammad Kashif Nadeem
+1 Syntax for Oracle is identical.
Tony Andrews
+5  A: 
update
  aaa
set
  ENAME = case when ENAME = '2' then '5' else '2' end
where
  ENAME in ('2', '5')
Constantin
A: 
UPDATE  aaa
SET E = 7 - E
WHERE E IN (5,2)
Michael Pakhantsov
E is varchar, i wonder if it will work?
Constantin
No it wouldn't. Also what would happen to a row where E = 4?
Tony Andrews
`set E = to_char(7 - to_number(E))`
be here now
+2  A: 
update aaa set ename = translate(ename, '25', '52')

or

update aaa set ename = decode(ename, '5', '2', '2', '5', ename)
be here now
This is really interesting..
Pravin Satav
The first will set `ename` to NULL if it was neither '2' or '5'. A `WHERE` clause should be added.
Jeffrey Kemp
it won't, at least in my 9i. check this: `select translate('7', '25', '52') from dual`.
be here now
here's what the manual says: `translate('char', 'from_string', 'to_string')`: TRANSLATE returns `char` with all occurrences of each character in `from_string` replaced by its corresponding character in `to_string`. Characters in `char` that are not in `from_string` are not replaced.
be here now