Regular Expressions in Update query

  • Hi all,

    I've read about regular expressions in sql server, but have not seen examples of updating data with them.

    Our office has moved and I have to set the new phone numbers for everyone. Thankfully, all the changed info is the same, and the job entails switching the first four numbers in bulk (we were put on a new exchange when we moved) from 9217 to 6552.

    Can I get away with just writing a query, or do I need to fire up some c# code?

    Can anyone shed some light on the steps to take or point me to some examples?

    many thanks

    mcalex

  • No need to write C# code.

    A simple UPDATE will be enough:

    DECLARE @sampleData TABLE (

    telephone varchar(20)

    )

    INSERT INTO @sampleData VALUES('9217335621')

    INSERT INTO @sampleData VALUES('9217854234')

    INSERT INTO @sampleData VALUES('9217115526')

    INSERT INTO @sampleData VALUES('9217418795')

    INSERT INTO @sampleData VALUES('9217417896')

    UPDATE @sampleData

    SET telephone = '6552' + RIGHT(telephone, LEN(telephone) - 4)

    SELECT *

    FROM @sampleData

    -- Gianluca Sartori

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

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