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

String to Columns SQL – PostgreSQL

This problem is giving me a headache. I have a column in PostgreSQL that is a string:

Example:
bpApx14L103

The characters have always the same structure, the first two letters meaning one thing, etc.

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

So the output should be into columns:

Output

I can’t figure it out, thanks in advance.

>Solution :

You can use the substring() function:

select substring(the_col, 1, 2) as a,
       substring(the_col, 3, 5) as b,
       substring(the_col, 8, 1) as c,
       substring(the_col, 9, 1) as d,
       substring(the_col, 10, 1) as e,
       substring(the_col, 11, 1) as f
from the_table
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