PL/SQL Convert String To Boolean

时光怂恿深爱的人放手 提交于 2019-12-11 02:38:33

问题


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.


回答1:


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', ...



来源:https://stackoverflow.com/questions/11097777/pl-sql-convert-string-to-boolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!