0

In Postgresql, I need to take an ASCII string like CNR39XL and turn it into a number like 21317392311 (ASCII values - 65, but digits left as is). This is to take some logic in our software and push it down into the databse level. I’m partway there with this:

ourdb=> SELECT CASE
ourdb->          WHEN ASCII(c) < 65 THEN c::integer
ourdb->          ELSE                    ASCII(c)-65
ourdb->        END
ourdb->   FROM regexp_split_to_table('CNR39XL','') s(c);
 case
------
    2
   13
   17
    3
    9
   23
   11
(7 rows)

However, I can't figure out how to take that final set of numbers and convert it into the string. What am I looking for?

1 Answer 1

1

Use string_agg

SELECT CAST(
          string_agg(
             CAST (CASE ... END AS text),
             ''
          )
       AS numeric)
FROM ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.