Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - Convert string to ASCII integer

How do I get the ASCII value of a string as an int in PostgreSQL?

For example: the string S06.6X9A

Currently, I am using an ASCII function, but it returns only the first character of a given string.

like image 269
Vanessa Ng Avatar asked Jun 24 '26 05:06

Vanessa Ng


2 Answers

Use string_to_array('S06.6X9A', null) to split the string into a text[] of individual characters. Then unnest to turn that text[] into a table. Then use that in a from clause and run ascii() over each row.

select ascii(char)
from (
  select unnest( string_to_array('S06.6X9A', null) )
) as chars(char);

 ascii 
-------
    83
    48
    54
    46
    54
    88
    57
    65
like image 114
Schwern Avatar answered Jun 28 '26 16:06

Schwern


Simpler than Schwern's answer is:

SELECT ascii( unnest( string_to_array('S06.6X9A', NULL) ) )
like image 32
marbanb Avatar answered Jun 28 '26 18:06

marbanb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!