how to split phone number based on - symbol dynamically

  • Hi Sql Experts,

    I would like to know how to split the phone number into two columns based on - symbol Dynamically.

    for example

    Phone Number

    123-12323

    1234-1222

    so output should be

    code number

    123 12323

    1234 1222

    Thankyou,

    Vj,

  • Something like this should work:

    SELECT PhoneNum

    , CHARINDEX('-',PhoneNum,1) AS ci

    , LEFT(PhoneNum,CHARINDEX('-',PhoneNum,1)-1) AS code

    , RIGHT(PhoneNum,LEN(PhoneNum)-CHARINDEX('-',PhoneNum,1))

    FROM

    (SELECT '123-12323' As PhoneNum

    UNION ALL

    SELECT '1234-1222') a;

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply