Convert Boolean to Varchar2

I have an example code where I am trying to print a boolean value. This led to an error.

wrong number or types of arguments when calling 'PUT_LINE'

wrong number or types of arguments when calling TO_CHAR

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

The error message makes it clear that the Boolean cannot be converted to a character in both directions (implicit, explicit).

Why is this impossible?

Do they have any specific reasons? or Oracle just forgot about this type of conversion (its unlikely).

How is their other way to convert? Or I need to go for the operator IFor CASEto guess what it has status.

+9
source share
3 answers

, varchar 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));
+13

. , case.

Oracle SQL Boolean. PL/SQL. , .

?

, ANSI.

+2

BOOL_TO_INT() SYS.DIUTIL:

DECLARE
status BOOLEAN:= false;
BEGIN
  DBMS_OUTPUT.PUT_LINE(sys.diutil.bool_to_int(status));
END;

1 true 0 false ( null null).

0

Source: https://habr.com/ru/post/1625317/


All Articles