mySQL regex in the where clause

前端 未结 3 1101
旧巷少年郎
旧巷少年郎 2020-12-01 23:21
SELECT telephone_number
FROM table
WHERE telephone_number REGEXP \'^1[() -]*999[() -]*999[() -]*9999$\';

how do i make so its valid for any number

相关标签:
3条回答
  • 2020-12-01 23:59
    SELECT telephone_number
      FROM table
     WHERE telephone_number REGEXP '[1]?[(]?[[:DIGIT:]]{3}[)]?[-]?[[:DIGIT:]]{3}[-]?[[:DIGIT:]]{4}'
    
    0 讨论(0)
  • 2020-12-02 00:00

    Use:

    SELECT telephone_number
      FROM table
     WHERE telephone_number REGEXP '^1[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{4}$';
    

    Reference:

    • Pattern Matching
    0 讨论(0)
  • 2020-12-02 00:13

    It isn't very wise to store phone numbers in a database with spaces, dashes, parentheses, etc. The most efficient way is to truncate all that garbage to a simple 10 digit number. That way you can actually store the number in an INTEGER based column instead of a VARCHAR.

    0 讨论(0)
提交回复
热议问题