# Find out the ASCII-Code of a Character in Teradata SQL

Roland Wenzlofsky

May 16, 2014

If you ever needed to find out the ASCII-Code of a character, you will know that no function in Teradata SQL exists to solve this problem.

Here is a query workaround that solves this issue. What the below SQL statement does is the following:

The CHAR2HEXINT Teradata SQL function returns a hexadecimal value; we need to convert it into the decimal number system. Conversion is done by multiplying the first part of the returned hexadecimal value by 16 and adding the second part of the hexadecimal value.

SELECT
CASE SUBSTRING(CHAR2HEXINT(‘B’) FROM 3 FOR 1)
WHEN ‘0’ THEN 0
WHEN ‘1’ THEN 1
WHEN ‘2’ THEN 2
WHEN ‘3’ THEN 3
WHEN ‘4’ THEN 4
WHEN ‘5’ THEN 5
WHEN ‘6’ THEN 6
WHEN ‘7’ THEN 7
WHEN ‘8’ THEN 8
WHEN ‘9’ THEN 9
WHEN ‘A’ THEN 10
WHEN ‘B’ THEN 11
WHEN ‘C’ THEN 12
WHEN ‘D’ THEN 13
WHEN ‘E’ THEN 14
WHEN ‘F’ THEN 15
END * 16 +
CASE SUBSTRING(CHAR2HEXINT(‘B’) FROM 4 FOR 1)
WHEN ‘0’ THEN 0
WHEN ‘1’ THEN 1
WHEN ‘2’ THEN 2
WHEN ‘3’ THEN 3
WHEN ‘4’ THEN 4
WHEN ‘5’ THEN 5
WHEN ‘6’ THEN 6
WHEN ‘7’ THEN 7
WHEN ‘8’ THEN 8
WHEN ‘9’ THEN 9
WHEN ‘A’ THEN 10
WHEN ‘B’ THEN 11
WHEN ‘C’ THEN 12
WHEN ‘D’ THEN 13
WHEN ‘E’ THEN 14
WHEN ‘F’ THEN 15
END

• Beat BrÃ¼ngger says:

Hi, if you are on Teradata >= 14.00 you can try:
SELECT ASCII(‘A’);
->> Returns the decimal representation of the first character in string_expr as a NUMBER value.