Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - A simpler function than decode

I am working with a pl/sql procedure. I have an initialized variable myvar and I want to check its value : if it does not contain 'Z', I want it to contain 'P'.

I am currently doing it this way:

myvar := decode(myvar,'Z','Z','P');

I was just wondering if there was a simplier way to do this. I mean, decode is already simple, but I feel it's weird to specify the content of the variable while it is already in it !

If such a function would exist, it would look like this:

Function myfunction(a In Varchar2, b In Varchar2, c In Varchar2) 
Return Varchar2 
Is               
Begin
      if a <> b
      then
           return c;
      end if;
      return a;
End myfunction; 

Any help would be appreciated !

like image 914
Hal Avatar asked Aug 24 '26 20:08

Hal


1 Answers

There is no built-in function that does exactly what you want.

You could use CASE rather than DECODE:

CASE myvar WHEN 'Z' THEN 'Z' ELSE 'P' END

It doesn't make it any shorter though!

like image 131
Tony Andrews Avatar answered Aug 28 '26 13:08

Tony Andrews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!