I'm looking for the Oracle equivalent of something that is easy to do in MS SQL, namely:
DECLARE @INDEC AS INT
SET @INDEC = 37000
RIGHT(CONVERT(VARBINARY(8),@INDEC),4)
This replicates a legacy software data storage format in a MS SQL database. The same software can run against oracle, and I now need to replicate this same statement there.
Specifically, when I look at the result of this character by I see a unique set of ASCII values:
SELECT
ASCII(SUBSTRING(RIGHT(CONVERT(VARBINARY(8),37000),4),1,1)) AS POS1,
ASCII(SUBSTRING(RIGHT(CONVERT(VARBINARY(8),37000),4),2,1)) AS POS2,
ASCII(SUBSTRING(RIGHT(CONVERT(VARBINARY(8),37000),4),3,1)) AS POS3,
ASCII(SUBSTRING(RIGHT(CONVERT(VARBINARY(8),37000),4),4,1)) AS POS4
FROM MYTABLE
| POS1 | POS2 | POS3 | POS4 |
|---|---|---|---|
| 0 | 0 | 144 | 136 |
When I try this on Oracle, using CAST as RAW, I either get my input value returned to me, or I get error messages.
I have specifically tried a variety of cast options including specifying a length for the data and converting the input to hex prior to casting as raw.
SELECT
ASCII(SUBSTR(CAST('37000' AS RAW(8)),3,1)) AS POS1,
ASCII(SUBSTR(CAST('37000' AS RAW(8)),4,1)) AS POS2,
ASCII(SUBSTR(CAST('37000' AS RAW(8)),5,1)) AS POS3,
ASCII(SUBSTR(CAST('37000' AS RAW(8)),6,1)) AS POS4
FROM dual;
This simply returns "7000" or:
| POS1 | POS2 | POS3 | POS4 |
|---|---|---|---|
| 55 | 48 | 48 | 48 |
If I try converting to hex first, I get an error message (invalid hex number):
select HEXTORAW(to_char(37000,'XXXXXXXX')) from dual
If I separate the conversion I know that hex for 37000 is 9088. But the following returns the input string (I have taken it apart.)
select HEXTORAW('9088') from dual
Ultimately the output of this conversion operation will be concatenated with other string data and stored in a column that is defined as VARCHAR.
I feel like I'm missing something very fundamental here.
Any thoughts or input would be greatly appreciated.