How can i do the following in 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?
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). The to_bin function is also shown here, but not needed to use bitand 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). eg, power(2,0), power(2,1), power(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/
The syntax for the cast function is:
cast ( { expr | ( subquery ) | MULTISET ( subquery ) } AS type_name )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.