Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

what will be the best way to calculate total ASCII value of the string of characters in SQL

I was searching for this question on StackOverflow but didn’t get any questions that are answering it. So I posted it here.

I want to calculate the ASCII value of the string which is only consisting of characters in a simple way as possible in SQL.

The string is "DESIGNATION OF EMPLOYEE". I want to calculate the total ASCII value of the string.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried the longest approach till now, which seems like this –

SELECT SUM(ASCII('D')+ASCII('E')+ASCII('S')+ASCII('I')+.....) AS "TOTAL ASCII" FROM  DUAL;

NOTE: I want to calculate the total ASCII value of the string which includes combined ASCII values of all the characters.

>Solution :

You can use CONNECT BY to unfold the characters, then SUM the ASCII values.

SELECT str
, SUM(ASCII(REGEXP_SUBSTR(str,'.',1,level))) AS "TOTAL ASCII"
FROM (SELECT 'DESIGNATION OF EMPLOYEE' AS str FROM DUAL)
CONNECT BY REGEXP_SUBSTR(str,'.',1,level) IS NOT NULL
GROUP BY str;
STR TOTAL ASCII
DESIGNATION OF EMPLOYEE 1642

db<>fiddle here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading