views:

62

answers:

4

I need to count number of email addresses in a db that have 3 or less characters before the @ sign, for example [email protected].

The parsename function isn't present in Oracle and I'm not sure how to write a regexp for this. Any help would be greatly appreciated!

+2  A: 

The regular expression you want is:

^[^@]{0,3}@

In English, that's:

  • Start of string
  • between 0 and three things that aren't an at sign.
  • an at sign.
Daniel Martin
+1 for the english translation ;-)
DCookie
Thank you the English translation +1
Sologoub
+2  A: 

You could define the WHERE clause & use COUNT, or skip to use REGEXP_COUNT instead:

SELECT REGEXP_COUNT(t.email, '^[^@]{0,3}@', 1, 'i') RESULT
  FROM TABLE t;

Using COUNT:

SELECT COUNT(*)
  FROM TABLE t
 WHERE REGEXP_LIKE(t.email, '^[^@]{0,3}@')

Reference:

OMG Ponies
Be aware that REGEXP is Oracle 10g+ -- there's no regular expression support in Oracle versions earlier than that.
OMG Ponies
+1 for The Way To Do It In Oracle.
DCookie
+6  A: 

Regex is overkill for this. All you need is

instr(t.email, '@') < 5 AND instr(t.email, '@') > 0

Edited with correction from the comments.

Hugh Brackett
+1 for simplicity.
DCookie
Although, it should be < 5...
DCookie
This is probably much faster than a regexp.
TTT
This is exactly what I ended up figuring out on my own, the end result looked like this: SELECT count(*)from somethingwhere instr(email, '@', 1) <= 3;
Sologoub
I still maintain you're off by one. For a 3 character address before the @ sign, the result of your expression is 4.
DCookie
DCookie is right. The `instr` returns 4 for [email protected], so the test should be < 5.
Hugh Brackett
another correction, thanks to Basha's keen eye
Jeffrey Kemp
+1  A: 

There is a little problem with following instr(t.email, '@') < 5

this query will work provided t.email has a '@' ! other wise it will return those entries also where t.email is not having '@'

Basha
you are right, fortunately that's not an issue here because the email format is validated on entry. Just needed to see if we can implement stronger validation.
Sologoub