Store phone numbers without formatting...the data is the numbers themselves the formatting is presentation.
The left side of the two where clauses are different fields/expressions. Since you do not specify what your table and indexes look like your "problem" is impossible to solve but likely has nothing to do with RegEx. Keep in mind, however, that an index can only be used if the pattern is fully anchored. With alternation in the RegEx you want the"^" outside of the part the part that uses "|" otherwise only the first _expression_ ends up being anchored. E.g, '^(a|b)' !='^a|b'. The first one matches a string that stars with a or b whereas the second matches a string that starts with a or contains b anywhere in the string. The second one cannot use the index since it is not guaranteed to be anchored at the start of a string. Particularly with RegEx you want to tell people what you are trying to do and not just give the expressions themselves. Not testing here but... and ignore whitespace '^( [ \[\( ]? \s* \d{3} \s* [ -\s\]\) ] \d{3} [ -\s ] \d{4} )$' The above should match both of your samples and use the index on the regular phone column. If you want to store encrypted and search the unencrypted you have to create a functional index. See documentation for syntax and requirements. In this case you can replace the \d{n} with your desired search strings. It would be a lot simpler if you strip out the non-numbers, via functional index if needed, and perform an equality string search. The question becomes, using the example data above, what happens if two people have the same phone number with only the format being different. The answer is the difference between a unique index and a non-unique one... example: create index name on table (clean_and_decrypt_phone(enc_phone)) Where clean_and_decrypt_phone(enc_phone) = clean_phone( search_string ) This can be done without changing columns, only indexes and queries. David J. |