Home » SQL & PL/SQL » SQL & PL/SQL » Simulating If Then Else in Decode
Simulating If Then Else in Decode [message #313165] Thu, 10 April 2008 23:55 Go to next message
sandhyaa
Messages: 79
Registered: February 2007
Member
can we simulate a IF THEN ELSE in a decode function?

Can we do something like this:


SELECT DECODE(NAME, LENGTH(NAME) < 20, 'LESS THAN TWENTY', LENGTH(NAME)=20, 'TWENTY', LENGTH(NAME)>20, 'GREATER THAN TWENTY') FROM tableA

Thanks
Sandi
Re: Simulating If Then Else in Decode [message #313167 is a reply to message #313165] Thu, 10 April 2008 23:59 Go to previous messageGo to next message
rajatratewal
Messages: 507
Registered: March 2008
Location: INDIA
Senior Member
Why you wan't to use decode only and not another option??

Why don't you use case for this.
Re: Simulating If Then Else in Decode [message #313168 is a reply to message #313165] Fri, 11 April 2008 00:02 Go to previous messageGo to next message
bibsdash
Messages: 47
Registered: April 2008
Member
well, I would have tried this way before posting here


SELECT DECODE('NAME', LENGTH('NAME') < 20, 'LESS THAN TWENTY', LENGTH('NAME')=20, 'TWENTY', LENGTH('NAME')>20, 'GREATER THAN TWENTY') FROM dual

Re: Simulating If Then Else in Decode [message #313170 is a reply to message #313168] Fri, 11 April 2008 00:06 Go to previous messageGo to next message
rajatratewal
Messages: 507
Registered: March 2008
Location: INDIA
Senior Member
SELECT 
  CASE WHEN LENGTH(NAME)<20 THEN 'LESS THAN TWENTY'
       WHEN LENGTH(NAME)=20 THEN  'TWENTY'
       WHEN  LENGTH(NAME)>20 THEN 'GREATER THAN TWENTY' 
  END 
FROM 
  TestA

Re: Simulating If Then Else in Decode [message #313181 is a reply to message #313165] Fri, 11 April 2008 00:29 Go to previous message
flyboy
Messages: 1832
Registered: November 2006
Senior Member
It seems you already found, that DECODE function parameters shall be expressions, not conditions.

So you cannot use comparison. Just have a look at SIGN function.
Previous Topic: UTL_FILE Package Used::ORA-29280: invalid directory path Error Occurred
Next Topic: Selecting from a stored proc
Goto Forum:
  


Current Time: Sat Dec 03 22:29:54 CST 2016

Total time taken to generate the page: 0.07263 seconds