You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Oracle查询中如何将RAW字段截取的十六进制值转为十进制?

Got it, let's work through converting that hex value from your RAW field to the decimal number you need. The TO_NUMBER function likely failed because of an incorrect format mask, or maybe you didn't properly convert the RAW data to a hex string first. Here are two solid approaches to make this work:

Approach 1: Convert RAW to Hex String, Then to Decimal

First, convert your RAW field to a human-readable hex string, grab the 8 characters you need, then use TO_NUMBER with the right format mask:

SELECT TO_NUMBER(SUBSTR(RAWTOHEX(your_raw_column), 1, 8), 'XXXXXXXX') AS decimal_value
FROM your_table;
  • RAWTOHEX: Converts the binary RAW data into its hexadecimal string equivalent.
  • SUBSTR(..., 1, 8): Extracts the first 8 characters of that hex string (matching your 003A2FD2 example).
  • TO_NUMBER(..., 'XXXXXXXX'): The XXXXXXXX format mask tells Oracle to interpret the input as an 8-digit hexadecimal number, which converts directly to your target decimal value 3813330.

If you run into issues with leading zeros, you can add the FM modifier to the mask to suppress any extra whitespace: 'FMXXXXXXXX'.

Approach 2: Directly Convert RAW Bytes to Decimal (More Efficient)

Since 8 hex characters equal 4 binary bytes, you can skip the string conversion entirely by working directly with the RAW data:

SELECT UTL_RAW.CAST_TO_NUMBER(UTL_RAW.SUBSTR(your_raw_column, 1, 4)) AS decimal_value
FROM your_table;
  • UTL_RAW.SUBSTR(..., 1, 4): Grabs the first 4 bytes from your RAW field (this corresponds to the 8 hex characters 003A2FD2).
  • UTL_RAW.CAST_TO_NUMBER: Converts those 4 bytes directly into a decimal NUMBER. This method is faster for large datasets because it avoids string manipulation overhead.

You can test these with static values to confirm they work:

-- Test Approach 1
SELECT TO_NUMBER('003A2FD2', 'XXXXXXXX') FROM DUAL; -- Returns 3813330

-- Test Approach 2
SELECT UTL_RAW.CAST_TO_NUMBER(HEXTORAW('003A2FD2')) FROM DUAL; -- Also returns 3813330

内容的提问来源于stack exchange,提问作者RobbeVL

火山引擎 最新活动