user1375026
user1375026

Reputation: 413

PL/SQL Convert String To Boolean

I have an EXT JS page which sends a form with some values. The values are all sent in string and some sent as boolean. It is calling a PL/SQL procedure where the params are all varchar. For some reason when the form is submitted, even though some values are sent as boolean, they can not be received by the procedure as boolean. All the values received from the procedure are varchar; and must be otherwise it crashes.

So I am sending a boolean from the form to the procedure. When it gets to the procedure it is now a varchar. How can I convert this back to a boolean?

Any help at all appreciated, I feel like I'm doing something wrong here. I don't understand why it is receiving this as a varchar.

Upvotes: 3

Views: 22099

Answers (1)

Tony Andrews
Tony Andrews

Reputation: 132620

You are right that there is no Oracle built-in string-to-boolean function, but you could easily create one yourself:

create or replace function to_boolean
  ( p_string varchar2
  ) return boolean
is
begin
  return
    case upper(p_string) 
      when 'TRUE' then true
      when 'FALSE' then false
      else null
      end;
end;

(The "else null" is redundant but I put it there to remind you that if upper(p_string) is anything other than 'TRUE' or 'FALSE' then the function will return null).

You could of course enhance the function to treat other string values as true or false also e.g. 'T', 'YES', ...

Upvotes: 5

Related Questions