Search code examples
sqlstringpostgresqlsubstring

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.

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.