简体   繁体   English

Oracle型铸件

[英]Oracle Type Casting

How can i do the following in Oracle?如何在 Oracle 中执行以下操作?

SELECT  (cast field as bit) From Table

Is there a way to conver this into an Oracle statement using something similar to cast or convert?有没有办法使用类似于 cast 或 convert 的东西将其转换为 Oracle 语句?

If what you want is to see is how to do binary,hex,oct conversions, see here .如果您想查看的是如何进行二进制、十六进制、八进制转换,请参见此处 (Tom Kyte rocks) (汤姆凯特摇滚)

For example,例如,

SQL> select to_bin( 123 ) bin, to_hex( 123 ) hex, to_oct( 123 ) oct from dual
2  /

BIN             HEX             OCT
--------------- --------------- ---------------
1111011         7B              173

EDIT: If you just wanted to see if a bit was on/off, you could use bitand function (which comes out of the box with Oracle).编辑:如果您只想查看某个位是否打开/关闭,您可以使用 bitand function(Oracle 开箱即用)。 The to_bin function is also shown here, but not needed to use bitand function.此处还显示了to_bin function,但不需要使用bit和function。

select to_bin(1234) bin,
  2             bitand(1234,1)+0 bit1,
  3             bitand(1234,2)+0 bit2,
  4             bitand(1234,4)+0 bit3
  5    from dual
  6  /

BIN                BIT1       BIT2       BIT3
------------ ---------- ---------- ----------
10011010010           0          2          0

You can also use power function to get 2nd param value for bitand (2^n).您还可以使用电源 function 来获取 bitand (2^n) 的第二个参数值。 eg, power(2,0), power(2,1), power(2,2)例如,幂(2,0)、幂(2,1)、幂(2,2)

CREATE OR REPLACE FUNCTION to_base (p_dec IN NUMBER, p_base IN NUMBER)
   RETURN VARCHAR2
IS
   l_str   VARCHAR2 (255) DEFAULT NULL;
   l_num   NUMBER         DEFAULT p_dec;
   l_hex   VARCHAR2 (16)  DEFAULT '0123456789abcdef';
BEGIN
   IF (p_dec IS NULL OR p_base IS NULL)
   THEN
      RETURN NULL;
   END IF;

   IF (TRUNC (p_dec) <> p_dec OR p_dec < 0)
   THEN
      RAISE PROGRAM_ERROR;
   END IF;

   LOOP
      l_str := SUBSTR (l_hex, MOD (l_num, p_base) + 1, 1) || l_str;
      l_num := TRUNC (l_num / p_base);
      EXIT WHEN (l_num = 0);
   END LOOP;

   RETURN l_str;
END to_base;

source: https://www.club-oracle.com/threads/how-to-convert-decimal-to-hexadecimal.16083/来源: https://www.club-oracle.com/threads/how-to-convert-decimal-to-hexadecimal.16083/

Oracle/PLSQL: Cast Function Oracle/PLSQL:铸造 Function

The syntax for the cast function is:转换 function 的语法是:

cast ( { expr | ( subquery ) | MULTISET ( subquery ) } AS type_name )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM