dbTalk Databases Forums  

Using a case within a simple update

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Using a case within a simple update in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sashi
 
Posts: n/a

Default Using a case within a simple update - 10-15-2009 , 10:21 AM






Hi all, I'm trying to do something like this.

update myTable
case length(duration)
when 8 then set hours = substr(duration,1,2)
else set hours = substr(duration,1, 1)
end case

Depending on the length of the 'duration' field, I'm trying to extract
either the first or the first two characters.
It ain't workin'.

Is such a construct possible withing pl/sql? The few examples that
I've googled around give easy options for using it within a select but
no examples within an update.

Thanks,
Sashi

Reply With Quote
  #2  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: Using a case within a simple update - 10-15-2009 , 10:28 AM






Sashi wrote:
Quote:
Hi all, I'm trying to do something like this.

update myTable
case length(duration)
when 8 then set hours = substr(duration,1,2)
else set hours = substr(duration,1, 1)
end case

Depending on the length of the 'duration' field, I'm trying to extract
either the first or the first two characters.
It ain't workin'.

Is such a construct possible withing pl/sql? The few examples that
I've googled around give easy options for using it within a select but
no examples within an update.

Thanks,
Sashi
update mytable set hours=case when length(duration)=8 then
substr(duration,1,2) else substr(duration,1,1) end

or shorter

update mytable set
hours=decode(length(duration),8,substr(duration,1, 2),substr(duration,1,1))

(assuming you wish update all rows in your table)
Best regards

Maxim

Reply With Quote
  #3  
Old   
Sasikanth Malladi
 
Posts: n/a

Default Re: Using a case within a simple update - 10-15-2009 , 09:48 PM



On Oct 15, 11:28*am, Maxim Demenko <mdeme... (AT) gmail (DOT) com> wrote:
Quote:
update mytable set hours=case when length(duration)=8 then
substr(duration,1,2) else substr(duration,1,1) end

or shorter

update mytable set
hours=decode(length(duration),8,substr(duration,1, 2),substr(duration,1,1))

(assuming you wish update all rows in your table)
Best regards

Maxim
Great! Thank you!
Sashi
Sashi

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.