Skip to Main Content
  • Questions
  • How can I show special characters in ascii values

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Derek.

Asked: March 06, 2019 - 6:53 pm UTC

Last updated: March 28, 2019 - 11:35 am UTC

Version: 12.1

Viewed 10K+ times! This question is

You Asked

Hi Tom,

I would like to show many characters ascii values and I use this sql below

set serveroutput on
declare

c      varchar2 (100);
a      number (3);

begin
       for i in (     select 'abcde' from dual
       ) loop
              for j in 1..length('abcde')
              loop
                     c      := substr('abcde',j,1);
                     a      := ascii (c);
                     dbms_output.put_line (j || '|' || c || '| ' || a);
              end loop;
              dbms_output.put_line ('----------------------------------');
       end loop;
end;
/


The result is following below.

1|a| 97
2|b| 98
3|c| 99
4|d| 100
5|e| 101
----------------------------------


declare

c      varchar2 (100);
a      number (3);

begin
       for i in (     select 'abc??de' from dual
       ) loop
              for j in 1..length('abc??de')
              loop
                     c      := substr('abc??de',j,1);
                     a      := ascii (c);
                     dbms_output.put_line (j || '|' || c || '| ' || a);
              end loop;
              dbms_output.put_line ('----------------------------------');
       end loop;
end;
/


But When I would like show some special characters in the string with the same code, I got errors. My question is how can I show special character in decimal value, any ideas?

ORA-06502: PL/SQL: numeric or value error: number precision too large
ORA-06512: at line 12
06502. 00000 -  "PL/SQL: numeric or value error%s"
*Cause:    An arithmetic, numeric, string, conversion, or constraint error
           occurred. For example, this error occurs if an attempt is made to
           assign the value NULL to a variable declared NOT NULL, or if an
           attempt is made to assign an integer larger than 99 to a variable
           declared NUMBER(2).
*Action:   Change the data, how it is manipulated, or how it is declared so
           that values do not violate constraints.


Any ideas for this?

Kind Regards,

Derek

and Chris said...

You get the "number precision too large" error when you try and stuff more digits into your number variable than it supports:

declare
  n number(1);
begin
  n := 12;
end;
/

ORA-06502: PL/SQL: numeric or value error: number precision too large


So presumably the character you're trying to convert has an ASCII value > 999.

You could fix this by declaring the A size variable as a large number:
declare
  c   varchar2 (100);
  a   number (10);


Or using SQL instead ;)

with strings as (
  select 'abcde' str from dual
)
  select substr ( str, l, 1 ) chr,
         ascii ( 
           substr ( str, l, 1 ) 
         ) ascii#
  from   strings, 
         lateral (
           select level l from dual
           connect by level <= length ( str )
         );

CHR   ASCII#   
a           97 
b           98 
c           99 
d          100 
e          101  

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

gone too far?

Racer I., March 08, 2019 - 8:31 am UTC

Chris Saxon
March 08, 2019 - 2:28 pm UTC

:)

SQL and the Slice of Pizza

Duke Ganote, March 25, 2019 - 8:48 pm UTC

I enjoyed that SQL Snippet for two reasons:

First it shows an example of what the UNISTR documentation means when it blandly states "Supplementary characters are encoded as two code units, the first from the high-surrogates range (U+D800 to U+DBFF), and the second from the low-surrogates range (U+DC00 to U+DFFF)."

Second, else would I get a 'slice of pizza' from Oracle (I used LiveSQL), at least as an emoji?

SELECT
unistr('\BBBB' ), 'HANGUL SYLLABLE'
FROM DUAL UNION ALL SELECT
unistr('\D83C\DF55' ), 'SLICE OF PIZZA (Plane 1)'
FROM DUAL UNION ALL SELECT
unistr('\27A8' ), 'BLACK RIGHTWARDS ARROW'
FROM DUAL UNION ALL SELECT
unistr('\D86D\DF40' ), 'CJK IDEOGRAPH (Plane 2)'
FROM DUAL UNION ALL SELECT
unistr('\00C6' ), 'LATIN CAPITAL LETTER AE'
FROM DUAL;

UNISTR('\BBBB') 'HANGULSYLLABLE'
➨ BLACK RIGHTWARDS ARROW
𫝀 CJK IDEOGRAPH (Plane 2) 뮻 HANGUL SYLLABLE
Æ LATIN CAPITAL LETTER AE
🍕 SLICE OF PIZZA (Plane 1)


Chris Saxon
March 28, 2019 - 11:35 am UTC

SELECT unistr('\D83D\DE0E' ), 'COOL'
FROM DUAL

UNISTR('\D83D\DE0E') 'COOL'
😎 COOL

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library