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

How to extract only numerics from string in PostgreSQL 8.0.2 Amazon Redshift

I only want the numeric part of this string column:

identity
student:1234
student:56
student:789
id:driver_license-111-AZ
id:learner_permit-222-NY

So that the output should be:

wanted
1234
56
789
111
222

I am using PostgreSQL 8.0.2 (Amazon Redshift) and I think SELECT REGEXP_SUBSTR(identity,'[0-9]') FROM table should work. But it does not. I tried multiple variations of optional arguments in the REGEXP_SUBSTR but I can’t get it to work. Would someone please help me? With this function or otherwise.

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

>Solution :

Well REGEXP_SUBSTR() should work assuming you use the correct regex pattern:

SELECT REGEXP_SUBSTR(identity, '[0-9]+')  -- [0-9]+ means one or MORE digits
FROM yourTable;

You might also be able to phrase this using a regex replacement:

SELECT REGEXP_REPLACE(identity, '[^0-9]+', '')  -- strip non digit characters
FROM yourTable;
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