Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2005
    Posts
    24

    Unanswered: Creating Function for Formatting Phone Number

    I want to create a function to format a phone number. This is what I have but it says I have a syntax error. Anyone have any suggestions???

    Thanks in advance!

    CREATE FUNCTION FormatPhone (@phone CHAR(10))

    RETURNS NVARCHAR(13)
    AS
    BEGIN
    DECLARE
    @number CHAR(14)
    SELECT
    if @number = 7 then
    set @number = RIGHT(LEFT(@phone,3) + '-' + RIGHT(@phone,4)) RETURN @number END

    elseif @number = 10 then
    set @number = ('(' + LEFT(@phone,3) + ') ' + RIGHT(LEFT(@phone,6),3) + '-' + RIGHT(@phone,4)) RETURN @number END
    end if

  2. #2
    Join Date
    May 2004
    Location
    New York
    Posts
    248
    why dont you try something like this

    let numchars = length(phone)

    if numchars = 10 then
    let scratch = "(",phone[1,3],")","-",phone[4,6],"-",phone[7,10]
    end if

    and the same thing for 7 numbers

    Hope this hels

    Cheers

    Artem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •