Home » SQL & PL/SQL » SQL & PL/SQL » Decode expression help
Decode expression help [message #645045] Tue, 24 November 2015 10:02 Go to next message
redhonda9834
Messages: 3
Registered: November 2015
Junior Member
Hello Everyone,

I have a decode expression i need help understanding.

decode(location_area, alternate_area, location_area, alternate_area) "Current_Location"

Am I correct with this if statement?

if location_area = alternate_area then
current_location = location_area
else
current_location = alternate_area
Re: Decode expression help [message #645046 is a reply to message #645045] Tue, 24 November 2015 10:14 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum

Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read

When all else fails Read The Fine Manual

http://docs.oracle.com/database/121/SQLRF/functions057.htm#SQLRF00631
Re: Decode expression help [message #645047 is a reply to message #645046] Tue, 24 November 2015 13:51 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Shortly, yes - you understood it correctly.
Re: Decode expression help [message #645048 is a reply to message #645047] Tue, 24 November 2015 14:01 Go to previous messageGo to next message
redhonda9834
Messages: 3
Registered: November 2015
Junior Member
Thank you littlefoot. I figured it was correct, but I wanted to double check before making a change that would affect 1000 rows.

Also, I will read up on the forum guide for next time BlackSwan. Sorry about that....
Re: Decode expression help [message #645049 is a reply to message #645048] Tue, 24 November 2015 14:42 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Well, you can try it (i.e. run your UPDATE statement), check whether it is OK or not (by running some SELECT statements) and - if it turns out to be wrong - simply ROLLBACK changes you made. Or, if it is correct, COMMIT them.
Re: Decode expression help [message #645060 is a reply to message #645049] Wed, 25 November 2015 03:19 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
Or do the sensible thing - copy your data onto a test instance and test it there.
Also if you're finding decode hard to follow use CASE instead - it's much more obvious.
Re: Decode expression help [message #645063 is a reply to message #645045] Wed, 25 November 2015 03:34 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
redhonda9834 wrote on Tue, 24 November 2015 21:32

decode(location_area, alternate_area, location_area, alternate_area) "Current_Location"

Am I correct with this if statement?

if location_area = alternate_area then
current_location = location_area
else
current_location = alternate_area


As CM said, if DECODE is difficult for you to understand, then you could use CASE which is verbose and easy to interpret.

CASE 
WHEN 
   location_area = alternate_area
THEN 
  location_area
ELSE 
   alternate_area
END


It's exactly like your IF-ELSE interpretation, only the IF becomes CASE WHEN and END IF becomes END CASE. It would work both in SQL and PL/SQL.

Read more in documentation https://docs.oracle.com/database/121/LNPLS/case_statement.htm#LNPLS01304

[Updated on: Wed, 25 November 2015 03:42]

Report message to a moderator

Re: Decode expression help [message #645070 is a reply to message #645063] Wed, 25 November 2015 07:10 Go to previous message
redhonda9834
Messages: 3
Registered: November 2015
Junior Member
Thank you for the feed back everyone. That is awesome.
Previous Topic: Insert into multiple blocks
Next Topic: count of tests
Goto Forum:
  


Current Time: Sun Jul 12 20:47:47 CDT 2026