将布尔值转换为 Varchar2 [英] Converting Boolean to Varchar2

查看:70
本文介绍了将布尔值转换为 Varchar2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例代码,我试图在其中打印布尔值.结果出错了.

I have a sample code where i'm trying to print Boolean value. It resulted error.

调用PUT_LINE"的参数数量或类型错误

wrong number or types of arguments in call to 'PUT_LINE'

调用TO_CHAR"时的参数数量或类型错误

wrong number or types of arguments in call to 'TO_CHAR'

DECLARE
    status BOOLEAN:= false;
    BEGIN
      DBMS_OUTPUT.PUT_LINE(status);
      DBMS_OUTPUT.PUT_LINE(to_char(status));
    END;

根据错误消息,很明显不能以两种方式(隐式、显式)将布尔值转换为字符.

By the error message it's clear that Boolean can't be converted to character in both ways (implicit, explicit).

为什么不可能?

他们有什么具体原因吗?或者 Oracle 只是忘记了这种类型的转换(极不可能).

Is their any specific reasons? or Oracle just forgot about this type of conversion(Its highly unlikely).

那么他们还有其他转换方式吗?或者我是否必须使用 IFCASE 语句来猜测 status 有什么.

So is their any other way to convert? Or do i have to go for IF or CASE statement to guess what status has.

推荐答案

看来你不能连接 varcharboolean.

It seems you cannot concat varchar and boolean.

定义这个函数:

CREATE OR REPLACE FUNCTION BOOLEAN_TO_CHAR(STATUS IN BOOLEAN)
RETURN VARCHAR2 IS
BEGIN
  RETURN
   CASE STATUS
     WHEN TRUE THEN 'TRUE'
     WHEN FALSE THEN 'FALSE'
     ELSE 'NULL'
   END;
END;

并像这样使用它:

DBMS_OUTPUT.PUT_LINE('status'|| BOOLEAN_TO_CHAR(status));

这篇关于将布尔值转换为 Varchar2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆