Skip to content

Instantly share code, notes, and snippets.

@odlp
Created August 20, 2014 09:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odlp/e02621612aae82a778c2 to your computer and use it in GitHub Desktop.
Save odlp/e02621612aae82a778c2 to your computer and use it in GitHub Desktop.
Distinct substring SQL query with Postgresql
result = ActiveRecord::Base.connection.execute("SELECT DISTINCT SUBSTRING(postcode, '[A-Z0-9]{3,} [0-9]{1}') FROM postcodes")
result.values.first
# => ['AB1 1']
@tiagofrancafernandes
Copy link

tiagofrancafernandes commented Mar 3, 2021

Thank you!!!!!!!!!!!!!!!

I will use to get different domains:

SELECT DISTINCT SUBSTRING(email, '([@].*)') AS at_domain FROM users WHERE email NOT ILIKE '%sememail.com%' LIMIT 15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment