SQL рд╕рд░реНрд╡рд░ рдореЗрдВ рд╕рдлрд╛рдИ рдЯреЗрд▓реАрдлреЛрди рдирдВрдмрд░
рд╕рдВрджрд░реНрдн
рдХреНрд▓рд╛рдЗрдВрдЯ рдЗрдиреНрдлреНрд░рд╛рд╕реНрдЯреНрд░рдХреНрдЪрд░ рдкрд░ рд╣рдорд╛рд░реЗ рд╕рд╛рдордиреЗ рд╕рдмрд╕реЗ рдЖрдо рдЪреБрдиреМрддрд┐рдпреЛрдВ рдореЗрдВ рд╕реЗ рдПрдХ рдЦрд░рд╛рдм рд╕реНрд╡рд░реВрдкрд┐рдд рдЯреЗрд▓реАрдлреЛрди рдирдВрдмрд░ рд╣реИред рдЬрдм рд╡реЗ рдПрдХреНрд╕реЗрд▓ рдпрд╛ рдЖрдЙрдЯрдкреБрдЯ рдХреЛ рдмрджрд▓рдиреЗ рд╡рд╛рд▓реЗ рд╡рд┐рднрд┐рдиреНрди рдЕрдиреНрдп рдкрд░рд┐рджреГрд╢реНрдпреЛрдВ рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рдЪрд▓рд╛рдП рдЬрд╛рддреЗ рд╣реИрдВ рддреЛ рд╡реЗ рдЦрд░рд╛рдм рд╕реНрдерд┐рддрд┐ рдореЗрдВ рдЖ рдЬрд╛рддреЗ рд╣реИрдВред
рдпрд╣ рдлрд╝рдВрдХреНрд╢рди рдпреВрдХреЗ рдХреЗ рдирдВрдмрд░реЛрдВ рдкрд░ рд▓рдХреНрд╖рд┐рдд рд╣реИ, рдЬрд┐рд╕рдореЗрдВ рдЕрдВрддрд░рд░рд╛рд╖реНрдЯреНрд░реАрдп рдирдВрдмрд░реЛрдВ рдХреА рд▓рдВрдмрд╛рдИ рдХреЗ рд▓рд┐рдП рдХреБрдЫ рдЧреБрдВрдЬрд╛рдЗрд╢ рд╣реИред рдкрд┐рдЫрд▓реЗ рдХреЗрд╕ рд╕реНрдЯреЗрдЯрдореЗрдВрдЯ рдореЗрдВ рдкрд╣рд▓реЗ 6 рдХреЗрд╕ рдЖрдЗрдЯрдо рдФрд░ рджреВрд╕рд░реЗ рдЖрдЦрд┐рд░реА рдХреЗрд╕ рд╕реНрдЯреЗрдЯрдореЗрдВрдЯ рдореЗрдВ рддреАрди рдХреЗрд╕ рдЖрдЗрдЯрдо рдХреЛ рдЕрдиреБрдХреВрд▓рд┐рдд рдХрд░рдХреЗ рдЗрд╕реЗ рдХрд┐рд╕реА рднреА рдЕрдиреНрдп рдХреНрд╖реЗрддреНрд░ рдХреЗ рд▓рд┐рдП рдЖрд╕рд╛рдиреА рд╕реЗ рдЕрдиреБрдХреВрд▓рд┐рдд рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред
рдпрд╣ рдХрдИ рд╡рд░реНрд╖реЛрдВ рд╕реЗ рдЙрдкрдпреЛрдЧ рдореЗрдВ рд╣реИ, рдФрд░ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдЪрд░рдгреЛрдВ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ рдЕрдирдЧрд┐рдирдд рдЯреЗрд▓реАрдлреЛрди рдирдВрдмрд░реЛрдВ рдХреЛ рдмрдЪрд╛рдпрд╛ рд╣реИ;
- рдПрдХ рдЕрд╕реНрдерд╛рдпреА рдЪрд░ рдореЗрдВ рд╕рдВрдЦреНрдпрд╛ рдХрд╛ рдмреИрдХрдЕрдк рд▓реЗрдВ
- рдЖрд░рдВрднрд┐рдХ рдЪрд┐рд╣реНрди рдХреЛ рджреЛрд╣рд░реЗ рд╢реВрдиреНрдп рд╕реЗ рдмрджрд▓реЗрдВ
- рд╕реНрдЯреНрд░рд┐рдВрдЧ рд╕реЗ рдХрд┐рд╕реА рднреА рд╢реЗрд╖ рдЯреЗрдХреНрд╕реНрдЯ рд╡рд░реНрдг рдХреЛ рд╣рдЯрд╛ рджреЗрдВ
- рдпреВрдХреЗ рдбрд╛рдпрд▓-рдЗрди рдирдВрдмрд░реЛрдВ рдХреЛ 0 . рд╕реЗ рдмрджрд▓реЗрдВ
- рдкреНрд░рддреНрдпреЗрдХ рд╕рдВрдЦреНрдпрд╛ рдкреНрд░рдХрд╛рд░ рдХреА рд▓рдВрдмрд╛рдИ рдХреА рдЬрд╛рдБрдЪ рдХрд░реЗрдВред
- рдирдпрд╛ рдирдВрдмрд░ рд▓реМрдЯрд╛рдПрдВ рдпрд╛ рдкреБрд░рд╛рдиреЗ рдирдВрдмрд░ рдХреЛ рд╡рд╛рдкрд╕ рдХрд░ рджреЗрдВ рдпрджрд┐ рдпрд╣ рд╕рддреНрдпрд╛рдкрд┐рдд рдирд╣реАрдВ рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрд╛ред
SQL
CREATE FUNCTION [dbo].[CleanseTelephone](@TelNo VARCHAR(30))
RETURNS VARCHAR(20)
AS BEGIN
DECLARE @OldNumber VARCHAR(20)=@TelNo
SET @TelNo =(CASE
WHEN LEFT(@TelNo,1)='+' THEN STUFF(@TelNo,1,1,'00')
ELSE @TelNo END)
DECLARE @Letter INT
SET @Letter =PATINDEX('%[^0-9]%',@TelNo)
BEGIN
WHILE @Letter>0
BEGIN
SET @TelNo =STUFF(@TelNo,@Letter,1,'')
SET @Letter =PATINDEX('%[^0-9]%',@TelNo)
END
END
SET @TelNo =(CASEWHEN LEFT(@TelNo,5)='00440' THEN STUFF(@TelNo,1,5,'0')WHEN LEFT(@TelNo,4)='0440' THEN STUFF(@TelNo,1,4,'0')
WHEN LEFT(@TelNo,3)='440' THEN STUFF(@TelNo,1,3,'0')
WHEN LEFT(@TelNo,2)='44' THEN STUFF(@TelNo,1,2,'0')
ELSE @TelNo END)
SET @TelNo =(CASE
WHEN LEFT(@TelNo,2)='01' AND LEN(@TelNo)=11 THEN @TelNo
WHEN LEFT(@TelNo,2)='02' AND LEN(@TelNo)=11 THEN @TelNo
WHEN LEFT(@TelNo,2)='05' AND LEN(@TelNo)=11 THEN @TelNo
WHEN LEFT(@TelNo,2)='07' AND LEN(@TelNo)=11 THEN @TelNo
WHEN LEFT(@TelNo,2)='09' AND LEN(@TelNo)=11 THEN @TelNo
WHEN LEFT(@TelNo,2)='08' AND LEN(@TelNo) BETWEEN 10 AND 11 THEN @TelNo
WHEN LEFT(@TelNo,3)='001' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0020' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0021' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0023' AND LEN(@TelNo) BETWEEN 12 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0024' AND LEN(@TelNo) BETWEEN 11 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0025' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0026' AND LEN(@TelNo) BETWEEN 13 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0027' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0030' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0031' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0032' AND LEN(@TelNo) BETWEEN 12 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0033' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0034' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0035' AND LEN(@TelNo) BETWEEN 12 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0036' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0037' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0038' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0039' AND LEN(@TelNo) BETWEEN 14 AND 15 THEN @TelNo
WHEN LEFT(@TelNo,4)='0040' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0041' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0042' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0043' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0044' AND LEN(@TelNo) BETWEEN 13 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0045' AND LEN(@TelNo) BETWEEN 12 AND 12 THEN @TelNo
WHEN LEFT(@TelNo,4)='0046' AND LEN(@TelNo) BETWEEN 11 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0047' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0048' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0049' AND LEN(@TelNo) BETWEEN 13 AND 16 THEN @TelNo
WHEN LEFT(@TelNo,4)='0050' AND LEN(@TelNo) BETWEEN 13 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0051' AND LEN(@TelNo) BETWEEN 12 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0052' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0053' AND LEN(@TelNo) BETWEEN 12 AND 12 THEN @TelNo
WHEN LEFT(@TelNo,4)='0054' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0055' AND LEN(@TelNo) BETWEEN 13 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0056' AND LEN(@TelNo) BETWEEN 12 AND 12 THEN @TelNo
WHEN LEFT(@TelNo,4)='0057' AND LEN(@TelNo) BETWEEN 12 AND 12 THEN @TelNo
WHEN LEFT(@TelNo,4)='0058' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0059' AND LEN(@TelNo) BETWEEN 11 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0060' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0061' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0062' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0063' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0064' AND LEN(@TelNo) BETWEEN 12 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0065' AND LEN(@TelNo) BETWEEN 12 AND 12 THEN @TelNo
WHEN LEFT(@TelNo,4)='0066' AND LEN(@TelNo) BETWEEN 12 AND 12 THEN @TelNo
WHEN LEFT(@TelNo,4)='0067' AND LEN(@TelNo) BETWEEN 12 AND 12 THEN @TelNo
WHEN LEFT(@TelNo,4)='0074' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0080' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0081' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0082' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0085' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0086' AND LEN(@TelNo) BETWEEN 15 AND 15 THEN @TelNo
WHEN LEFT(@TelNo,4)='0087' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0088' AND LEN(@TelNo) BETWEEN 15 AND 15 THEN @TelNo
WHEN LEFT(@TelNo,4)='0090' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0091' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0092' AND LEN(@TelNo) BETWEEN 13 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0093' AND LEN(@TelNo) BETWEEN 12 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0094' AND LEN(@TelNo) BETWEEN 13 AND 13 THEN @TelNo
WHEN LEFT(@TelNo,4)='0095' AND LEN(@TelNo) BETWEEN 11 AND 11 THEN @TelNo
WHEN LEFT(@TelNo,4)='0096' AND LEN(@TelNo) BETWEEN 11 AND 15 THEN @TelNo
WHEN LEFT(@TelNo,4)='0097' AND LEN(@TelNo) BETWEEN 13 AND 14 THEN @TelNo
WHEN LEFT(@TelNo,4)='0098' AND LEN(@TelNo) BETWEEN 11 AND 11 THEN @TelNo
WHEN LEFT(@TelNo,4)='0099' AND LEN(@TelNo) BETWEEN 14 AND 14 THEN @TelNo
ELSE NULL END)
RETURN ISNULL(@TelNo,@OldNumber)
--RETURN @TelNo--Use for returning NULL with non valid numbers
END
GO