Extracting Numeric Tokens from Strings with CHAR2HEXINT Function in Teradata SQL

Roland Wenzlofsky’s recent blog introduced the CHAR2HEXINT function, which extracts coding table numbers for tokens.

These functions proved invaluable in solving a practical issue we encountered. If this seems irrelevant to your daily concerns, consider their practical application.

Given a string with a combination of characters and numbers, including special characters, extract the numerical portion and return it as a number if it exists. Otherwise, return a default value.

We resolved the issue of the given solution’s failure with special characters by extracting the desired substring only when every token is numeric.

Numeric tokens have an adjacent set of hexadecimal values. For anything not purely numeric as a whole, we default to -9, as agreed.

Beware that using shortcuts like converting the substring “12 ” (one-two-blank) to an integer can create a deceptive sense of security.

Here is the SQL code that performs the task:

SELECT
CASE WHEN CHAR2HEXINT( SUBSTR(‚'1201'', 2, 1) ) BETWEEN ‚'030''AND ‚'039''AND
CHAR2HEXINT( SUBSTR(‚'1201'' 3, 1) ) BETWEEN ‚'030''AND ‚'039''AND
CHAR2HEXINT( SUBSTR(‚'1201'', 4, 1) ) BETWEEN ‚'030''AND ‚'039''
THEN SUBSTR(‚'1201'', 2, 3)
ELSE -9
END

Other approaches I used in previous data warehousing settings, such as implementing regular expressions through Oracle SQL, are not as applicable in Teradata SQL.

Incorporate this feature into your repertoire of tools for handling tricky coding operations.

Related Services

⚡ Need Help Optimizing Your Data Platform?

We cut data platform costs by 30–60% without hardware changes. 25+ years of hands-on tuning experience.

Explore Our Services →

📋 Considering a Move From Teradata?

Get a personalized migration roadmap in 2 minutes. We have migrated billions of rows from Teradata to Snowflake, Databricks, and more.

Free Migration Assessment →

📊 Data Platform Migration Survey

Help us map where the industry is heading. Results are public — see what others chose.

1. What is your current data platform?

2. Where are you migrating to (or evaluating)?

Migrating FROM
Migrating TO

Thanks for voting! Share this with your network.

Follow me on LinkedIn for daily insights on data warehousing and platform migrations.

Stay Ahead in Data Warehousing

Get expert insights on Teradata, Snowflake, BigQuery, Databricks, Microsoft Fabric, and modern data architecture — delivered to your inbox.

Leave a Comment

DWHPro

Expert network for enterprise data platforms. Senior consultants, project teams built for your challenge — across Teradata, Snowflake, Databricks, and more.

📍Vienna, Austria & Jacksonville, Florida

Quick Links
Services Team Teradata Book Blog Contact Us
Connect
LinkedIn → [email protected]
Newsletter

Join 4,000+ data professionals.
Weekly insights on Teradata, Snowflake & data architecture.