Insert a 0 if the first character is a numeric between 1-9

  • Hi all,

    How can I achieve the following:

    -------------------------------------

    If the phone number's first character is 1-9

    {add a leading 0}

    Else

    {Do nothing to the phone number}

    ---------------------------------------

    Thanks for your help.

  • SELECT CASE WHEN LEFT(PhoneNo, 1) LIKE [0-9] THEN '0' ELSE '' END + PhoneNo FROM dbo.Table WHERE...

  • UPDATE tbl SET phoneNumber = '0'+phoneNumber WHERE LEFT( phoneNumber, 1) LIKE '[1-9]'

    ... should work.

    EDIT: Dang Ninjas... sneaking in unexpectedly EVERYWHERE!


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Craig Farrell (6/21/2011)


    UPDATE tbl SET phoneNumber = '0'+phoneNumber WHERE LEFT( phoneNumber, 1) LIKE '[1-9]'

    ... should work.

    EDIT: Dang Ninjas... sneaking in unexpectedly EVERYWHERE!

    It's easy when you type more than 3 words / minute ;-)!

    P.S. Now you figured out the suffix in my name!

Viewing 4 posts - 1 through 3 (of 3 total)

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