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

PostgreSQL: How to extract text from a particular letter?

I’m practicing exercises with SQL and I’ve got a problem I couldn’t resolve yet.

I have a table with a column named: **’email’ ** and I want to extract just the Domain of each mail. Then I was thinking to extract since ‘@’ to get that information.

But idk how to do it, was trying with SUBSTRING, but that didn’t work because that’s about position, and each mail has different size.

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 attach a screenshot about the table’s composition (does not contain real information). Thank u so much 🙂

enter image description here

I tried with SUBSTRING method but that didn’t work
Example email: example_email@outlook.com
Output expected: @outlook.com

>Solution :

We can use SPLIT_PART to fetch everything after the @ and then append the @:

SELECT CONCAT('@',SPLIT_PART(email, '@', 2)) AS mailDomain
FROM people_practice;

Here the documentation about this and other useful string functions.

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