oracle 如何转换select中函数返回的布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23289991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:20:33  来源:igfitidea点击:

How to convert boolean value which returned by function in select

sqloracle

提问by Horman Lewis

I had a next query, which doesn't work:

我有一个下一个查询,它不起作用:

SELECT case BasePak.SomeFunction(333,4,5555) when true then 1 else 0 end 
               from dual

BasePak.SomeFunction(333,4,5555) returns boolean value. How to convert result(true, false) to number or string?

BasePak.SomeFunction(333,4,5555) 返回布尔值。如何将结果(真,假)转换为数字或字符串?

I tried cast function but not successful.

我尝试了强制转换功能但没有成功。

Is there any way to convert boolean to int or string?

有没有办法将 boolean 转换为 int 或 string?

回答by Michal

SQL does not have boolean datatype, but PL/SQL does. In PL/SQL you have more options. You can:

SQL 没有布尔数据类型,但 PL/SQL 有。在 PL/SQL 中,您有更多选择。你可以:

  • write functions so they don't return boolean, but the type you need.
  • if you can't edit the functions, you can write a wrapper function, that does the conversion
  • or you can directly use code like this:

    string_var := CASE
    WHEN  boolean_var
    THEN  'True'
    WHEN  NOT boolean_var  -- or ELSE, depending on what you want
    THEN  'False'
    END;
    
  • 编写函数,使它们不返回布尔值,而是返回您需要的类型。
  • 如果你不能编辑这些函数,你可以写一个包装函数来进行转换
  • 或者你可以直接使用这样的代码:

    string_var := CASE
    WHEN  boolean_var
    THEN  'True'
    WHEN  NOT boolean_var  -- or ELSE, depending on what you want
    THEN  'False'
    END;
    

In your case, I would either change the original function or write a wrapper function and use it directly from SQL code.

在您的情况下,我要么更改原始函数,要么编写一个包装函数并直接从 SQL 代码中使用它。

回答by DLR

As far as i know SQL doesn't contain a Boolean datatype. A Bit is generally used to represent true (1) or false (0). Are you sure your function is not returning a NULL value as this needs to be handled differently

据我所知,SQL 不包含布尔数据类型。A Bit 通常用于表示真 (1) 或假 (0)。您确定您的函数没有返回 NULL 值,因为这需要以不同的方式处理

Try this instead:

试试这个:

SELECT case when BasePak.SomeFunction(333,4,5555) = 1 then 'true' else 'False' end 
from dual