简体   繁体   中英

Validate telephone number in SQL Server 2000

Does anyone have a nifty way of validating telephone numbers using sql (SQL Server 2000).

I need to select all users fro ma Db that have valid phone number

Thanks Sp

Valid number 
08450000000
01332000000
07444000000
+441332000000

Standard UK numbers

Allow the user to enter numbers in a variety of formats with various dial prefixes and punctuation.

Strip the dial prefix digits, remove spacing and punctuation. Validate the number has the right number of digits and is in a valid range and then store the number in your database in E.164 format with full country code.

This allows you to make bulk area code changes as they happen as well as make it easy to pull all numbers for a country unambiguously.

Store the number without spaces or punctuation.

Use country-specific formatting rules to format the number correctly for display.

UK numbers are quite complex. There's full data here: http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers

It describes the selection, validation and formatting processes in detail.

This website has extremely thorough validation for UK telephone numbers, with code examples in JavaScript, VBScript, & PHP. You will need to translate this to use in a SQL Server stored procedure, but the principle should be straightforward to follow.

UK Telephone Number Validation - JavaScript, VBScript, & PHP

If you have a regular expression to match the number, you can install a regex extended stored procedure on your SQL Server. I installed this extended stored procedure at work and we use it quite a bit. There are several procedures (each with corresponding function wrappers):

  1. check for matches (yes, no)
  2. check for matches (count)
  3. search and replace
  4. format
  5. split

To find matches you would use it as such:

 select number 
 from numberTable
 where dbo.fn_pcre_match(number, 'someRegex') = 1

Where 'someRegex' is the regular expression matching the format you are looking for. This site has some matches on it, but I'm not sure how well they work since I'm not familiar with UK numbers whatsoever.

I don't knwo the exact rules that define a valid UK phone number and as you have not provided any rules to validate a Phone no, I just picked rules listed in the url provided by Colin Pickard in his answer.

The following rules are checked for validating UK phone number - 1. Telephone number provided is not null 2. Telephone number does not contain the required 10 or 11 digits. 3. A valid full UK telephone numbers must start with a 0

If there are any rules that I missed out, you can add the check for those conditions too in this function.

ALTER FUNCTION [dbo].[ValidatePhoneNo] 
(   
    @PhoneNo varchar(20) 
)
RETURNS varchar(10)
AS
BEGIN
    DECLARE @Result varchar(10)
    SET @RESULT = 'invalid'

    IF len(@PhoneNo) > 9 AND len(@PhoneNo) < 12 AND @PhoneNo IS NOT NULL AND (substring(@PhoneNo,1,1) = 0)
    BEGIN
        SET @Result = 'valid'
    END

    RETURN @RESULT
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM